Stateful vs stateless services
Overview
One of the first things you do when a system must grow beyond one server is make the application servers stateless. It sounds like a small detail; in practice it decides whether you can add servers freely, survive a crash or deploy during the day. This lesson explains what state is, why it causes trouble and where it should live instead.
What counts as state
State is any data a server remembers between requests. Common examples:
- login sessions,
- shopping carts and drafts,
- uploaded files saved to local disk,
- in-memory caches and counters,
- background jobs held in a list in memory,
- open connections, such as WebSockets.
The problem with stateful servers
Suppose Asha adds shoes to her cart and the cart lives in Server A’s memory. Several things can now go wrong:
- Load balancing breaks. If her next request goes to Server B, her cart is empty.
- Failures lose data. If Server A crashes or is replaced, every cart on it is gone.
- Deploys hurt. Restarting a server to ship new code logs out or empties the carts of everyone on it.
- Scaling is uneven. You can’t move users between servers, so one busy server stays busy even after you add more.
You can work around the first problem with sticky sessions (the load balancer always sends a user to the same server), which week 2 covers. That only hides the problem: the other three remain.
The fix: move the state out
A stateless server keeps nothing between requests that another server would need. Every request carries enough information (usually a session ID or token) for any server to handle it, and the real data lives in shared systems:
| State | Where it moves |
|---|---|
| Sessions | A shared cache such as Redis, or a signed token on the client |
| Carts, drafts, profiles | A database (with a cache in front if hot) |
| Uploaded files | Object storage such as S3, served through a CDN |
| Background jobs | A message queue processed by workers |
| Counters, rate limits | Redis or another shared store |
The state still exists; it has just moved to systems designed to store it safely, replicate it and share it.
Sessions: server-side store or token?
There are two common ways to make authentication stateless:
- Session store: the client holds a random session ID in a cookie; servers look it up in Redis. Easy to revoke (delete the key), but every request needs a lookup.
- Signed tokens (for example JWTs): the token itself contains the user ID and an expiry, signed by the server. Any server can verify it without a lookup. The catch is revocation: a stolen token works until it expires, so tokens are kept short-lived and paired with refresh tokens.
Either is fine in an interview; say which you chose and why.
What statelessness buys you
- Horizontal scaling: add servers behind the load balancer and they immediately share the work.
- Autoscaling: the platform can add servers at peak and remove them at night without losing anything.
- Failure tolerance: if a server dies, the load balancer stops sending it traffic and nobody loses data.
- Easy deploys: replace servers one by one (a rolling deploy) with no user impact.
Where state can’t be avoided
Some components are stateful by nature, and that’s fine as long as you treat them carefully:
- Databases and caches hold state by definition. Weeks 3 and 4 cover how to replicate and partition them.
- WebSocket or chat gateways hold open connections. A user’s connection lives on one gateway, so the system needs a registry of which gateway holds which user.
- Game servers and collaborative editors often keep a live session in memory for speed, with frequent snapshots to durable storage.
The design principle is to keep state in as few, well-understood places as possible, and keep everything else stateless.
Recap
A stateful server remembers data between requests, which breaks load balancing, loses data on crashes and makes deploys painful. Stateless servers push that data into shared databases, caches, object storage, queues or signed tokens, so any server can handle any request. That single change makes horizontal scaling, autoscaling and safe deploys possible.
Make it stateless
A small photo-editing site runs on one server. It keeps (1) logged-in sessions in a dictionary in memory, (2) uploaded photos in a folder on the server's disk, (3) a 'recently viewed' list per user in memory, and (4) background resize jobs in an in-memory list. The team wants to run 5 servers behind a load balancer. For each piece of state, say where it should move and why.
(1) Sessions:
(2) Uploaded photos:
(3) Recently viewed:
(4) Resize jobs:(1) Sessions: move to a shared store such as Redis (session ID in a cookie), or use
signed tokens (for example JWTs) that the client sends with every request. Any
server can then authenticate any request.
(2) Uploaded photos: move to object storage (S3, GCS, Azure Blob) and serve them through
a CDN. A local disk is only visible to one server and is lost if it's replaced.
(3) Recently viewed: store in Redis (a small list per user, with an expiry) or in the
database if it must survive cache restarts. It's per-user data that any server
must be able to read.
(4) Resize jobs: move to a message queue (SQS, RabbitMQ, Kafka) and process them with
separate worker processes. An in-memory list is lost when the server restarts
and can't be shared between servers.Summary
- A stateful server keeps client data (sessions, carts, uploads) in its own memory or disk between requests.
- A stateless server keeps nothing between requests; any server can handle any request.
- State doesn't disappear: it moves to a database, a shared cache, object storage, a queue or a signed client token.
- Stateless app servers make horizontal scaling, autoscaling and rolling deploys simple.
- Some services are inherently stateful (databases, WebSocket gateways); they need replication, partitioning or sticky routing.