Stateful vs Stateless Services
Where a service keeps what it remembers between requests, and what that choice costs: stateless servers that scale and fail easily, and the stateful ones some real-time systems still need.
Overview
State is anything a service remembers between requests: who is logged in, what is in a cart, which WebSocket connections are open, a running total. A stateless service keeps none of it in its own memory or disk. Every request carries, or can fetch, everything needed to answer it, so any copy of the service can take any request. A stateful service keeps some of it locally, so a request has to reach the copy that holds its data.
The choice shapes almost everything about running a service. Stateless servers can be added, removed, restarted and redeployed at any time, because none of them is special. Stateful servers need routing that finds the right one, a plan for moving data when servers are added, and a way to recover when one dies.
The state never disappears; it only moves. A stateless web tier works because its state lives somewhere else: a database, a cache such as Redis, or a signed token the client carries. Interviewers want to hear where each piece of state lives, and why there.
What you will be able to do
- Say where each kind of state in a design lives and what happens when that place fails.
- Move session state out of app servers, and compare a shared store, sticky sessions and signed tokens.
- Explain the JWT revocation problem and the usual fix.
- Decide when keeping state in a server is worth it, and route requests to the right server.
- Plan recovery for stateful parts: rebuild, replicate, or checkpoint and replay.
Key concepts
- Kinds of state
- Stateless servers
- Shared session store
- Sticky sessions
- Signed tokens (JWT)
- Routing by key
- Rebuilding lost state
- Kubernetes StatefulSets
Interview questions
- Why do we prefer stateless application servers?
- Compare sticky sessions with a shared session store.
- How does a JWT make authentication stateless, and what does it cost?
- When would you choose a stateful service, and how do you scale it?
- A stateful node crashes. How does the system recover?
- When would you use a Kubernetes StatefulSet instead of a Deployment?
- Your web app logs users out at random after you add a second server. Why, and what do you do?
- Is a WebSocket service stateful? How do you design around it?
Sign up free to read the full topic
More System Design topics
Start free on Cariara · Coding problems · System design studio