Topic 091 / Core
Stateless and stateful services
Chapter 6.3 · Scaling Services
What and why
Horizontal scaling (→ 90) only works if any instance can serve any request — true only if instances don't hold request-specific state that only they know about. A stateless service keeps no such state between requests: everything needed to serve a request is in the request itself or in a shared store. A stateful service holds state that matters (a WebSocket connection, an in-memory session) and needs a different scaling plan. Builds on: → 90 (vertical and horizontal scaling).
Diagram
| Stateless | Stateful | |
|---|---|---|
| Example | a handler reading everything from a shared DB/cache | a WebSocket connection, an in-memory session |
| Any instance can serve any request? | yes | no — only the instance holding the state |
| Scaling | add/remove instances freely | needs sticky routing or explicit handoff |
| Failure | lose one instance, nothing that mattered is lost | lose one instance, lose whatever it alone held |
How it works
- Identify what state a request handler touches that would be missing if a different instance served that user's next request.
- Move session-like state to a shared store (Redis, → 87) or encode it in a token the client carries (→ 162) — either way, every instance can reconstruct it, making the handler itself stateless.
- For state that genuinely can't be centralized cheaply (an open WebSocket, an in-process computation in flight), either route all of that client's traffic to the same instance consistently (sticky routing, via the load balancer, → 92) or design an explicit handoff for when that instance must go away.
- A stateless instance can be added, removed, or restarted at any moment with no coordination — this is what makes auto scaling (→ 95) and rolling deploys (→ 187) simple.
- "Stateless" describes the request-handling layer, not the whole system — the database and cache are still stateful; statelessness just means state lives somewhere shared and addressable, not locked inside one instance's memory.
Practical example
(numbers illustrative). A service keeps logged-in users' session data in each instance's local memory. With 4 instances and round-robin balancing, a user's request lands on a different instance than their last one roughly 75% of the time (3 of 4 instances aren't the one holding their session) — so about 3 in 4 requests fail to find the session. Moving session data to a shared Redis store removes this entirely: the failure rate drops from 75% to 0%, and any of the 4 instances can serve any request.
When to use it
- Use a shared store or client-held token for session data whenever more than one instance exists or ever will.
- Keep a connection genuinely stateful when the state is a live connection, not data about it — a WebSocket can't be centralized, only routed consistently or handed off.
- Avoid sticky routing as a first resort — it reintroduces a soft single-instance problem for the clients stuck to it, and should be the exception, not the default.
- Failure scenarios: in-memory state that works silently in development (one instance) and fails silently in production (many instances) — the bug appears only at the scale the architecture was supposedly designed for; sticky routing sending a disproportionate share of long-lived connections to a few instances, unbalancing load despite a correct-looking balancer config.
- Simpler alternative: for small or single-instance deployments, in-memory state is fine — the discipline matters once more than one instance is in play, not before.
Practice
(small coding exercise, 20 min, lab: Redis).
- Build a minimal login handler storing session data in a process-local map; run two instances behind a simple round-robin dispatcher and reproduce the "logged in on one instance, logged out on the other" bug.
- Move the session store to Redis; rerun the same two-instance test and confirm the bug is gone regardless of which instance handles a given request.
- List which parts of your test service remain stateful after the fix (the Redis store itself) and explain why that's fine.
Done when either instance can serve any request in sequence with identical results.
Shared lab setupKey takeaway
Statelessness at the request-handling layer is what makes horizontal scaling (→ 90) and auto scaling (→ 95) simple; state doesn't disappear, it moves to a shared store or the client. Done when you can: point to every piece of state in a given service and say whether it's shared, client-held, or genuinely stuck on one instance — and what that last category costs. Used next in → 92 (load balancing, which assumes statelessness by default), → 98 (graceful shutdown, which must handle whatever state wasn't made stateless).
References
None — this topic makes no external claims.
Source directory