A belief that shows up in tutorials and design discussions alike: “Sessions are old-fashioned and don’t scale. Use JWTs, they’re stateless.” Parts of that sentence are true. Parts are quietly wrong. Let us take it apart.
First, what each thing is
A classic server session works like a cloakroom ticket. After you log in, the server stores your details (user ID, roles, cart) in its own memory or a database and gives your browser a random, meaningless ID, usually in a cookie. On each request the server looks the ID up. The ticket carries no information; the cloakroom holds everything.
A JSON Web Token (JWT, specified in RFC 7519) is closer to a signed letter. It has three parts separated by dots: a header, a payload of claims (who the user is, which roles, when the token expires), and a signature. The server signs it with a secret key, or with a private key in the asymmetric case. Any service holding the matching key can verify the signature and trust the claims without looking anything up. That is what “stateless” means here: the proof travels with the request.
Myth one: the payload is secret
A signed JWT is not encrypted. The payload is only encoded, and anyone who gets the token can read it. The signature prevents tampering; it does not provide confidentiality. So never put a password, an Aadhaar number or anything sensitive in the claims. (An encrypted variant exists, called JWE, but ordinary JWTs are signed only.)
Myth two: logout works the same way
This is the big one. With a session, logging out deletes the server-side record and the ticket is instantly worthless. Banning a user, or cutting their access after a stolen laptop is reported, works the same way: delete the record.
With a pure JWT there is nothing to delete. The token is valid until its expiry time, wherever it is, because every service can verify it independently. If an employee is dismissed at 11:00 and their token expires at 11:30, they keep access for thirty more minutes unless you add something. The statelessness that made verification cheap is the same thing that makes revocation hard.
The usual remedies each give back some of what you gained:
- Keep access tokens short-lived, around five to fifteen minutes, and pair them with a longer-lived refresh token. Refresh tokens are stored server-side, so they can be revoked. The window of exposure shrinks to the access token’s lifetime.
- Keep a denylist of revoked token IDs and check it on every request. This works, but now you have a lookup per request, which is state again.
- Embed a version number in the token and compare it with the user’s current version. Same lookup, same trade.
Myth three: sessions don’t scale
Sessions scale fine. The old problem was that sessions lived in one server’s memory, so a load balancer had to keep sending a user to the same machine. Moving the session store into a shared system such as Redis removes that limit, and a fast key lookup per request is cheap compared with most of what your application does anyway. Plenty of very large systems run on server-side sessions.
Where JWTs genuinely help is when many independent services, possibly run by different teams or companies, must trust a login performed by one identity provider, and you do not want each of them calling it on every request. That is the setting OAuth 2.0 and OpenID Connect were built for, and ID tokens in OpenID Connect are JWTs. A mobile app talking to several microservices is another reasonable fit.
Where tokens live in a browser
Teams often store JWTs in local storage because it is easy for JavaScript to read. That is also exactly why it is risky: any cross-site scripting bug on your page can read it and send it away. A cookie marked HttpOnly cannot be read by scripts, though cookies bring their own concern, cross-site request forgery, which the SameSite attribute and anti-forgery tokens address. There is no storage place without trade-offs; the point is to choose knowingly.
Mistakes that have caused real vulnerabilities
Some JWT libraries in the past accepted tokens whose header said the algorithm was “none”, meaning unsigned, or let attackers switch between asymmetric and symmetric algorithms. Current libraries guard against this, but the lesson stands: your server must decide which algorithm it accepts, never take it from the token. Always check the expiry, the issuer and the audience claims too. And keep signing keys out of the codebase.
So which one?
For a single web application with its own backend, ordinary sessions in a shared store are usually simpler and safer, with instant logout for free. Reach for JWTs when you have a real need for verification without a central lookup, across services or organisations, and accept short lifetimes and the revocation work that comes with them. “Stateless” is a property you pay for, and it is worth paying for only when you need it.
Leave a Reply