Dashboard
Courses
Problems
Badges
Theme
Sign inStart free
Courses/Foundations of Scale
Lesson 5 of 16

Stateless services and sessions

Move user state out of your servers so any instance can serve any request.

14 min read

Where state hides

A service is stateless when nothing it needs to answer the next request lives only in its own memory or local disk. In practice, state sneaks in: a login session kept in a server-side map, an uploaded file written to /tmp, a shopping cart held in a process variable, or a WebSocket connection pinned to one machine. Each of these ties a user to a particular instance. When that instance is replaced during a deploy or crashes, the user is logged out, the upload disappears and the cart is empty. Finding and removing hidden state is the first step to scaling out.

Sticky sessions and why they hurt

One quick fix is sticky sessions: the load balancer always sends the same user to the same instance, usually by a cookie. It works, but it undermines the reasons you scaled out. Load becomes uneven because some instances collect heavy users, scaling in or deploying kills active sessions, and a failed instance takes its users' state with it. Sticky routing is acceptable as a performance hint, for example to reuse a warm local cache, but correctness should never depend on it.

Externalise the session

The usual design keeps sessions in a shared store that every instance can reach. Each request carries a session ID in a cookie, and the service looks it up on every call.

  • In-memory key-value store such as Redis: sub-millisecond lookups, TTL per session, replicate it so a node loss does not log everyone out
  • Database table: durable and simple, but adds a query to every request; fine at low traffic
  • Signed tokens such as JWTs: the session lives in the token itself, so no lookup is needed, but revoking a token before it expires needs a deny list or short lifetimes

Sizing a session store

Session stores are small but very hot. Five million active sessions at 1 KB each is only 5 GB, which fits in memory on one node. The load is the issue: if every request reads the session, the store sees the full request rate of the site, possibly tens of thousands of lookups per second. Use TTLs so abandoned sessions expire on their own, refresh the TTL on activity, and decide what happens if the store is unreachable. On the canvas, a replicated cache between the API tier and the database is the standard shape.

Key takeaways

  • A stateless instance can be added, replaced or killed without users noticing
  • Sticky sessions are a performance hint, never a correctness requirement
  • Keep sessions in a replicated in-memory store with TTLs, or in signed short-lived tokens
  • A session store sees the full request rate even though its data is small

Sign in to track lessons and earn badges.

Horizontal scaling and load balancingAPI basics: REST, pagination and idempotency

Challenge checkpoint

Course: 0/16 lessons · 0/10 challenges0%

Build it on the canvas, pass the review (70+) and mark it complete to count it toward the course badge.

  1. 2/10Design a Session StoreMove login sessions out of the web tier into a replicated in-memory store with TTLs and a clear fallback.Easy
All 10 challenges →