Sub topic 3
Expiry, Rotation and Revocation
This chapter assumes you understand HTTP cookies, web services, and load balancers.
Expiry, rotation and revocation
A session has a lifetime. Expiry ends a session after a fixed time or after a period of inactivity.
With a server-side session, the store can enforce expiry with a time-to-live (TTL). A request can refresh the TTL to create a sliding session. With a client-side token, the token usually contains an exp timestamp. The service rejects it after that time.
Rotation replaces a session identifier or token during an important event, such as login or privilege elevation. Rotate after login to prevent session fixation. A common pattern is to create a new session ID after authentication and delete the pre-login session.
Revocation ends a session before its normal expiry. A server-side store can delete the session immediately. Signed client-side tokens are harder to revoke because the client holds a valid copy. You may keep a denylist, use short token lifetimes with refresh tokens, or maintain a user-wide session version.
When the session store fails or slows down
A shared store is part of the request path. If it becomes slow, request latency rises. If it is unavailable, the service must choose between rejecting requests, allowing only public requests, or using a carefully bounded fallback. Accepting requests without checking authentication can create a security failure.
Set connection and operation timeouts. Monitor latency, errors, and capacity. Use replication or a managed store where appropriate, but define what happens during failover. A fallback cache must not silently treat an unknown session as authenticated.
Other local state to move out of instances
Session data is not the only state that breaks horizontal scaling. Move these items out of individual instances when requests can reach any instance:
- Uploaded files and generated reports
- Background job queues
- Rate-limit counters
- WebSocket connection metadata
- Shared feature flags and configuration
- Temporary workflows that must survive a restart
Choosing a strategy for your system
| Strategy | Strength | Main cost |
|---|---|---|
| Sticky sessions | Simple migration path | Uneven load and weak failover |
| Shared session store | Immediate revocation and small cookies | Store dependency and latency |
| Signed tokens | No session lookup | Larger tokens and difficult revocation |
Choose sticky sessions only when temporary affinity is acceptable. Choose a shared store when you need central control or immediate logout. Choose signed tokens when independent verification and low lookup overhead matter more than immediate revocation.
Exercise
Which approach best fits an administration system that must log out a user immediately across all instances?
Show answer
A shared server-side session store, because deleting the session invalidates it for every instance.