Stateless architecture
Why any server instance should be able to answer any request, where state goes instead, and what a JWT does not fix.
One cashier at the counter. Any cashier can pick up where another left off.
That is a stateless server. Nothing about your visit is kept in one cashier's head. The details are written on the receipt, so whoever serves you next can carry on.
How it works
A stateless server does not rely on its own memory for anything that must survive between requests. Any instance can handle any request.
Request 1: User → Server 1
Request 2: User → Server 2 (still works, no local state needed)
The state does not vanish. It moves somewhere every instance can reach:
- the database
- a shared store such as Redis
- a signed token the client carries
- shared session storage
Tokens and sessions
With token-based login, the backend issues a signed token and the client sends it back on each request.
Login → Backend → token → Client
Later: Client → Authorization: Bearer token → any server → validate
No server has to remember the login, which suits several instances behind a load balancer. The alternative is a server-side session stored in a database or cache: the cookie holds only an id, and the state lives in the shared store. Both are stateless at the app tier. They differ in how easily you can revoke a login, which I cover in the authentication note.
A token does not make the whole app stateless
This is the trap. A token solves login. Other state can still sit on a single instance:
- uploaded files written to local disk
- temporary data held in memory
- WebSocket connections, which pin a client to one instance
- background jobs running inside the process
Each one needs its own answer. Files go to object storage. Temporary data goes to a shared cache. Long-lived connections need a pub/sub layer between instances. In-flight work needs a durable queue, so a job survives the instance being stopped.
A quick test
I check a design with one question: if I stop this instance right now and send the next request to another one, what does the user lose? If the answer is nothing, the tier is stateless. If the answer is a half-finished upload, a cart, or a running job, that is state living in the wrong place. Running two instances locally and alternating requests between them finds these quickly, long before a load balancer exists. The test costs nothing, and it turns a vague property into something you can observe: the same user, two instances, and no difference in what they see.
Where I used it
I haven't shipped a horizontally scaled, stateless deployment in a public project yet.
In POS SaaS, sessions use rotating refresh tokens with reuse detection. I did not write a trade-off for that choice, so I will not invent one here. POS is early-stage, and this is a design, not a production result.
Trade-offs
- State needs a home. Moving state out means a shared store that now needs care: backups, limits and a plan for when it is down.
- An extra lookup. Reading a session from a store costs a round trip that local memory did not. Tokens avoid the lookup, at the price of harder revocation.
- Long-lived connections still stick. WebSockets keep a client on one instance unless you add a broadcast layer across instances.
- Jobs need a durable queue. An in-memory task on an instance that gets terminated is lost. A message queue keeps the work safe.
- It is a discipline, not a switch. One stray file written to local disk breaks the property quietly, and you find out under load.
The rule I use: keep the app tier stateless by default, because it keeps every later option open. Adding more instances, rolling deploys and failing over all depend on it.