BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutSync devicesRSSllms.txtGitHubX
Posts/authentication
AI-generated illustration. A ticket only means something to the counter that issued it. A wristband carries its own proof.
Intermediate4 min read

Part 2 of Engineering

Sessions vs JWT: Where Does Your Login Actually Live?

Sessions vs JWT explained with a coat check and a wristband: who stores the login, why JWTs are hard to revoke, and which one to pick for a web app.

Mohsen Fallahnejad

Mohsen Fallahnejad

Nov 27, 2026Discuss

You log in once, and the next fifty requests still know who you are. HTTP forgets everything between requests, so something has to carry your login along.

There are two classic answers: a session or a JWT. The difference is one question: who keeps the proof?

The mental model#

A session is a coat-check ticket. A JWT is a signed festival wristband.

With a coat check, the server keeps your coat (your login data) and gives you a numbered ticket. The ticket means nothing on its own. With a wristband, you carry the proof yourself. The guard at the door doesn't look anything up; they check the seal.

How it works#

Sessions. You log in, and the server saves a record like { id: "a8f3", userId: 7 } in a database or Redis. It sends back only the ID, usually in a cookie. On every request, the server looks up a8f3 to find out who you are.

JWT (JSON Web Token). You log in, and the server writes your details into a token, such as { userId: 7, role: "admin", exp: ... }, and signs it with a secret key. The signature is the seal: change one letter and the check fails. On every request, the server checks the signature and reads the data straight from the token. No lookup.

Session (ticket)JWT (wristband)
Where the login data livesServerIn the token you carry
Each requestLook up the IDCheck the signature
Log out / ban a userDelete the record. Done.Hard: the token is valid until it expires
Scaling many serversAll need the same storeAny server with the key can check it

In practice#

The big trade-off is revoking. Delete a session, and that user is out on the next request. A JWT has no record to delete, so a stolen or outdated token keeps working until its exp time. If you just removed someone's admin role, their old token still says "admin".

A guard with scissors stands by torn tickets while a person with a gold wristband walks past himA guard with scissors stands by torn tickets while a person with a gold wristband walks past him The guard can tear up any ticket. He can't cut a wristband he never kept a record of.

That's why most JWT setups use short-lived access tokens (minutes) plus a refresh token stored on the server. And once you store refresh tokens to make logout work, you've brought back a server-side record: a session by another name.

So, which one?

  • A normal web app with one backend: sessions in an httpOnly cookie. Simple, easy to revoke.
  • Many services or third parties that must check identity without calling you: JWTs, with short expiry.

Two myths to drop: a JWT is signed, not encrypted, so anyone can read what's inside. Never put secrets in it. And JWT vs session is not the same choice as cookie vs localStorage: a JWT can live in a cookie too, and where it lives decides your XSS and CSRF risks.

Say it in an interview: "With sessions, the server stores the login and the client holds only an ID, so every request needs a lookup, but logout and bans are instant. A JWT is a signed token that carries the user data itself, so any server can verify it without a lookup, but you can't easily revoke it before it expires. For a typical web app I use sessions in an httpOnly cookie; I reach for short-lived JWTs when several services need to verify identity on their own."

Cheat sheet#

  • Session = coat-check ticket: server keeps the data, you hold an ID.
  • JWT = signed wristband: you carry the data, the server checks the seal.
  • Sessions are easy to revoke; JWTs live until they expire.
  • JWTs are signed, not encrypted: readable by anyone.
  • Default for web apps: session + httpOnly cookie.

TL;DR: sessions trust a lookup, JWTs trust a signature. Pick the one whose weakness you can live with.

Advanced · optional

Going deeper#

Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.

What a JWT looks like#

Three Base64URL parts joined by dots: header.payload.signature. The header names the algorithm, the payload holds the claims (sub, exp, your own fields), and the signature covers both. Paste one into a decoder and you can read the payload without any key. That's the "signed, not encrypted" point in action.

Ways to revoke a JWT anyway#

  • Keep access tokens short (5 to 15 minutes is common) so a stolen one dies soon.
  • Keep a blocklist of revoked token IDs (jti). That's a lookup on every request again.
  • Store a per-user "token version" and bump it on logout or password change.

Each of these trades some of the "no lookup" benefit for control.

Signing algorithms#

HS256 uses one shared secret to sign and to check, so every checker can also create tokens. RS256 and ES256 sign with a private key and check with a public key, which suits many services that only need to verify. Always pin the expected algorithm on the server; never trust the alg field in the token alone.

Filed under#authentication#security#jwt#system-design#interview

Series

Engineering

Ideas behind reliable systems: idempotency, flags, indexes, and the vocabulary seniors expect in design interviews.

  1. 1Idempotency: Why a Retried Payment Can't Charge Twice
  2. 2Sessions vs JWT: Where Does Your Login Actually Live? · you are here
  3. 3Deploy is not release · coming soon
  4. 4One trip to the shop with a list · coming soon

New parts land here first — subscribe below to get them in your inbox.

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← Olderasync vs defer: Why Script Order Breaks Your PageNewer →any vs unknown vs never in TypeScript, Simply

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

Idempotency: Why a Retried Payment Can't Charge Twice

Idempotency means doing something twice has the same effect as doing it once. Learn how idempotency keys make retries safe, and why PUT is idempotent but POST isn't.

Nov 25, 20265 min read

XSS vs CSRF: Two Browser Attacks With Two Different Fixes

XSS runs the attacker's code on your page. CSRF makes the browser send a request you didn't mean to. Each cookie flag stops only one of them.

Nov 14, 20267 min read

CORS Explained Simply — Why Your Fetch Works in Postman but Fails in the Browser

Cross-Origin Resource Sharing in plain language: what blocks your request, what preflight means, and how to fix the errors you'll see in every frontend job.

Jul 8, 20266 min read

Get new posts in your inbox

One email when a new post is out: short, diagram-first explanations of JavaScript, React and Next.js. No spam, unsubscribe any time.

4 min left
Discuss👍 React
👍