Track 07 · Lesson 3 of 18Identity and login

Track 07 · Identity and login

Sessions versus tokens

Lesson 3 of 18· 7 min

After you log in, the server has to recognize you on every later request. It can give you a random session ID that it looks up in its own store, or a signed token it can verify without looking anything up. Each choice gives something up.

Log in, then kick yourself out

Try both modes

Not logged in

What the browser sends

(nothing yet)

What the server stores

(no sessions)
  1. Log in to start.

Server sessions#

The server stores “session 8f3a… = user 4821” in a database or Redis. The browser holds only the random ID, in a cookie. Revoking is easy: delete the row and the user is out, everywhere. The cost is state: every request needs a lookup in a store every server can reach.

Self-contained tokens#

A JWT carries “user 4821, role user, expires 10:00”, signed by the server. Any server with the key can check it with math, no lookup. The catch is revocation: a token stays valid until it expires, unless the server keeps a denylist, which brings the lookup back. That’s why tokens are kept short-lived.

Most apps use both ideas#

A short-lived access token for everyday calls (fast, stateless) plus a refresh token the server tracks and can revoke (like a session). The hotel below shows how the pieces fit together, and the next lessons take each one apart.

The hotel analogy

Tap a card to flip it

Tap a card to flip it to the technical version. 0 of 6 flipped.

Check yourself

An admin logs a user out everywhere. With plain JWTs and no denylist, when does the user actually lose access?