All articles
165 articles · updated weekly See our Tools
All articles
All

JWT vs sessions: which auth model should you use

Sessions give total control and instant revocation; JWT scales without a central store, but revocation is the weak spot. Compare the trade-offs and pick right.

JWT vs sessions: which auth model should you use
COVER · All

Every time an app needs to remember who is logged in, someone reopens the debate: "JWT or sessions?". It turns into religion fast — some treat sessions as a dinosaur, others treat JWT as a silver bullet. Both are wrong. They are different models with concrete trade-offs, and the wrong call costs you: you either reinvent sessions in a worse way, or you build a system that can't log anyone out. This piece compares the two on what actually matters — revocation, scaling, where to store the token, and security — and ends with a direct recommendation.

The focus here is authentication for web apps and APIs. I won't cover OAuth, OIDC, or third-party flows; that's a separate topic.

The session model: state on the server

Sessions are the classic model. On login, the server generates a random session id, stores the associated data (who the user is, permissions, whatever you need) in a store — memory, Redis, a database — and hands back only the id to the client, usually inside a cookie. On every following request, the browser sends the cookie automatically, the server takes the id, looks the data up in the store, and knows who's talking.

The strength is total control. Want to log a user out right now? Delete the row in the store. Want to kill all of someone's sessions after a password change? Delete every entry for that user. Want to change permissions and have them take effect on the next request? Just edit the data on the server. None of this depends on the client cooperating. The cookie carries an opaque id that means nothing on its own — if it leaks, you revoke it.

The price shows up when you scale horizontally. If the session lives in one process's memory, the user's requests have to keep hitting the same server (sticky sessions) or the login is lost when they switch instances. The standard fix is a shared store — Redis sitting in front of every server. It works very well, it's cheap and predictable, but it's one more piece of infrastructure to maintain, monitor, and that becomes a single point of failure if you're careless.

The JWT model: genuinely stateless

JWT (JSON Web Token) flips the logic. Instead of keeping state on the server, it puts the data — the claims — inside the token itself, which is signed. A JWT has three parts separated by dots: header, payload, and signature.

header.payload.signature
eyJhbGciOiJI...  .  eyJzdWIiOiIxMjM0...  .  SflKxwRJSMeKKF2QT4...

The payload holds things like sub (the user), exp (when it expires), and anything else you want. The signature is generated with a secret key (or a key pair) and guarantees nobody altered the content. On the next request the server only needs to verify the signature with the key — it doesn't go anywhere to fetch anything. That's the win: no central store, no lookup. Any server that holds the key can validate the token on its own.

This shines in two scenarios. Stateless APIs, where you don't want to carry session infrastructure, and service-to-service communication, where one microservice passes the token along and each service validates it independently. It scales without the headache of a shared store.

Worth noting: the payload is just base64, not encryption — anyone can read the content. You can decode a token to inspect its claims without sending anything to any server, precisely because the data sits right there in plain text, only signed. Never put secrets inside a JWT.

Revocation: JWT's weak spot

This is JWT's honest problem, and the one most people ignore in the rush to adopt it. Because the server validates the token by its signature alone, without consulting anything, it has no way of knowing that a given token is "no longer valid". Changed your password? The old token stays valid until exp. Fired the employee? Their token works until it expires. Detected a leak? You can't invalidate it early.

The usual way out is to keep a blocklist — a list of revoked tokens the server checks on every request. It works, but notice the irony: you just added a central-store lookup to every request. In other words, you killed the "stateless" property that was the whole argument for JWT. You rebuilt the session, only with more bytes on the wire and more complexity.

The real mitigation is a short exp. An access token that lives 5 to 15 minutes limits the blast radius: even without revoking, the damage expires on its own quickly. But short tokens demand frequent renewal, and that's where refresh tokens come in.

Refresh tokens, size, and where to store them

The mature pattern with JWT is two tokens. A short access token (minutes) that goes on every request, and a long refresh token (days) used only to obtain a new access token when it expires. The refresh token you store on the server — yes, with state — and you can revoke it. Notice you got a store back; the difference is it's only consulted on renewal, not on every request. It's a reasonable middle ground.

Where to store it on the client is where most people slip up. The right answer is an httpOnly, Secure, SameSite cookie. httpOnly stops JavaScript from reading the token, which cuts off theft via XSS. The classic mistake is dumping the JWT in localStorage "because it's convenient": any injected script reads the token and it leaks. An httpOnly cookie applies equally to session ids and JWTs — it's not an advantage of either, it's basic hygiene.

There's also size. A session id is a handful of bytes. A JWT with several claims can exceed 1 KB, and it travels on every request. At high volume that adds bandwidth and latency. It's not decisive in most cases, but it's real.

The direct recommendation

For a monolithic web app, start with sessions and an httpOnly cookie. It's simpler, it's more secure than most people assume, and it gives you revocation for free. You don't need a refresh token, you don't need a blocklist, you don't need to nervously pick an expiration time. Adding Redis when you scale is trivial and has been solved for decades.

Don't start with JWT because it's fashionable. JWT is the right call when you have genuinely stateless APIs, communication across multiple services, or a scenario where the central session store is the bottleneck you're trying to eliminate. In those cases it's excellent. Outside them, you're usually paying for the complexity of stateless without reaping the benefit — and the market's worst piece of nonsense is putting a JWT in localStorage in an ordinary web app and, in practice, reimplementing sessions in a worse, less secure way.

If you want to understand the mechanics of signing, claims, and verification from the inside before deciding, see how a JWT works under the hood.

Frequently asked questions

Is JWT more secure than sessions?

No, not by nature. Security comes from where you store the token (an httpOnly cookie in both), from using HTTPS, and from good expiration and revocation management. Sessions even have an edge on one point: revocation is instant. A misused JWT, in localStorage with a long exp, is less secure than a well-built session.

Can I revoke a JWT before it expires?

Not without giving up statelessness. The only way is to keep a blocklist on the server and check it on every request, which recreates the central-store dependency JWT promised to avoid. The common practice is a short exp (5 to 15 minutes) plus a refresh token, accepting a small window where the token stays valid.

Where should I store the token in the browser?

In a cookie with the httpOnly, Secure, and SameSite flags. This applies to both session ids and JWTs. localStorage is a bad idea because any JavaScript on the page — including one injected via XSS — can read the content and steal the token.

When does JWT really pay off?

In stateless APIs, in microservice architectures where the token is passed between services, and when the central session store has become a scaling bottleneck. In those contexts the gain of avoiding a per-request lookup is real. For a monolithic web app, sessions are usually the simpler and safer choice.

What to take away

Sessions and JWT don't compete for the same spot. Sessions are state on the server: total control, instant revocation, the cost of a shared store when you scale. JWT is stateless: it scales without a central store and is great between services, at the price of hard revocation and bigger tokens. Choose by the problem, not the fashion — a web monolith calls for sessions, a distributed API calls for JWT — and in either case, store the token in an httpOnly cookie.

RD
Author
Rafael Duarte
Backend developer with a background in fintech and B2B SaaS — worked on teams that scaled APIs from zero to millions of requests. Carries enough production scars to hold strong opinions on tools, patterns and architecture decisions. Not an academic: he read the UUID RFC when he had to choose between v4 and v7 for a write-heavy table.
View profile