JWT in Redis? Understanding Stateless Auth Trade-offs for Interviews
This article explains JWT structure, its stateless design, four scenarios where pure JWT fails (account theft, user ban, password change, token expiry), solutions using Redis blacklist or whitelist, double-token refresh, Redis reliability, when pure JWT fits, and how to articulate trade-offs in interviews.
JWT Structure and Stateless Verification
JWT (JSON Web Token) is a string divided by dots into three parts: Header (algorithm, e.g., HS256), Payload (user ID, roles, expiry – Base64 encoded, not encrypted), and Signature (Header + Payload + server secret). The verification flow: user logs in, server signs token, client stores it (usually localStorage), each request sends Authorization: Bearer <token>, server verifies signature and extracts user info from Payload without querying a database or Redis. This is the "stateless" property.
Four Fatal Scenarios with Pure JWT
Scenario 1: Account theft, cannot kick out immediately. Token valid for 2 hours, 1h45m remaining. Pure JWT cannot revoke before expiry.
Scenario 2: User ban ineffective. Banned account still works with existing token.
Scenario 3: Password change does not log out other devices. Pure JWT cannot invalidate tokens on password change.
Scenario 4: Token expiry swallows form data. User fills form for 20+ minutes, token expires on submit, content lost.
These four scenarios cover almost every real business system.
Why Put Token in Redis: Blacklist vs Whitelist
Solution: blacklist – add revoked tokens to Redis with TTL matching remaining validity. On each request, check blacklist first. Redis query <1ms, distributed. Code example shows logout adding token to blacklist and validation checking it.
// Logout: add token to blacklist
public void logout(String token) {
long expiration = jwtUtil.getExpiration(token);
long ttl = expiration - System.currentTimeMillis();
if (ttl > 0) {
redisTemplate.opsForValue().set(
"blacklist:" + token, "1", ttl, TimeUnit.MILLISECONDS
);
}
}
// Validate token: check blacklist first
public boolean isTokenValid(String token) {
if (redisTemplate.hasKey("blacklist:" + token)) {
return false;
}
return jwtUtil.verify(token);
}Redis only stores "revoked" tokens; normal requests don't write, reads are fast.
Alternative: whitelist – store every issued token in Redis, validate by existence. Equivalent to traditional session using JWT format as session ID.
Comparison of Three Schemes
Pure JWT : stores nothing; 0 Redis queries; cannot actively revoke; fully stateless.
JWT + Blacklist : stores revoked tokens; 1 Redis query per request; can actively revoke; not fully stateless.
Token in Redis (Whitelist) : stores all tokens; 1 Redis query per request; can actively revoke; not stateless.
Blacklist is lighter (writes only on revocation); whitelist offers stronger control (device limits, real-time online status) but every request depends on Redis.
Double-Token Refresh
AccessToken : short expiry (30 min), used for actual auth.
RefreshToken : long expiry (7 days), stored in Redis, used only to obtain new AccessToken.
When AccessToken expires, frontend uses RefreshToken to get a new one seamlessly. RefreshToken deleted on password change, forcing re-login on next operation.
Redis Reliability
Redis high availability proven over 10+ years: Sentinel (auto failover) and Cluster (sharding, horizontal scaling). Single query <1ms, single-node QPS 100k+. "Cannot use Redis due to single point of failure" is invalid.
What Interviewers Are Really Laughing At
Misusing JWT: Using JWT format but validating every request by querying Redis for token existence. This wastes JWT's stateless benefit; a random string as session ID would be simpler.
Poor articulation: You have legitimate revocation needs and chose JWT+Redis, but only said "put in Redis" without explaining why. Interviewer assumes you misunderstand stateless design.
First case requires design change; second requires better explanation.
When Pure JWT Is Actually Suitable
Cross-service calls: Gateway verifies once, passes user info downstream. Order/payment services read userId from JWT without Redis. JWT acts as identity propagation, not session management.
One-time credentials: Email verification, password reset, temporary share links. Sign once, use once, no revocation needed.
Low-security internal tools: Few users, no need to kick anyone. Pure JWT saves a dependency.
How to Answer in Interviews
Don't start with "put in Redis" or "JWT is stateless". First clarify business requirements: need active revocation? password change logs out other devices? limit concurrent devices? Then propose solution: if revocation needed, choose JWT+Redis blacklist, explain pure JWT cannot revoke; if not, pure JWT with signature verification and key management. Interviewers care about whether you know each scheme's boundaries, not which you picked. "Token in Redis" is fine; the problem is inability to explain why.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
