← Back to System Design, page 2

system-design-0027 · Easy fresher

What does it mean for a service to be stateless, and how do you handle user sessions in a horizontally scaled web application?

A stateless service keeps no per-user data in its own memory between requests, so any instance can serve any request. Session data moves elsewhere: into a shared store like Redis keyed by a session ID cookie, or into a signed token such as a JWT that the client sends with each request. This allows load balancing, autoscaling and painless restarts.

Full answer

The problem with in-memory sessions

If a student logs in and server A stores their session in A's memory, the next request might go to server B, which knows nothing about them, so they are logged out. Workarounds such as sticky sessions (always send this user to A) break when A restarts or is removed by autoscaling, and they spread load unevenly.

Stateless means

The server's memory holds no information a later request depends on. Everything needed is in the request itself or in a shared backing store. Then servers become interchangeable: add 10 during an exam rush, remove them after, deploy one at a time, and nobody notices.

Option 1: server-side sessions in a shared store

  1. On login, create a random session ID, store session:<id> -> {user_id, roles, expires} in Redis with a TTL.
  2. Send the ID in a cookie: Set-Cookie: sid=...; HttpOnly; Secure; SameSite=Lax.
  3. Every server looks up the session in Redis.

Pros: easy to revoke (delete the key), small cookie, the server controls the data. Cons: a Redis lookup per request and Redis must be highly available.

Option 2: signed tokens (JWT)

The server signs a token containing claims (sub, exp, roles). Any server with the verification key checks the signature without a lookup.

Pros: no shared session store, works across services. Cons: hard to revoke before expiry, so keep access tokens short-lived (for example 15 minutes) and use a refresh token stored server-side for longer sessions. Do not put secrets or large data in a JWT; it is only signed, not encrypted, by default.

Other state to move out

  • Uploaded files: to object storage (S3-compatible), not local disk.
  • Caches: shared cache, or accept that each instance has its own small local cache.
  • Scheduled jobs: a job scheduler or queue, not a timer inside one web server.
  • WebSocket connections are inherently stateful; keep connection routing in a separate gateway tier.

By level

Fresher: Explain why storing login data in one server's memory breaks with many servers, and describe a shared session store. The logged-out-on-another-server example is the expected starting point.

Mid: Compare Redis sessions and JWTs including revocation and cookie security, and list other state that must leave the instance. Expect cookie flags and JWT revocation to be covered.

Senior: Discuss session store availability and multi-region replication, token rotation and revocation lists, security implications, and how stateful components such as WebSockets are isolated. It should also say how stateful pieces like WebSockets are kept separate.

Key points

  • Stateless servers keep no per-user memory between requests
  • Sticky sessions are a fragile workaround
  • Shared session store: easy revocation, one lookup per request
  • JWT: no lookup, but hard to revoke, so keep it short-lived
  • Move files, caches and jobs out of the instance too

Common mistakes

  • Storing sessions in local memory behind a load balancer
  • Treating JWTs as encrypted
  • Using long-lived JWTs with no revocation plan
  • Saving uploads to the app server's local disk

Follow-ups

  • How would you log a user out of all devices with JWTs?
  • What happens if the Redis session store goes down?
  • Why should session cookies be HttpOnly and Secure?

Tips

  • Start with the logged-out-on-server-B example
  • Compare the two session options in a small table
  • Mention cookie security flags

Evaluation rubric

  • Clear definition of stateless
  • Explains why in-memory sessions fail
  • Compares shared store and token approaches correctly
  • Mentions other state such as files and jobs