CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 3 of 8 · 3 min read

Cookies, sessions and the session store

How a login is remembered across requests, and how big the store must be.

Think of a cloakroom ticket. The ticket is small and means nothing alone. The attendant uses its number to find the coat in the back room.

Logins work in the same way. The book "Understanding Distributed Systems" describes it. The client sends its credentials to a login endpoint, which checks them. If they are right, the endpoint creates a session token, a long random value, and returns it, usually in an HTTP cookie. The client sends the token with every later request. The application uses the token to fetch a session object from an in-memory cache or an external store. That object holds the user's identifier and roles, and the handlers use it to decide whether to allow the request.

Cookie

A cookie is a small piece of text that a server asks the browser to keep, and that the browser sends back with every later request to that server.

Example: the server replies with Set-Cookie: sid=9f3c...; HttpOnly; Secure. The browser then adds Cookie: sid=9f3c... to each request.

Session

A session is the saved facts about one logged-in visit: who the user is, what they may do, and often a few temporary items.

Example: the session for the cookie sid=9f3c says "user 7, role customer, cart id 55".

The steps of a login with a shared session store:

  1. The user posts the name and password to POST /login.
  2. The server checks them against the user table.
  3. The server makes a random session id and saves session:9f3c → {user: 7, role: customer} in the shared store, with an expiry.
  4. The server sets the cookie.
  5. Each later request carries the cookie. Any server reads the session from the store.
  6. Logout, or expiry, deletes the entry.

Three cookie flags matter for safety.

Flag Effect
HttpOnly Scripts on the page cannot read the cookie, which limits theft through injected scripts
Secure The cookie is sent only over HTTPS
SameSite The cookie is not sent on requests started from other sites, which limits forged requests

A worked example of sizing. A site has 2 million users logged in at the peak. Each session record takes 1 kilobyte (made up).

  • Memory: 2,000,000 × 1 KB = 2,000,000 KB = 2 GB.
  • That fits comfortably in the memory of one machine, and easily across a few.
  • Each request makes one lookup. At 30,000 requests per second, that is 30,000 lookups per second. A fast memory store can serve far more than that from one node, so the load is fine, but the store must still be duplicated for safety (Level 4).
  • If a lookup takes 0.5 milliseconds on the inside network, it adds 0.5 ms to each request.

The store is now a vital part. If it is lost, everyone is logged out. So it is copied, and monitored like a database.

Common mistake

Using a short, guessable session id, such as an increasing number. An attacker can try the next numbers and take over other people's sessions. Ids must be long, random and from a secure random source.

Check yourself:

  • What does the cookie hold, and what does the session store hold?
  • Why is a session store a part that needs a copy for safety?