Skip to content

Interview question · Identity, OAuth & Access Control

Server sessions or bearer tokens: how do you choose, and how do you secure each?

'JWTs are stateless and scale' is half an answer. The other half is what happens the day you need to log someone out right now.

mediumEssentialNewPreparation tags: Meta, Google, AmazonUpdated Nov 2026

TL;DR: Choose by revocation needs, who verifies, and the client type. Browser apps talking to their own backend: a server-side session in an HttpOnly, Secure, SameSite cookie, which is simple and instantly revocable. APIs verified by many services or third parties: short-lived bearer tokens (often JWTs) with revocable, rotated refresh tokens, and sender-constrained tokens (DPoP or mTLS) where theft is likely. Whichever you pick, the security work is storage, lifetime, rotation and revocation.

How to approach it

Frame the choice around four questions: who verifies the credential, how fast must revocation take effect, what kind of client holds it, and what happens if it is stolen. Then give the hardening checklist for each option.

A strong answer

FIG · SESSIONS VERSUS TOKENS · CHOOSE BY REVOCATION AND VERIFIER
State on the server, or claims in the token

Server-side session

Opaque cookie

HttpOnly, Secure, SameSite

Session store

lookup per request

Delete row = instant revocation

Outcome, safe: Best for browser apps

One backend verifies

Self-contained token (JWT)

Signed claims

verified by many services

No lookup

valid until expiry

Revocation needs short lifetimes or a deny list

Outcome, safe: Best for distributed APIs

Keep lifetimes in minutes

FactorServer sessionBearer token (JWT)
VerificationLookup in a session storeSignature and claim check, no lookup
RevocationDelete the record: immediateWait for expiry, or add denylist or introspection
Theft impactUsable until the session ends; contents not exposedUsable anywhere until expiry; claims readable
Best forFirst-party browser appsAPIs, cross-domain, many verifying services

Securing sessions. Random IDs of at least 128 bits from a cryptographic generator; regenerate on login and privilege change (prevents fixation); cookies with HttpOnly, Secure, SameSite=Lax or Strict, and the __Host- prefix; idle and absolute timeouts; server-side logout that deletes the record; CSRF protection for state-changing requests even with SameSite.

Securing tokens. Access tokens of minutes, not days; refresh tokens stored server-side or in secure platform storage, rotated on each use with reuse detection, and revocable; validate signature with a fixed algorithm, plus issuer, audience, expiry and scopes; never put secrets in JWT claims (they are readable); keep tokens out of URLs and logs; and bind tokens to the client's key with DPoP (RFC 9449) or mTLS (RFC 8705) for high-risk clients.

Browser single-page apps. Tokens in localStorage are readable by any injected script. A backend-for-frontend pattern keeps tokens on the server and gives the browser only a session cookie, combining the API-friendliness of tokens with the safer browser storage of sessions.

Agents and services. Agents hold tokens on servers, in memory and sometimes in tool configurations, where leakage through logs and traces is common. Short lifetimes, narrow scopes and sender constraints matter more here than for a human's browser.

What interviewers probe next

  • "A user reports their account was compromised. How fast can you cut off access?" With sessions, immediately; with JWTs, revoke refresh tokens now and accept access tokens until expiry, which is why lifetimes should be minutes.
  • "Why does refresh token rotation with reuse detection help?" If an old refresh token is used after rotation, someone copied it; revoke the whole token family.
  • "Is SameSite enough to stop CSRF?" It stops most cases, but not same-site attackers (subdomains) or every browser behaviour, so keep CSRF tokens on sensitive actions.

What if?

What if · interview follow-ups

Hold your answer as the constraints change

A web app is moving from server-side sessions in Redis to self-contained JWTs so it can scale horizontally, and wants to know what it gives up.

  1. Start here

    What do server-side sessions do well?

How it's scored

Weak: "JWTs are better because they are stateless," with no revocation or storage discussion.

Hire: Compares the two on revocation and theft, and lists cookie and token hardening.

Strong Hire: Chooses by context, covers refresh rotation, backend-for-frontend and sender-constrained tokens, and applies it to agents.

Common mistakes

  • Day-long access tokens with no revocation path.
  • Tokens in localStorage on XSS-prone apps.
  • Secrets or personal data in JWT claims.
  • Logging authorization headers.
You just read a free answer. Every topic opens its first 5 answers to anyone. Sign in and the count per topic goes to 10, and we keep track of how far you get in every topic.Google sign-in · free · no payment details
HOW DID IT GO?
0
READING SIGNED OUT

Signing in doubles your free answers, from 5 to 10 per topic, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

No comments yet. Share a correction or a different way to answer it.