What is the difference between JWT and session cookies?

Dark navy cover card with the title 'What is the difference between JWT and session cookies?' in white and a green accent bar

The difference between JWT and session cookies is where the login state lives. A session cookie holds a random ID, and the server looks that ID up in its own store on every request. A JWT carries the user's claims itself, signed, so the server can check it without a lookup but also can't easily take it back.

The name of the comparison is off, because a JWT often travels inside a cookie too. The real choice is between an opaque session ID and a self-contained token.

A session cookie holds a random session ID that means nothing on its own. The server keeps the real data (user ID, roles, expiry) in memory, a database or a key-value store, and uses the ID to find it.

The ID must be impossible to guess. The session management cheat sheet asks for at least 64 bits of entropy, which means at least 16 hexadecimal characters. For custom IDs it recommends a secure random generator with at least 128 bits.

A typical response header looks like this:

Set-Cookie: sid=9f2c4e1ab07d83f6c5e2a91d04b7f3e8; Path=/; Secure; HttpOnly; SameSite=Lax

Decode that value and you get nothing. Someone who copies the cookie gets an ID that works only until the server deletes the session.

What a JWT holds

A JWT holds the claims themselves: who the user is, when the token expires, and a signature that proves the server issued it. The JWT standard calls it "a compact, URL-safe means of representing claims to be transferred between two parties."

The payload is Base64URL-encoded JSON, not encrypted. A decoded payload might look like this:

{ "sub": "user_4821", "role": "editor", "iat": "<issued-at, Unix seconds>", "exp": "<expiry, Unix seconds>" }

Real tokens hold plain Unix timestamps in iat and exp; they're shown as labels here.

The server checks the signature and the exp claim. Per the standard, exp is the time "on or after which the JWT MUST NOT be accepted for processing." No database call. That's the whole appeal.

Paste any token into a JWT decoder and you'll see the header, payload and expiry in plain text. OnlineViewer's decoder runs in the page and makes no network requests, but it only decodes; it does not verify the signature. Anyone holding a JWT can read it, so keep secrets out of the payload. The post on the three parts of a JWT covers the format in more detail.

JWT vs session cookies: the differences that matter

The JWT vs session cookies choice comes down to lookup cost against control. Sessions cost a store read per request but give you instant logout. JWTs skip the read but stay valid until they expire.

Session cookie (opaque ID) JWT
Where state lives Server-side store Inside the token
Server work per request Look up the ID Verify the signature
Readable by the client No, just a random ID Yes, payload is only encoded
Logout or ban Delete the session, done Wait for exp, or keep a revoke list
Size Small Larger: header, claims, signature
Works across services Needs a shared store Any service with the key can verify
How it's sent Cookie Cookie or Authorization header

Watch the size row. Browsers usually cap a single cookie at about 4KB, according to MDN's cookie guide. A JWT with many roles or permissions can get close to that, and it rides along on every request.

How logout and revocation differ

Logging a user out is a one-line delete with sessions and a design problem with JWTs. With sessions, you remove the record and the ID stops working on the next request.

A signed token stays valid until exp passes, because the server never checks a list. One widely shared essay, JWT should not be your default for sessions, names three ways around this:

  1. Make tokens short-lived and refresh them often.
  2. Keep a list of revoked tokens and check it on every request.
  3. Accept that the server can't force a logout.

The essay says good systems use the first two. Option 2 puts back the per-request lookup that JWTs were supposed to remove. The standard's jti claim gives each token a unique ID you can put on that list. If your users see "token expired" errors after a refresh flow breaks, the post on JWT expired errors walks through the fixes.

Where each one is exposed: XSS and CSRF

Exposure depends on where you store the value, not on whether it's a JWT or a session ID. Any value in a cookie marked HttpOnly is out of reach of page scripts. MDN says such a cookie "can't be accessed by JavaScript."

Put a JWT in an HttpOnly, Secure cookie and it has the same XSS profile as a session ID in the same cookie. Put a JWT in localStorage and any injected script can read it. Cookies of either kind are sent automatically, so they need CSRF protection such as SameSite. The full trade-off is in where to store a JWT.

Two more cookie rules apply to sessions specifically. MDN says to regenerate the session cookie whenever a user logs in, which blocks session fixation. And the cheat sheet warns that high entropy stops guessing but does nothing for a token that has already leaked.

When to use each

Use a session cookie for a normal web app where the browser talks to your own server. Use a JWT when several services need to check identity without sharing a session store.

Pick sessions when:

  • One backend (or a few that share Redis or a database) serves the site.
  • You need instant logout, account bans or role changes that take effect now.
  • Signing keys and algorithm settings are a chore nobody on the team wants.

Pick JWTs when:

  • Separate services or third-party APIs must trust the same login.
  • Requests come from mobile apps or servers that send an Authorization: Bearer header.
  • Tokens can be short-lived, so a few minutes of stale claims is fine.

If you do use JWTs, follow the JWT best current practices, which add rules on algorithms and token confusion that the original standard didn't cover. The step people skip is verifying the signature after decoding; see decode vs verify.

My take: start every browser app with plain server sessions in an HttpOnly cookie, and add JWTs only when a second service has to verify users on its own. The essay above makes the same point about scale: even at millions of active sessions, a distributed key-value store is unlikely to struggle.

Where that advice fails: an API consumed by mobile clients and partner services. There a shared session store becomes a dependency every service must reach, and short-lived JWTs are the cleaner design.

FAQ

Is a JWT more secure than a session cookie?

Not by itself. A session cookie holds a random ID with no meaning, while a JWT holds readable claims. Security depends more on where the token is stored and how fast you can revoke it.

Can a JWT be stored in a cookie?

Yes. A cookie is only a way to send a value with each request. You can put an opaque session ID or a JWT in it, and the HttpOnly and Secure flags work the same for both.

How do you log out a user who has a JWT?

You can't delete a JWT the way you delete a server session. Common options are short expiry times, a list of revoked token IDs checked on each request, or both.

Are JWTs bigger than session IDs?

Yes. A session ID only needs enough random bits to be unguessable, while a JWT carries a header, claims and a signature. Browsers usually cap a cookie at about 4KB.

When should I use JWTs instead of sessions?

Use JWTs when several services must check a user's identity without sharing a session store, such as API-to-API calls. For a single web app with its own server, a session cookie is simpler.

Sources