Comparison
JWT vs Session Cookies: Which Authentication Should You Use?
TL;DR
| JWT | Session cookie | |
|---|---|---|
| Where state lives | In the token (client-side) | On the server (store/DB) |
| Revocation | Hard — valid until exp (needs a denylist) | Easy — delete the session row |
| Scales statelessly? | Yes — any node can verify | Needs shared/sticky session store |
| Size per request | Larger (whole token sent each time) | Tiny (just a session ID) |
| Best for | APIs, microservices, mobile, SSO | Classic server-rendered web apps |
| Main risk | Can't revoke; XSS token theft | CSRF; server state at scale |
The options in depth
JWT (stateless token)
Self-contained and stateless — great for APIs and microservices.
Good for
- ·Stateless APIs and microservices
- ·Mobile / SPA clients
- ·Single sign-on across services
- ·When you can't share session state between nodes
Avoid when
- ·You need instant, reliable logout / revocation
- ·You store large data in the token (keep it small)
- ·You can't protect against XSS token theft
Session cookie (stateful)
Server holds the state — simple, revocable, battle-tested.
Good for
- ·Classic server-rendered web apps
- ·When reliable logout / revocation matters
- ·Smaller apps where a session store is fine
Avoid when
- ·Fully stateless, massively-scaled APIs
- ·Cross-domain / cross-service auth without shared state
Which one should you pick?
→ I'm building a REST/GraphQL API consumed by a SPA or mobile app — JWT or session?
JWT (usually). Stateless tokens fit APIs and multiple clients well. Keep access tokens short-lived and pair them with refresh tokens, and store them carefully (httpOnly cookie beats localStorage for XSS). If you need hard revocation, add a denylist.
→ I'm building a traditional server-rendered web app — JWT or session?
Session cookies. They're simpler, instantly revocable, and the whole framework ecosystem supports them. Don't reach for JWTs just because they're trendy; sessions are the right default for monolithic web apps.
Common pitfalls
- ⚠Storing JWTs in localStorage: any XSS can steal them. Prefer an httpOnly, Secure, SameSite cookie so JavaScript can't read the token.
- ⚠Assuming JWTs can be 'logged out': they can't, without a denylist. Use short expiries + refresh tokens, and a revocation list for immediate bans.
- ⚠Putting sensitive or large data in a JWT: the payload is only Base64URL-encoded (readable by anyone) and is re-sent on every request. Keep it minimal and non-secret.
- ⚠Forgetting CSRF protection with session cookies: because cookies are sent automatically, you need SameSite and/or CSRF tokens.
Frequently Asked Questions
Are JWTs more secure than sessions?
Not inherently. They solve different problems. Sessions are easier to revoke; JWTs scale statelessly. Both are secure when implemented correctly — the common JWT mistakes (localStorage storage, no revocation, oversized payloads) are what cause trouble.
Can I use JWTs in a cookie?
Yes, and it's often the best of both: store the JWT in an httpOnly, Secure, SameSite cookie. You get the stateless verification of a JWT with the XSS resistance of a cookie — just add CSRF protection as you would for any cookie-based auth.
Related comparisons
OAuth vs JWT
OAuth and JWT are constantly compared, but they aren't alternatives — OAuth 2.0 is an authorization framework (a protocol for getting access), while a JWT is a token format (a way to encode claims). They're often used together. This guide explains what each actually is, how they relate, and when you need which.
HS256 vs RS256 vs ES256
HS256 is symmetric (shared secret), RS256 and ES256 are asymmetric (public/private keypair). This guide explains when each JWT signing algorithm is the right choice.