The Architectural Purpose of Short-Lived Tokens
Proactive Versus Reactive Refresh Strategies
Proactive Refresh
expires_in parameter (typically expressed in seconds), the client calculates an absolute expiration timestamp based on the client device’s clock.Reactive Refresh
The Stampede Problem: Managing Concurrent Requests
Implementing Mutex Locks and Request Queuing
-
The new access token is stored in the application state.
-
The queued requests have their authorization headers updated with the new bearer token.
-
Every queued request is replayed against the API.
-
The lock is released, and the in-flight promise reference is cleared.
Cross-Tab Synchronization in Web Applications
Storage Security and Architecture Trade-offs
Native Mobile Environments
Web Environments and the Backend-for-Frontend Pattern
localStorage or sessionStorage exposes the entire session to any script injected through third-party dependencies, compromised CDN bundles, or XSS vectors. If an attacker reads a refresh token from storage, they can quietly impersonate the user indefinitely from a remote machine.HttpOnly, Secure, and SameSite cookie dispatched to the browser.HttpOnly cookies are completely invisible to client-side JavaScript, malicious scripts cannot read them. When the browser makes API requests, it automatically attaches the session cookie. The BFF layer handles the token expiration, concurrency queueing, and silent refreshes entirely on the server side before forwarding the request to the upstream microservices. This pattern eliminates client-side token juggling and significantly hardens the application’s attack surface.