Skip to content

Security · 1 of 6

Sessions vs tokens

Step 1 · The pattern

A server-side session: looked up every time

The user signs in and gets a random session ID. On every request the API looks it up in the session store (40 ms here). Signing out deletes the row, and the next request fails at once. The price is a lookup on every request, on every server. (All numbers are illustrative.)

  • a request
  • served

In short

Sessions vs tokens: how it works and how it breaks

After you sign in, every request has to prove who you are. A server-side session is a random ID: the server looks it up on each request, so ending the session ends access at once, at the cost of a lookup every time. A self-contained token (often a JWT) carries the user's ID and an expiry, signed by the server: any server can check the signature without a lookup, which is why APIs and microservices like it. The cost is revocation. Signing out deletes the token from the user's device, but a copy stolen earlier is still correctly signed, and the server accepts it until it expires. The usual design keeps both: short-lived access tokens (minutes) that need no lookup, and a long-lived refresh token or session the server looks up, so signing out, a password change or a stolen-device report takes effect within minutes.

What this simulation simplifies

  • The theft and the sign-out happen on a fixed timer; real thefts go unnoticed for days.
  • Access tokens last seconds here so the expiry shows; real ones last minutes.

How this comes up in interviews

"Sessions or JWTs for a mobile app's API? How do you sign a user out everywhere?"

Then they ask

  1. How long should an access token live?
  2. Where do you store tokens on the client?
  3. How do you handle a stolen refresh token?

A strong answer covers

  • Signed tokens avoid a lookup per request, but cannot be revoked before they expire.
  • Short access tokens (minutes) plus a refresh token or session checked on the server.
  • Sign-out everywhere: delete the user's sessions; copies die at their next refresh.
  • Rotate refresh tokens on use and treat reuse of an old one as theft.
  • Store tokens where scripts cannot read them; bind sessions to devices if the risk is high.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last reviewed: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.