Backend and APIs · Question 18 of 50
JWT or session cookies?
Stateless trade-offs, and revoking access.
The answer
With sessions, the server keeps the session (in memory, Redis or a database) and the browser holds only a random session id in a cookie. Logging out or banning someone is instant: delete the session.
A JWT is a signed token that carries the claims (user id, roles, expiry). Any server can check it with the key, with no lookup, which suits many services. The cost: it cannot be revoked before it expires without a blocklist, which brings the lookup back. The usual setup is a short-lived access token (5 to 15 minutes) plus a refresh token in an HttpOnly cookie that can be revoked.
The picture
session: browser [sid=abc] ─▶ server ─▶ store.get(abc) → user
a lookup every time, instant logout
JWT: browser [header.payload.signature] ─▶ server checks the signature → user
no lookup, hard to revoke earlyIs a JWT encrypted?
Show the answer
Usually not: it is base64-encoded and signed, so anyone can read what is inside. Never put secrets in it.