Session Management Overview
Session Store
Redis, PostgreSQL Scalable storage
Session Expiration
Auto expiration Sliding expiration
Token Refresh
Refresh Token Access Token reissue
Logout
Session invalidation Token blacklist
Session-Based Authentication
How Sessions Work
Session-based authentication is a method where the server stores the user’s login state on the server side. Understanding how it works helps you better handle security and scalability issues. Session Authentication Flow:- When user logs in, server generates a session ID
- Session data (user ID, etc.) is stored in session store
- Session ID is sent to client as a cookie
- Each subsequent request authenticates user via session ID in cookie
- Server has full control over sessions (can invalidate anytime)
- Sensitive information is not exposed to client
- Simple to implement and well-proven method
- Stores state on server, requiring scalability considerations
- Session store can become a single point of failure (SPOF)
Session Store Configuration
Memory Store (Development Only): Memory store should only be used in development environments. All sessions are lost when the server restarts, and sessions cannot be shared across multiple server instances.prefix: Prefix for session keys to distinguish from other datattl: Time To Live, automatic session expiration time (in seconds)client: Redis client instance
Redis supports cluster mode for implementing high availability (HA). Using managed services like
AWS ElastiCache or Azure Cache for Redis makes operations more convenient.
Session Expiration Strategies
Fixed Expiration: Session expires unconditionally after a set time from creation. High security but may result in poor user experience.Session Data Management
Store only minimal information in sessions. Large session size degrades performance and increases storage costs.JWT Token Management
How JWT Works
JWT (JSON Web Token) is a stateless authentication method. After the server creates a token, all information is contained within the token itself, so the server doesn’t need to store anything separately. JWT Structure:- Header: Token type and algorithm (e.g.,
{"alg": "HS256", "typ": "JWT"}) - Payload: User information and claims (e.g.,
{"userId": 123, "role": "user"}) - Signature: Signature to verify token integrity
- Good scalability as server doesn’t store state
- Suitable for microservice environments
- No separate lookup needed as information is contained in token itself
- Difficult to forcibly invalidate token from server
- Token size is larger than cookies
- If token is stolen, it can be used until expiration
Access Token and Refresh Token
Access Token is a short-lived token used for API requests. Usually has a short validity period of 15 minutes to 1 hour. Even if stolen, damage is minimized. Refresh Token is a long-lived token used to reissue Access Tokens. Usually has a validity period of 7 to 30 days and is stored on the server for management. Why use two tokens? If Access Token validity is set to 30 days:- If stolen, it can be abused for 30 days
- User permission changes are not immediately reflected
- Stolen Access Token is only valid for a short time
- Permission changes can be reflected quickly
- Refresh Token can be stored on server and invalidated
Refresh Token Database Schema
Token Refresh Implementation
Token Invalidation (Logout)
JWT is valid until expiration by default, but can be forcibly invalidated using a blacklist.Token Blacklist Middleware
Practical Patterns
Concurrent Login Restriction
How to restrict concurrent access from multiple devices with one account.Cleanup Expired Sessions
Periodically delete expired Refresh Tokens to manage database size.Cautions
Next Steps
Authentication Setup
Building authentication system
Protected Routes
Permission control with Guards
Context
Learn more about Context
Workflows
Task scheduling