Video in progress
For authentication, how do you choose between JWT (stateless) and a server-side session, and what do you do if you need to log a user out immediately?
Every service checks a JWT without going to the DB — scaling is easy, but you can't revoke a token before it expires. A session is stored centrally and deleted instantly, but every request needs a trip to the store. In practice a short access token is combined with a refresh token stored on the server.
📖 Answer
Server session. A random ID in the cookie, the state in Redis. Every request is one Redis read. "Log out of all devices" or blocking is just deleting the key, and it takes effect immediately.
JWT. The token holds sub, roles, exp and a signature. The service verifies it with the public key only and goes nowhere. With dozens of microservices this is convenient. The problem is that the token is "valid on its own". An employee is fired, and their token keeps working for another 24 hours.
A practical compromise:
access token (JWT) — 5–15 minutes, can't be revoked
refresh token — 30 days, stored in the DB/Redis, can be revoked
Logging a user out = deleting the refresh token. At most 15 minutes later the access token expires and they can't get a new one.
If you need it immediately (for example, an account was stolen):
- a short-lived denylist by
jtiin Redis — only the revoked tokens, TTL = the token's remaining lifetime; - or a
token_versionon the user — when it changes, old tokens are rejected (this is again a check on every request, but a small and cacheable one).
In Spring Security, the resource server validates the JWT with spring.security.oauth2.resourceserver.jwt.issuer-uri; for an extra check like the denylist you add your own OAuth2TokenValidator.
A good interview answer: "JWT is stateless" is true only for the access token part. A complete system still stores some state; the question is where and how much.
Sign up to mark studied questions and save your progress.