diagram.mmd — flowchart
Secure Session Storage flowchart diagram

Secure session storage is the server-side management of user session state — including how session data is stored, how session IDs are generated, how sessions are validated on each request, and how they are safely terminated.

Session management begins at login. After successfully authenticating the user, the server generates a cryptographically random session ID using a secure random number generator (at least 128 bits of entropy). This ID is the only thing stored in the client's cookie — the actual session data lives on the server side, keyed by this ID. This is fundamental: never store sensitive data like user roles or permissions in a cookie value directly unless the cookie is cryptographically signed.

Session data is stored server-side in a fast data store. In-memory stores like Redis are the standard choice for production applications — they support fast lookups by session ID, automatic TTL expiry, and horizontal scaling (all server instances can access the same Redis instance). Storing sessions in local server memory is only appropriate for single-instance deployments.

Each request, the server reads the session ID from the cookie, performs a lookup in the session store, validates the session exists and has not expired, and attaches the session data to the request context. The session's last-activity timestamp should be updated on each request (a rolling TTL), and absolute session expiry (maximum lifetime regardless of activity) should also be enforced.

Secure session practices include: regenerating the session ID after privilege escalation (login, role change) to prevent session fixation attacks; enforcing idle timeouts and absolute session lifetimes; binding sessions to the user-agent string or IP address for additional anomaly detection; and providing explicit logout that deletes the server-side session record (not just clears the cookie). See Secure Cookie Flow for the cookie configuration that protects the session ID in transit.

Free online editor
Edit this diagram in Graphlet
Fork, modify, and export to SVG or PNG. No sign-up required.
Open in Graphlet →

Frequently asked questions

Secure session storage is the server-side management of user session state after authentication. The server generates a cryptographically random session ID, stores session data in a fast backend store (typically Redis), and gives the client only the opaque session ID in a cookie. Sensitive state like user roles never travels to the client.
If an attacker can set a known session ID on the victim's browser before login (session fixation), they can use that ID to take over the session after the victim authenticates. Regenerating the session ID at login invalidates any pre-login session ID the attacker may have planted.
An idle timeout terminates the session after a period of inactivity — the timer resets on each request. An absolute lifetime caps the total session duration regardless of activity. Both should be enforced: idle timeout reduces risk from unattended sessions, while absolute lifetime limits the window for stolen session IDs to be exploited.
mermaid
flowchart TD A[User authenticates successfully] --> B[Generate cryptographically\nrandom session ID\n128+ bits entropy] B --> C[Store session data server-side\nin Redis with TTL] C --> D[Set HttpOnly Secure\nSameSite cookie with session ID] D --> E[Subsequent request arrives\nwith session cookie] E --> F[Extract session ID\nfrom cookie] F --> G[Lookup session in Redis\nby session ID] G --> H{Session found\nand not expired?} H -- No --> I[Reject request\nRedirect to login] H -- Yes --> J[Attach session data\nto request context] J --> K{Privilege change?\nLogin or role escalation} K -- Yes --> L[Regenerate session ID\nprevent session fixation] K -- No --> M[Update last-activity timestamp\nrolling TTL in Redis] L --> M M --> N[Process authenticated request] N --> O{User logs out?} O -- Yes --> P[Delete session record\nfrom Redis] P --> Q[Clear cookie in response\nSet-Cookie: session= Expires=epoch]
Copied to clipboard