Why prefer stateless services and how do you handle sessions?
566 views
32 of 106
1
The problem
Question presented to candidate:
"We want to run our web app on many instances behind a load balancer and autoscale it. Why does everyone say the instances should be stateless? Users still log in and have carts, so where does that session data go, and what are the trade-offs between the options?"
What a strong answer should cover:
Definition: a stateless service keeps nothing about a client in process memory between requests, so any instance can serve any request and instances can be added, killed or replaced freely
What breaks with in-memory sessions: round-robin sends the next request to an instance that never saw the login; sticky sessions patch that but lose sessions when the instance dies and skew load
The two real answers: a shared session store (session ID in a cookie, data in Redis or a database โ central and revocable, one lookup per request) or a signed token such as a JWT (no lookup, but hard to revoke before it expires and sent on every request)
Token details: HMAC vs public-key signatures, short exp plus refresh tokens, a jti denylist for logout, HttpOnly/Secure/SameSite cookies, the roughly 4 KB per-cookie budget
The other hidden state to move out: uploaded files, local caches that must be consistent, scheduled jobs, rate-limit counters, open WebSocket connections
Clarifying questions expected:
Do we need instant logout or permission revocation (for example banking or admin roles)?
Is this browser traffic, mobile apps, service-to-service calls, or long-lived WebSockets?
Code / implementation expected: Optional โ sketching the session lookup or a token sign/verify step helps; the example simulates all four approaches.
stateless
sessions
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Beginners to system design โ you have built a web app with a login, nothing more.
Difficulty: Medium
Verified with: Node 22.22 and Python 3.12 (the example prints the same output, byte for byte, including the token).
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the exact words an interviewer expects. All output shown was produced by actually running the example; the Verified section quotes it in full.
1. Why This Even Matters โ A Story First
A music festival has three entrance gates. On day one you buy a ticket at gate 1 and the guard there remembers your face. On day two you walk up to gate 2. That guard has never seen you: "Sorry, no ticket." Annoying, but you go back to gate 1 โ and that works, until the gate 1 guard goes home sick. Now nobody remembers you.
The solution is hidden so you can try first
14 min read ยท 11 sections
4
Read the code
Where the session lives: memory, sticky, shared store, signed token