Does the server remember you between requests? This choice decides how easily you can scale.
A stateless server keeps nothing about you between requests. A stateful server remembers you (a login, a cart, a game position) in its own memory or disk.
State is anything the system must remember to answer the next request correctly: who is logged in, what is in the shopping cart, which step of a form you are on, the position of a player in a game.
In a stateful design, the server stores this state itself, usually in memory. The second request must reach the same server, or the state is not found. In a stateless design, every request carries (or can look up) all it needs. The server holds no memory of earlier requests, so the next request can go to any server.
| Stateless | Stateful | |
|---|---|---|
| Remembers earlier requests? | No. Each request is complete by itself | Yes. Needs earlier context |
| Which server may answer? | Any server | Often one particular server |
| Scaling out | Add servers, done | Must move or copy the state |
| A server crashes | Nothing is lost | That server's state may be lost |
| Per-request cost | May re-read the state each time | State is local and fast |
| Good for | Web and API tiers, serverless | Databases, games, live connections, some caches |
| Approach | How it works | Good and bad |
|---|---|---|
| Server memory + sticky sessions | Load balancer always sends a user to the same server | Easy, but a crash loses sessions and load can become uneven |
| Shared session store | Sessions live in Redis or a database. Servers look them up | Any server works, and sessions survive crashes. Adds a network call and a store to run |
| Client-side token | A signed token (for example a JWT) carries the data in each request | No lookup needed. Hard to revoke, and the token must stay small |
Simplified and fixed (no random numbers): the balancer's second-click choices in the plain mode are a fixed example, and a request for a crashed server moves to the next one. In real life about two in three users would move on 3 servers.
| Term | Meaning in easy words |
|---|---|
| State | What the system must remember to handle the next request correctly |
| Stateless | The server keeps no memory of earlier requests, so any server can serve any request |
| Stateful | The server remembers earlier requests, so the same server is needed again |
| Session | A series of requests from one user, with the data kept between them |
| Sticky session | The load balancer always sends a user to the same server |
| Session store | A shared place (Redis, database) that holds sessions for all servers |
| Token | A small signed piece of data the client sends with each request to prove who it is |
| Horizontal scaling | Adding more servers instead of making one bigger |
| Idempotent | Safe to repeat: doing it twice has the same result as doing it once |
| Shared-nothing | Servers do not share memory, and any shared data lives in a separate store |
| Connection affinity | A long connection (such as a WebSocket) is tied to one server |
| Choice | We gain | We lose |
|---|---|---|
| Stateless servers + shared store | Free scaling and crash safety | A store to run, and a lookup per request |
| Sticky sessions | Simple, fast local memory | Sessions lost on a crash, uneven load, hard rolling deploys |
| Client-side token | No server lookup at all | Larger requests, hard to cancel a token before it expires |
| Stateful for speed | Very low latency, rich local data | Harder to scale, move and recover |