Skip to content
Commitwise
Menu

JWT access tokens or server-side sessions: how do you choose?

BackendMid-level

In shortServer-side sessions are simple and instantly revocable; JWTs are verified without a lookup, which helps across many services but makes revocation hard. A common design is short-lived JWT access tokens plus refresh tokens stored and revocable on the server, or plain sessions for a single web app.

Model answer

Server-side sessions: the server stores session state (in a database or Redis) and gives the client an opaque id, usually in an HttpOnly, Secure, SameSite cookie. Every request looks it up. Revocation is instant (delete the row), sessions can carry server-side data, and the cookie can’t be read by JavaScript. The cost is a lookup per request and shared session storage across instances.

JWTs: the server signs claims (sub, exp, aud, roles); any service with the public key can verify them without a lookup, which suits many services or third-party APIs. The cost: a JWT is valid until it expires, so revocation needs a denylist (a lookup again) or short lifetimes. Payloads are only encoded, not encrypted.

A balanced design:

  • Access tokens: JWTs that expire in minutes, verified locally, with aud and iss checked and a fixed algorithm (never none or accepting the header’s choice).
  • Refresh tokens: opaque, stored hashed on the server, rotated on every use (reuse of an old one revokes the family, which detects theft), revocable on logout or password change.
  • Storage: mobile apps keep refresh tokens in the Keychain or Keystore; web apps prefer HttpOnly cookies over localStorage.

For a single web app, plain sessions are often the simplest secure choice.

What they're looking for

How each works, revocation and expiry trade-offs, where tokens are stored on web and mobile, refresh token rotation, and security basics (signature algorithms, audience, cookie flags).

Common mistakes

  • Long-lived JWTs with no way to revoke them.
  • Putting secrets or personal data in JWT payloads.
  • Storing tokens in localStorage on the web without considering XSS.

More backend questions

Esc

Type to search the whole site.