Part 4 of 10 · Security

Sessions or JWTs: which should you use to keep users signed in?

Two good ideas for different jobs. For a web app with its own backend, server-side sessions are simpler and safer: a random cookie, instant revocation, nothing sensitive in the browser. JWTs earn their place between services and across organisations, where checking a signature beats a database…

Popcorn spilling from a striped box next to red admit-one tickets

Short answer: the verdict, which the evidence below will earn:

  • First-party web app (your frontend, your backend, one domain): server-side sessions. The cookie is a random ID; the server looks it up. Sign-out and “sign out everywhere” work instantly.
  • APIs called by other services or third parties: short-lived JWTs (access tokens), verified locally by each service without a shared database.
  • Mobile apps: either works; a session token or an OAuth refresh/access pair, stored in the Keychain/Keystore.

The real culprit in most stories is putting a long-lived JWT in localStorage for a web app: it can’t be revoked, and any XSS can steal it.

Picture the case. A user’s laptop is stolen, they click “sign out everywhere”, and the thief keeps browsing for days. How? This debate gets heated, which is a shame, because both ideas make sense once you see what each is for. One trusts a lookup; the other trusts mathematics. Let’s examine how each one works before deciding who’s responsible.

Case file

The case A user clicks “sign out everywhere”, and the thief keeps browsing for days
Suspects Sessions, JWTs, “stateless must be faster”
Culprit A long-lived JWT in localStorage: it can’t be revoked, and any XSS can steal it
The fix Server-side sessions for first-party web apps, short-lived JWTs between services
Evidence The blogging platform I run: hashed 256-bit session tokens in __Host- cookies

How they work #

Sequence diagram: random token, cookie, tokenSequence diagram: random token, cookie, token
Diagram source
sequenceDiagram
  participant B as Browser
  participant A as App server
  participant S as Session store
  Note over B,S: Server-side session
  B->>A: POST /login
  A->>S: store hash(random token) → user, expiry
  A-->>B: Set-Cookie: __Host-session=<random>#59; HttpOnly#59; Secure#59; SameSite=Lax
  B->>A: GET /dashboard (cookie)
  A->>S: look up hash(token)
  S-->>A: user 42, valid
  A-->>B: page

The JWT’s trick is that the token carries its own proof, so any service holding the public key can check it without asking anyone. That independence is genuinely elegant, and it’s also where our mystery hides.

Sequence diagram: sub: 42, exp: +10 min, aud: api, no lookupSequence diagram: sub: 42, exp: +10 min, aud: api, no lookup
Diagram source
sequenceDiagram
  participant C as Client
  participant AS as Auth server
  participant API as Any API
  Note over C,API: Stateless JWT
  C->>AS: sign in
  AS-->>C: JWT {sub: 42, exp: +10 min, aud: api} signed with private key
  C->>API: Authorization: Bearer <JWT>
  API->>API: verify signature with public key, check exp and aud (no lookup)
  API-->>C: data

The evidence, side by side #

Server-side session JWT access token
What the client holds A random ID (256 bits) A signed, readable set of claims
Validation A lookup (database or Redis), usually under a millisecond A signature check, no network call
Revocation Delete the row: instant Wait for expiry, or keep a deny list (which is a lookup again)
Size ~45 bytes Hundreds of bytes to a few KB, on every request
Data freshness Always current (role changed? next request sees it) Claims are as old as the token
Works across services and organisations Only if they share the store Yes: anyone with the public key can verify
Main risks Session fixation, CSRF (mitigated with SameSite and tokens) Long expiry, alg confusion, storage in localStorage, no revocation

Follow the “Revocation” row and the stolen-laptop case solves itself. The session is a row you can delete. The JWT is a promise already handed out, valid until it expires, unless you add a deny list, which is a lookup again.

Case 1: a web app with its own backend #

What I’d use: sessions. The blogging platform I run does this:

  • 256-bit random tokens; only their SHA-256 hash is stored, so a database leak doesn’t hand out valid cookies.
  • A __Host- cookie: Secure, HttpOnly, SameSite=Lax, and bound to the exact host so subdomains can’t set or read it.
  • Rotated at login (prevents fixation) and deleted server-side at logout.
  • Idle and absolute expiry (7 days idle, 30 days total), and a password reset ends every session.
  • CSRF protection on every POST (SameSite alone isn’t enough for older browsers and same-site subdomains; see the CSRF and CORS post).

The tempting lead is that “stateless” must be faster. A session lookup by primary key costs well under a millisecond. “Stateless” saves that and costs you revocation. A random number and a table row are hard to beat.

Case 2: microservices behind a gateway #

What I’d use: sessions at the edge, JWTs inside. This is the satisfying turn, where the two stop competing and start cooperating.

  • The browser has a session cookie with the gateway or a backend-for-frontend.
  • The gateway turns it into a short-lived internal JWT (a minute or two) carrying the user ID, tenant and roles, and forwards it to services.
  • Each service verifies the JWT locally. Revocation is handled at the edge: end the session and no new JWTs are minted.

Case 3: a public API for partners #

What I’d use: OAuth 2.0 access tokens (often JWTs) obtained with client credentials or the authorization-code flow (OAuth flows post), short-lived, scoped and audience-restricted.

If you use JWTs, close these doors #

  1. Pin the algorithm you accept (e.g. EdDSA or ES256); reject none and unexpected ones.
  2. Short expiry (minutes), plus refresh tokens with rotation (access and refresh tokens post).
  3. Check aud and iss, as well as the signature.
  4. No secrets or sensitive personal data in claims: they’re readable by anyone holding the token.
  5. Asymmetric keys (services need only the public key), published as a JWKS and rotated.

Remember this

  • A session is a row you can delete; a JWT is valid until it expires.
  • Sessions at the edge, short-lived JWTs inside.
  • Never put long-lived JWTs in browser storage.

Case closed: what I’d do by default #

  1. Sessions for first-party web apps, with hashed tokens, __Host- cookies, rotation and expiry.
  2. Short-lived JWTs between services, minted at the edge.
  3. OAuth access tokens for third parties.
  4. Never long-lived JWTs in browser storage.

Once you stop treating it as a contest, the answer is rather satisfying: sessions where you want control, signed tokens where you want independence, and each one doing the thing it’s best at.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.