Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
JWT vs session cookies compared: stateful vs stateless auth, how each works, revocation, storage, security pitfalls, and which to choose for your app.
Quick answer: After you log in, the server needs a way to recognise your later requests. With sessions, the server stores your login state and gives the browser a random session ID in a cookie; every request sends the ID, and the server looks it up. With JWTs (JSON Web Tokens), the server gives the client a signed token that contains your identity; each request sends the token, and the server only verifies the signature, with no lookup. Sessions are simple, easy to revoke and the sensible default for a web application. JWTs avoid a shared session store, which helps when many services or outside parties need to verify identity, but they are hard to revoke before they expire.
HTTP is stateless. Each request stands alone, and the server has no built-in memory that the person requesting /account is the one who logged in a moment ago. Something must travel with every request to prove it.
Two terms to keep apart:
Both sessions and JWTs are ways of carrying the result of authentication from one request to the next.
Set-Cookie: sid=8f3a9c...; HttpOnly; Secure; SameSite=Lax; Path=/
The session ID is an opaque reference. It carries no information. All the state lives on the server, which is why this is called stateful authentication.
Strengths
Costs
The OWASP Session Management Cheat Sheet describes how to do this well.
A JWT, defined in RFC 7519, is a string in three parts separated by dots:
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI0MiIsImV4cCI6MTc2MDAwMDAwMH0.Xk3v...
header payload signature
A decoded payload:
{
"sub": "42",
"role": "editor",
"iat": 1759996400,
"exp": 1760000000
}
The flow:
Authorization: Bearer <token> header.Two points are frequently misunderstood:
"role": "editor" to "role": "admin", the signature no longer matches and the token is rejected.Signing can be symmetric (one shared secret, HS256) or asymmetric (sign with a private key, verify with a public key, such as RS256 or ES256). With asymmetric signing, other services can verify tokens without being able to create them. See how public key cryptography works.
Because the server keeps no record, a valid JWT stays valid until it expires. If a user logs out, changes their password, is banned, or has a token stolen, that token still works.
The usual answers:
| Approach | How it works | Trade-off |
|---|---|---|
| Short expiry | Access tokens live for minutes | Limits the window; users need a way to get new tokens |
| Refresh tokens | A long-lived token, stored server-side and revocable, is exchanged for new access tokens | Reintroduces server state |
| Deny list | The server keeps a list of revoked token IDs and checks it on each request | A lookup on every request, which is what JWTs were meant to avoid |
| Token version | Store a version number per user; reject tokens with an older one | Also a lookup |
In other words, as soon as you need reliable revocation, you are keeping state again. The common production pattern is a short-lived access token plus a refresh token that is stored, rotated on each use, and can be revoked.
| Sessions | JWTs | |
|---|---|---|
| Where state lives | On the server | In the token |
| Per-request cost | A store lookup | A signature check |
| Revocation | Immediate | Difficult without extra state |
| Size | A short ID | Hundreds of bytes or more, on every request |
| Scaling | Needs a shared store | Any server with the key can verify |
| Changing a user's permissions | Takes effect at once | Takes effect when the token is reissued |
| Across services and domains | Awkward | Straightforward |
| Typical use | Web applications | APIs, service-to-service calls, federated login |
This question is separate from the choice of sessions or JWTs, and it matters more for security.
| Storage | Readable by page scripts (XSS risk) | Sent automatically (CSRF risk) |
|---|---|---|
HttpOnly cookie | No | Yes; mitigate with SameSite and CSRF tokens |
localStorage / sessionStorage | Yes | No |
| In memory only | Only while the page is open | No |
A token in localStorage can be read by any script running on the page, including one injected through cross-site scripting. An HttpOnly cookie cannot be read by scripts at all. For browser applications, an HttpOnly, Secure, SameSite cookie is generally the safer place, and a JWT can be stored in one. See cookies vs localStorage vs sessions.
none. Always fix the expected algorithm on the server.Use a maintained library and its verification function. Do not write token parsing yourself; see never roll your own crypto.
Choose sessions when:
Choose JWTs when:
A common hybrid: the browser holds an ordinary session cookie with a gateway or back end, and that back end uses short-lived JWTs when calling internal services.
"JWTs scale better" is true in principle and rarely decisive. A session lookup in a fast store is cheap, and most applications that adopt JWTs to avoid server state end up adding it back for revocation.
No. They are a different trade-off. Sessions are easier to secure and revoke; JWTs are easier to verify across systems.
Not by itself. You need short lifetimes, refresh tokens, or a server-side deny list.
Not in a standard signed JWT. The payload is readable by anyone who has the token. The signature only prevents modification.
Preferably in an HttpOnly, Secure, SameSite cookie, so scripts cannot read it.
Sessions keep state on the server and hand the client a meaningless ID. JWTs hand the client a signed statement and keep nothing. The first gives you control; the second gives you independence between services. For most web applications, sessions are the simpler and safer choice, and JWTs earn their place when identity has to cross system boundaries.