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
audandisschecked and a fixed algorithm (nevernoneor 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
HttpOnlycookies overlocalStorage.
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
- What is the N+1 query problem, and how do you find and fix it?Mid-level
- What does Spring Boot's auto-configuration do, and how do you change what it set up?Mid-level
- How does a Spring backend authenticate requests from a mobile app, and how do you make sure users only see their own data?Mid-level
- Your Spring Boot service in Kubernetes keeps restarting with exit code 137. How do you investigate and fix it?Senior