System design concepts · Compute and concurrency

Stateful vs Stateless

Short answer

A stateless service keeps no client-specific data in its own memory or disk between requests: each request carries what it needs, or the service fetches it from a shared store, so any instance can handle any request. A stateful service remembers something from earlier requests, such as a session, an open connection or a slice of a database, so requests for that client or key must reach the instance that holds it. Stateless tiers scale by adding instances behind a load balancer, and losing an instance loses nothing but in-flight work. Stateful components need routing, replication and careful failover. Most real systems are a stateless application tier in front of a small number of stateful stores that are managed with much more care.

Read the video transcript

Every request to your service lands on one of these servers. The question is whether that server remembers you. A stateful server keeps your session in its own memory. So the load balancer has to send you back to the same server every time. That is a sticky session. It works, until that server dies. Every session it held disappears, and those users are logged out, with empty carts.

A stateless server keeps nothing between requests. The session moves out, to a shared store like Redis, or into a signed token that the client carries. Now any server can take any request. Kill one, and the next request simply goes to another, with the cart intact. Scaling gets easy too. Add three more servers, and they take traffic straight away. Nothing has to move.

But the state did not vanish. It moved into the store, and that store is now the piece you replicate, back up, and scale with care. That is the usual shape. A stateless app tier, in front of a few stateful stores. Databases, caches and chat gateways stay stateful. Your API servers should not be.

Stateful vs Stateless: comparison table

AspectStatefulStateless
Where state livesIn the instance: memory, local disk or an open connectionOutside the instance: a database, a cache, or the client (cookie or signed token)
Load balancingRequests must reach the owner: sticky sessions or key-based routingAny instance works: round robin, least connections
Scaling outAdd nodes and move data or connections to themAdd instances; nothing moves
Instance failureState is lost unless it was replicated; clients reconnect or fail overRetry on another instance
DeploysDrain connections, hand off partitions, roll one node at a timeRolling restart with little ceremony
Kubernetes objectStatefulSet with stable identity and persistent volumesDeployment
ExamplesDatabases, caches, WebSocket gateways, game servers, Kafka brokersREST API servers, image resizers, token-checking auth middleware

When to use each

Keep a component stateless when

  • It sits on the request path and needs to scale with traffic.
  • You want instances to be disposable, so autoscaling and spot capacity are safe.
  • The state it needs can be fetched quickly from a cache or database, or carried in the request.

Accept a stateful component when

  • The state is the product: a database, a cache, a queue.
  • A long-lived connection is required, as in chat, presence and multiplayer games.
  • Fetching the state on every request would be too slow, and locality wins (an in-memory game world, a stream processor holding window state).
The verdict

Keep a component stateless when

It sits on the request path and needs to scale with traffic.

Otherwise

Accept a stateful component when

The state is the product: a database, a cache, a queue.

Stateless servers can be added or removed at any time because no session lives on them; every request reads state from the shared store.

Stateless does not mean there is no state

A stateless service moves its state somewhere else. Sessions go to Redis, user data goes to the database, and sometimes the client holds it as a signed token. The service becomes easy to scale, and the store becomes the thing you have to scale, replicate and back up.

The state still exists: either the server looks a session id up in a shared store, or the client carries a signed token that any server can verify with the key.

Signed tokens such as JWTs are the purest version: a server can verify one with nothing but a key. The catch is revocation. If a token must stop working before it expires, something has to remember that, which brings state back as a denylist or forces short expiry with refresh tokens.

Sticky sessions are a halfway house

A load balancer can pin a client to one instance with a cookie or a hash of its address. That lets a service keep session data in memory without a shared store. It works until an instance dies or the pool resizes, at which point those users lose their sessions. Treat sticky routing as an optimization for cache locality, not as the place your only copy of state lives.

Sticky sessions pin each user to one server, so when that server dies its users land on a server that has never seen their session.
Interview angle

How it comes up in a system design interview

Almost every design has a moment where you draw a row of app servers behind a load balancer. Say out loud that they are stateless, because that is what makes the horizontal scaling and failure story simple. Then spend your time on the stateful parts, which is where the interviewer will push.

Where you will meet it

  • In a chat system, which gateway holds a given user connection, and how does a message from another server find it?
  • Where do sessions live, and what happens to logged-in users when an app server is replaced?
  • How do you scale the stateful tier: partitioning, replication, or both?
  • Can the rate limiter be stateless, or does it need a shared counter store?
How a strong answer sounds
“The API servers are stateless, so the load balancer can use least connections and we can autoscale them freely. The connection gateways are stateful because they hold WebSockets, so we keep a registry from user ID to gateway in Redis, and on gateway failure clients reconnect to any gateway and re-register.”

Common mistakes

  • Calling a service stateless while it keeps a local in-memory cache that other instances depend on being warm or correct.
  • Using sticky sessions as the only copy of session data.
  • Forgetting that moving state to one shared store turns that store into the new bottleneck and single point of failure.

Frequently asked questions

Is HTTP stateless?

Yes. Each HTTP request is independent at the protocol level. Applications add state on top with cookies, session IDs and tokens.

Is REST stateless?

Statelessness is one of the REST constraints: the server keeps no client context between requests, and each request carries everything needed to process it. Resource state in the database is fine; what REST rules out is conversation state held on the server.

Can a stateful service scale horizontally?

Yes, but it takes more work. You partition the state so each node owns a slice, route requests to the owner, and replicate each slice so a node failure does not lose data. Databases, caches and message brokers all do this.

Are JWTs stateless?

Verification is stateless because a server only needs the signing key. Revocation is not: to cancel a token before it expires you need a shared denylist or very short expiry, and both reintroduce state.

Learn and practise

Lessons

Problems where this choice matters

Knowing the trade-off is half of it

Practise saying it under follow-up questions: a timed system design interview with an AI interviewer that probes your choices and grades against a rubric.

Start a mock interview