DevKits

Comparison

JWT vs Session Cookies: Which Authentication Should You Use?

After a user logs in, your app needs to remember them on the next request. The two classic approaches are server-side sessions (state lives on the server, the browser holds an opaque session cookie) and JWTs (a signed, self-contained token the client sends on every request). The trade-off is essentially statefulness vs revocability: sessions are easy to revoke but need shared server state; JWTs scale statelessly but are hard to invalidate before they expire.

TL;DR

 JWTSession cookie
Where state livesIn the token (client-side)On the server (store/DB)
RevocationHard — valid until exp (needs a denylist)Easy — delete the session row
Scales statelessly?Yes — any node can verifyNeeds shared/sticky session store
Size per requestLarger (whole token sent each time)Tiny (just a session ID)
Best forAPIs, microservices, mobile, SSOClassic server-rendered web apps
Main riskCan't revoke; XSS token theftCSRF; server state at scale

The options in depth

JWT (stateless token)

Self-contained and stateless — great for APIs and microservices.

A JWT carries the user's claims inside a signed token, so any service that trusts the signing key can authenticate the request without a database lookup. That statelessness is ideal for horizontally-scaled APIs, microservices, mobile clients, and single sign-on. The catch is revocation: because the token is valid until it expires, logging a user out or banning them mid-session requires a short expiry plus refresh tokens, or a server-side denylist — which quietly reintroduces the state JWTs were meant to avoid.

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

Try it: JWT Decoder →

Session cookie (stateful)

Server holds the state — simple, revocable, battle-tested.

With sessions, the server creates a record on login and sends the browser an opaque session ID in a cookie. Every request looks up that ID to find the user. Revocation is trivial — delete the session — which makes logout, bans, and 'log out all devices' straightforward. The cost is shared state: at scale you need a session store (Redis) or sticky sessions, and you must defend against CSRF since the cookie is sent automatically.

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

Try it: Cookie Parser →

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