NEW in v2.1.0 - Enterprise session management with Redis for stateful authentication and concurrent session control.
Overview
Redis provides production-grade session storage with:- Persistent Sessions - Survive server restarts
- Distributed Sessions - Share across multiple instances
- Sliding Expiration - Extend sessions on user activity
- Concurrent Limits - Control max sessions per user
- Instant Revocation - Immediate logout across all devices
- Session Analytics - Track active users and session patterns
Why Redis Sessions?
Token-based (Stateless)
Best for: Serverless, Cloud Run, stateless architectures
- No server storage required
- Lower infrastructure costs
- Simpler deployment
- Cannot revoke individual tokens
Session-based (Stateful)
Best for: Enterprise apps, multi-instance deployments
- Longer session lifetimes (24+ hours)
- Instant revocation
- Concurrent session limits
- User session tracking
Quick Start
1
Deploy Redis
- Docker Compose
- Kubernetes
- Cloud Managed
2
Configure Application
3
Test Session Store
4
Verify in Redis
Session Lifecycle
Session Configuration
Session TTL
Concurrent Sessions
- 1 session: Single device login (revokes other devices)
- 3-5 sessions: Normal multi-device usage
- Unlimited: Set to 0 or omit
Session Metadata
Store additional context with sessions:API Operations
Create Session
Get Session
Refresh Session
Revoke Session
Session Analytics
Integration with Authentication
Keycloak + Redis Sessions
Production Deployment
High Availability
Deploy Redis with replication:redis.conf
save 900 1 # Save after 900s if >= 1 key changed save 300 10 # Save after 300s if >= 10 keys changed save 60 10000 # Save after 60s if >= 10000 keys changedAOF (append-only file) for durability
appendonly yes appendfsync everysecAlways use password
REDIS_PASSWORD=strong-random-password requirepass strong-random-passwordTLS/SSL in production
REDIS_SSL=true REDIS_URL=rediss://redis-session:6380/0Redis 6+ with ACL
REDIS_USERNAME=sessions REDIS_PASSWORD=passwordredis.conf
maxmemory 2gb maxmemory-policy allkeys-lru # Evict least recently usedOr volatile-lru (evict only keys with TTL)
maxmemory-policy volatile-lruMonitoring
Key metrics to track:- Memory usage > 80%
- Evicted keys > 0
- Hit ratio < 90%
- Replication lag > 5s
Troubleshooting
Connection refused
Connection refused
Authentication failed
Authentication failed
Error:
NOAUTH Authentication requiredSolutions:- Check REDIS_PASSWORD environment variable
- Verify redis.conf has
requirepassset - Use
-a passwordflag with redis-cli
Session not found
Session not found
Possible causes:
- Session expired (check TTL)
- Redis evicted key (memory full)
- Wrong Redis database (check
/0in URL) - Session was revoked
High memory usage
High memory usage
Solutions:
- Reduce TTL: Lower SESSION_TTL_SECONDS
- Enable eviction: Set
maxmemory-policy allkeys-lru - Increase memory: Scale up Redis instance
- Clean expired: Redis auto-expires, but run
FLUSHDBif needed
Slow session operations
Slow session operations
Performance tuning:
- Use connection pooling (automatic in Python redis client)
- Enable pipelining for batch operations
- Use local Redis instance (reduce network latency)
- Check Redis logs for slow queries
Migration from Token-based
Switching from stateless tokens to Redis sessions:1
Deploy Redis
Deploy Redis instance (see Quick Start above)
2
Update Configuration
3
Dual Mode Support (Optional)
Support both tokens and sessions during migration:
4
Update Client Applications
Migrate clients from token to session:Before (Token):After (Session):
5
Deprecate Token Support
After migration period, remove token support:
Next Steps
Authentication
Configure session mode authentication
Keycloak SSO
Integrate Keycloak with sessions
Kubernetes Deployment
Deploy Redis on Kubernetes
Production Checklist
Session security best practices
Production Ready: Redis sessions provide enterprise-grade session management with instant revocation and multi-device support!