Networking / Sessions vs tokens

Sessions vs tokens

After Ana logs in, how does the server know the next request is hers? Follow both answers: a row the server keeps, and a signed note the browser carries.

Intermediate7 steps~7 min
Browser — Ana’s, carries the proof of login
Server — must recognise each request
Session store — the server’s table of who is logged in
Token — signed note: readable top, signature below
// THE SHORT VERSION

In 30 seconds

HTTP forgets who you are between requests, so a login has to be proved again each time.

With a session, the server keeps a table of who is logged in and gives the browser a random id that points into it. With a token, the server signs a small note saying who you are and until when, and the browser carries the whole note.

Sessions are easy to cancel but need a store every server can reach. Tokens need no store but stay valid until they expire.

Sessions vs tokens, step by step

STEPS01 / 07

A token can travel in a cookie too: the real difference is where the login state lives. Signed here with one secret key; with public-key signatures, checking servers need only the public key.

STEP 01 / 07 HTTP forgets you
THE MATH
HTTP is stateless: no request remembers the one before it
  1. Step 1: HTTP forgets you. HTTP has no memory: each request arrives alone, with nothing tying it to the one before. After Ana logs in, her next request looks like a stranger’s unless it carries some proof of the login. Sessions and tokens are the two ways to supply that proof.
  2. Step 2: Session: server remembers. At login the server writes a row in a session store and hands the browser only the row’s random id, in a cookie. The browser returns that id with every request, and the server looks it up to learn who is asking. All the facts stay on the server.
  3. Step 3: Token: a signed statement. A token turns this around. At login the server writes the facts (who, and until when) into a small document, signs it with a secret key, and gives the whole thing to the browser. The server stores nothing. The best-known format is the JSON Web Token (JWT).
  4. Step 4: Token: check, no lookup. The browser sends the token with each request. The server recomputes the signature with its key: if it matches and the expiry time has not passed, the contents are trusted as written. Changing even one letter of the payload breaks the match. No table, no lookup.
  5. Step 5: Many servers. Behind a load balancer, a request may land on any server. With sessions, all of them must reach one shared store, which each request then waits on. With tokens, each server needs only the key and can check a request by itself.
  6. Step 6: Logging out. Ana logs out at 14:20. Her session row is deleted, so the id stops working at once. Her token, though, is still correctly signed and not yet expired: any copy keeps working until 15:00. Recalling it early means keeping a list of cancelled tokens, which is server-side state again.
  7. Step 7: Where each fits. Sessions suit a website talking to its own server, where instant logout matters and one store is easy to run. Tokens suit APIs used by apps and by many separate services that all trust one login. Many systems mix them: short-lived tokens plus a server-side way to cancel.
VALUES SO FAR
HTTPstatelessrule
sid····cookie
store····server
payload····public
key····secret
check····rule
logout····event
token····result

Spotted a mistake on this page?