The JWT Interview Answer Most Junior Devs Get Wrong

The written version
Sessions break when you scale out. JWT exists so that any server can verify a user without shared state. That is the answer interviewers are listening for.
1. The Traditional Session Problem
In traditional stateful sessions, when a user logs in, the backend creates a session ID in server memory or a centralized Redis store and sets a session cookie.
This works wonderfully for a single server instance. But what happens when you introduce a load balancer with 5 independent backend instances? If request #1 goes to Server A, and request #2 lands on Server B, Server B has no clue who you are unless it queries a shared Redis database on every single request.
At massive scale, that centralized database becomes the single point of failure and your biggest latency bottleneck.
2. How JWT Solves Stateless Verification
A JSON Web Token (JWT) flips this model upside down. Instead of storing the user session in a database, the server serializes the user claims into a signed cryptographic token and gives it to the client.
When the client sends the token back in the Authorization header or an HttpOnly cookie, ANY backend server holding the shared secret or public key can cryptographically verify that:
1. The payload was issued by our authentication authority.
2. Not a single character of the payload was tampered with in transit.
3. The token has not expired.
// NestJS Stateless JWT Verification
import { Injectable, CanActivate, ExecutionContext, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
@Injectable()
export class JwtAuthGuard implements CanActivate {
constructor(private jwtService: JwtService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
const token = this.extractTokenFromHeader(request);
if (!token) {
throw new UnauthorizedException('No token provided');
}
try {
// Cryptographic verification happens locally in CPU memory - zero DB calls!
const payload = await this.jwtService.verifyAsync(token, {
secret: process.env.JWT_SECRET,
});
request['user'] = payload;
return true;
} catch {
throw new UnauthorizedException('Invalid or expired token');
}
}
private extractTokenFromHeader(request: any): string | undefined {
const [type, token] = request.headers.authorization?.split(' ') ?? [];
return type === 'Bearer' ? token : undefined;
}
}3. The Hidden Trade-off: Revocation
Here is where most candidates fail: they praise JWT as a magic silver bullet without understanding the fatal drawback.
Because JWT is stateless, you cannot simply 'delete' a session. If a user's laptop is stolen or permissions change from Admin to User, their JWT remains completely valid until its `exp` claim expires.
The production answer: Use short-lived access tokens (5 to 15 minutes) paired with a rotating refresh token stored securely in an `HttpOnly`, `Secure`, `SameSite=Strict` cookie, with a lightweight Redis blacklist only for revoked refresh tokens.
Always articulate the trade-offs: don't just say “JWT is faster”. Explain that stateless tokens trade off instantaneous revocation for infinite horizontal read scalability. That separates junior memorize-answers from real systems thinkers.

One email when a post goes up. Nothing else. There is also an RSS feed.