Black Friday: 30% off every plan through November 30. See the prices
SysDesignPrep.com
Study guide

Updated 24 October 2026

JWT vs session cookies

Two ways to keep users logged in: server-side sessions with cookies versus self-contained JSON Web Tokens. How each works, revocation, size, security pitfalls, refresh tokens, storage in browsers and mobile apps, and the hybrid most large systems use.

Reading is half of it. Now try it in an interview.An AI interviewer asks you to design Slack, out loud or typed, and scores the answer for your level. Or watch a sample interview first, free with no sign-up to start.
Start an AI mock

Try it yourself, in a playground:Session management

After a user logs in, every later request must prove who they are. The two common approaches are server-side sessions (a random id in a cookie, looked up on each request) and JSON Web Tokens (a signed token carrying the user's identity, verified without a lookup). Both are fine; each has costs that matter at scale. Interviewers ask about this when a design includes authentication, an API gateway or many services.

Server-side sessions

  1. On login, the server creates a session record (user id, expiry, metadata) in a store and generates a long random session id.
  2. The id is sent as a cookie (HttpOnly, Secure, SameSite).
  3. Each request sends the cookie; the server looks up the session.

Properties:

  • Instant revocation: delete the session record (logout, password change, suspicious activity).
  • Small cookie: just an opaque id.
  • A lookup per request: needs a fast, shared, replicated store (Redis) so any server can validate. See stateful vs stateless services.
  • Natural fit for browser apps on one domain.

JSON Web Tokens

A JWT has three base64url parts: header, payload (claims such as user id, roles, expiry, issuer, audience) and a signature. Any service with the verification key can check it locally.

  • No lookup: verification is a signature check, so services and gateways validate tokens without calling an auth service. Good for many services and multiple regions.
  • Hard to revoke: a valid token works until it expires. Mitigate with short lifetimes (5 to 15 minutes) plus refresh tokens, or a deny list of revoked token ids (which reintroduces a lookup, but only for a small set).
  • Larger: hundreds of bytes to kilobytes on every request; keep claims minimal.
  • Stale claims: roles in the token do not change until it is reissued.

Use asymmetric signatures (RS256, ES256, EdDSA) so services verify with a public key and only the auth service can sign; publish keys via a JWKS endpoint and rotate them. See encryption and key management.

Common JWT mistakes

  • Accepting alg: none or letting the token choose the algorithm; pin the expected algorithm.
  • Not validating expiry, issuer and audience.
  • Putting secrets or personal data in the payload; it is encoded, not encrypted. See hashing and encoding.
  • Long-lived access tokens with no revocation path.
  • Storing tokens in localStorage in browsers, where any injected script can read them.

Refresh tokens

The usual pattern pairs a short-lived access token with a long-lived refresh token:

  • The access token (JWT) is sent with API requests and verified locally.
  • When it expires, the client exchanges the refresh token at the auth service for a new access token.
  • Refresh tokens are stored server-side (or tracked) so they can be revoked, and rotated on each use; reuse of an old refresh token signals theft and revokes the whole family.

This gives local verification on the hot path and revocation within minutes. See OAuth 2.0 and OpenID Connect.

Where to store tokens

  • Browsers: prefer HttpOnly, Secure, SameSite cookies, which scripts cannot read, with CSRF protection for state-changing requests. Many teams keep a session cookie for the browser and convert it to a short-lived token at the gateway.
  • Mobile apps: the platform's secure storage (Keychain, Keystore). See mobile system design.
  • Service to service: short-lived tokens from the platform, or mTLS identities. See service discovery and service mesh.

Comparison

Session cookieJWT
Validationlookup in session storesignature check, local
Revocationimmediateat expiry, or via deny list
Size per requestsmalllarger
Cross-service and cross-regionneeds shared storeeasy
Claims freshnessalways currentstale until reissued
Best forbrowser apps, high-security revocation needsAPIs, microservices, mobile, federated systems

The common hybrid

Large systems often combine them: the browser holds an opaque session cookie; the API gateway validates the session (with caching) and issues a short-lived internal JWT to downstream services, which verify it locally. Users get instant logout, services get fast local checks. See API gateways.

In the interview

"Clients authenticate once and receive a short-lived signed access token plus a rotating refresh token; the gateway and services verify tokens locally with the auth service's public keys, so there is no auth call per request; revocation happens within the token lifetime, or immediately via a small deny list for compromised sessions." For browser-first products, mention HttpOnly cookies and CSRF protection. See security and auth.

Checklist

  • Sessions for immediate revocation; JWTs for local verification across services.
  • Short access token lifetimes plus rotating refresh tokens.
  • Asymmetric signing, key rotation, strict validation of algorithm, expiry, issuer and audience.
  • No secrets or personal data in token payloads.
  • HttpOnly cookies in browsers, secure storage on mobile.
  • Gateway hybrid: session at the edge, short internal tokens inside.

Questions people ask

What is the difference between JWT and session cookies?

A session cookie holds a random id that the server looks up in a session store on every request, while a JWT is a signed token carrying the user's identity that any service with the key verifies locally, without a lookup. Sessions give instant revocation and a small cookie but need a fast, shared store such as Redis. JWTs work easily across services and regions, but they are larger, hard to revoke, and carry claims that stay stale until reissued.

What is a JSON Web Token (JWT)?

A JSON Web Token (JWT) is a signed token with three base64url parts: a header, a payload of claims such as user id, roles, expiry, issuer and audience, and a signature. Any service with the verification key can check it locally, so gateways and services validate tokens without calling an auth service. The payload is encoded, not encrypted, so it must never hold secrets or personal data.

What is the difference between stateful sessions and stateless tokens?

A stateful session keeps a session record on the server and looks it up on each request, while a stateless token such as a JWT carries its own claims and is verified with a signature check. Stateful sessions allow immediate revocation by deleting the record, but every server needs a fast, shared, replicated store. Stateless tokens remove the lookup, which suits many services and multiple regions, but a valid token keeps working until it expires.

How do you revoke a JWT?

A JWT is hard to revoke, because a valid token works until it expires. The usual mitigation is a short lifetime of 5 to 15 minutes plus a refresh token that can be revoked, so revocation takes effect within minutes. For compromised sessions, a deny list of revoked token ids gives immediate revocation; it reintroduces a lookup, but only for a small set of tokens.

How do refresh tokens work?

A short-lived access token, a JWT, is sent with API requests and verified locally; when it expires, the client exchanges a long-lived refresh token at the auth service for a new access token. Refresh tokens are stored or tracked server-side so they can be revoked, and they are rotated on each use. Reuse of an old refresh token signals theft and revokes the whole token family.

In browsers, store tokens in HttpOnly, Secure, SameSite cookies rather than localStorage, because scripts cannot read those cookies, while any injected script can read localStorage. Cookies need CSRF protection for state-changing requests. Many teams keep a session cookie in the browser and convert it to a short-lived token at the API gateway, and mobile apps use the platform's secure storage, such as Keychain or Keystore.