Invalidate JWT token on logout without blacklisting
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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:
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:
- Any refresh token stored in the database or cache is deleted.
- The client-side application deletes or clears the stored tokens in the browser or mobile app storage.
Table Summary: Comparison of Methods
| Strategy | Pros | Cons | Complexity |
| Short Expiration | Simple, Stateless | Frequent renewal needed | Low |
| Refresh Tokens | Revokable, Secure | Requires handling multiple tokens | Moderate |
| Change Detection Markers | No additional queries for most actions | Critical changes require a DB check | High |
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.

