LESSON 07 · Shared state

Keep web servers replaceable

Any healthy web server should be able to serve the next request.

SYSTEM VIEW

The architecture

Diagram view
Times logged out—
Session—
Lookups—

Watch a login get lost between two servers, then survive a restart once sessions live in a shared store.

  1. A user logs in, and the load balancer sends the request to Web A. Web A keeps the session, what it remembers about this user between requests, in its own memory.

    What changed

    • Session in Web A’s memory
  2. The balancer sends the user’s next request to Web B. Web B has no copy of the session, so the user appears logged out.

    What changed

    • Web B has no session
    • User appears logged out
  3. Sticky sessions route this user back to Web A every time, so the login works again. But the session still lives on one machine.

    What changed

    • Balancer pins the user to Web A
    • Login works again
  4. A rolling deploy restarts each server. When Web A restarts, the sessions in its memory are gone, and every user pinned to it is logged out.

    What changed

    • Web A’s sessions, on restart
    • Pinned users logged out
  5. The user logs in again. This time Web A writes the session to a shared store, such as Redis or a database, that every server can reach, and the browser gets only a random session ID.

    What changed

    • Shared session store
    • Browser holds only a session ID
  6. A deploy restarts Web A, and the balancer, no longer sticky, sends the user to Web B. Web B looks the session up in the store, typically about a millisecond on the same network, and the user stays logged in.

    What changed

    • Balancer no longer sticky
    • 1 store lookup, about 1 ms
  7. Any healthy server can now serve the next request. A shared store costs a lookup but can log someone out at once; a signed token skips the lookup but stays valid until it expires.

    Sessions kept in

Space plays · ← → step · Home resets

THE IDEA

How it works

A session is what the server remembers about a logged-in user between requests. If a session exists only on one web server, a later request sent to another server may appear logged out. Sticky sessions route a user back to the same server, but they make server removal and failure recovery harder: a rolling deploy that restarts each server logs all those users out.

There are two common ways out. The first is a shared store: move session information into Redis or a database that all web servers can reach, and give the browser only a random session ID. Each request costs a lookup, typically about a millisecond on the same network, and the store needs its own availability plan. In exchange, deleting a record logs someone out at once.

The second is a signed token, such as a JSON Web Token (JWT). At login, the server signs a small document containing the user ID and an expiry, and the client sends it with each request. Any server holding the verification key can check it without a lookup. The cost is revocation: a token stays valid until it expires unless you add a server-side deny list, which brings back a lookup. Teams often pair expiries of minutes with a refresh step. Standard JWT payloads are readable, so they should not hold secrets.

Either way, the web tier is stateless with respect to sessions: servers can be added or removed without moving anyone’s session. The system still has state; it simply lives outside the web process.

DECISION POINT

A login works on Server A, then fails when the next request lands on Server B.

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Stateless web servers do not keep unique user sessions locally.
  • A shared store makes revocation easy; signed tokens avoid the lookup but are hard to revoke early.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A team replaces server-side sessions with signed tokens that last 24 hours. A fired employee’s access must end within minutes. What is the catch?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.