JWT Token
User Logout
Token Invalidity
Web Security
Web Development

Invalidate JWT token on logout without blacklisting

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

JSON Web Tokens (JWTs) are a popular means for handling authentication and authorization in web and mobile applications. JWTs are secure, lightweight, and can be easily transmitted between parties. However, managing the lifecycle of a JWT, particularly invalidation upon user logout, presents unique challenges. Unlike session-based authentication, where you can simply destroy the session, JWTs, by design, are stateless. This statelessness implies that once a JWT is issued, it is valid until it expires, unless explicitly checked against a storage mechanism that maintains invalid tokens—commonly referred to as blacklisting.

The Problem with Blacklisting

Blacklisting involves maintaining a store (such as a database or cache) of tokens that are still valid but no longer trusted. Typically, when a user logs out, their token is added to this blacklist. For every authenticated request, the application checks if the token is blacklisted, which introduces latency and requires additional data storage. Moreover, this approach contradicts the stateless nature of JWTs, adding complexity and overhead.

Strategies for Invalidating JWTs without Blacklisting

1. Setting a Short Expiration Time

One simple technique is to issue JWTs that have a very short expiration time (like 15-30 minutes). To maintain user sessions, implement a mechanism that refreshes the tokens automatically if the user remains active.

Pros:

  • Maintains the statelessness of JWTs.
  • Simple to implement.

Cons:

  • Requires a mechanism on the client side to handle token renewal.

2. Using Rotating Refresh Tokens

A more robust approach involves using a long-lived refresh token in addition to a short-lived access token. When a user logs out, the refresh token is invalidated on the server, preventing the issuance of new access tokens.

Pros:

  • Access tokens remain short-lived and stateless.
  • Refresh tokens can be revoked, instantly ending the session.

Cons:

  • Slightly more complex as it requires handling two types of tokens.

3. Change Detection Markers

Another approach is to include a change detection marker (CDM) in the payload of the JWT. This CDM could be a timestamp of the last password change or a serialized version number from the user database.

Technical Implementation:

javascript
1const jwtPayload = {
2  sub: userId,
3  name: userName,
4  iat: Math.floor(Date.now() / 1000),
5  cdm: userData.version,
6};

Here, every time critical user information changes (e.g., password update), userData.version gets incremented. The server, upon receiving a JWT, checks if the cdm in the JWT matches the latest cdm stored in the database.

Pros:

  • Token remains stateless for most user actions.
  • Forces token invalidation on critical changes.

Cons:

  • Requires a query to the user database for validating the cdm.

Handling User Logout

When a user requests to logout, instead of blacklisting the JWT, ensure that:

  1. Any refresh token stored in the database or cache is deleted.
  2. The client-side application deletes or clears the stored tokens in the browser or mobile app storage.

Table Summary: Comparison of Methods

StrategyProsConsComplexity
Short ExpirationSimple, StatelessFrequent renewal neededLow
Refresh TokensRevokable, SecureRequires handling multiple tokensModerate
Change Detection MarkersNo additional queries for most actionsCritical changes require a DB checkHigh

Conclusion

Invalidating JWTs without resorting to blacklisting can align better with the stateless and scalable nature of modern web applications. Each method has its trade-offs, and the right choice depends on specific application needs and security requirements. By strategically leveraging token expiration, refresh tokens, or change detection markers, developers can effectively manage user sessions while maintaining performance and scalability.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.