Redis Storage
Redis storage is suitable for production environments and distributed applications, providing high performance and automatic expiration capabilities.
Features
- ✅ Data persistence
- ✅ Distributed support
- ✅ High-performance read/write
- ✅ Native TTL support
- ✅ Async persistence support
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
WithRedisClientURL(url string) |
string |
- | Create Redis client via URL, format: redis://[username:password@]host:port[/database] |
WithRedisInstance(instanceName string) |
string |
- | Use a pre-configured Redis instance (lower priority than URL) |
WithExtraOptions(extraOptions ...any) |
[]any |
nil |
Extra options for the Redis client |
WithSessionEventLimit(limit int) |
int |
1000 |
Maximum events per session |
WithSessionTTL(ttl time.Duration) |
time.Duration |
0 (no expiry) |
TTL for session state and events |
WithAppStateTTL(ttl time.Duration) |
time.Duration |
0 (no expiry) |
TTL for app-level state |
WithUserStateTTL(ttl time.Duration) |
time.Duration |
0 (no expiry) |
TTL for user-level state |
WithEnableAsyncPersist(enable bool) |
bool |
false |
Enable async persistence |
WithAsyncPersisterNum(num int) |
int |
10 |
Number of async persistence workers |
WithSummarizer(s summary.SessionSummarizer) |
summary.SessionSummarizer |
nil |
Inject session summarizer |
WithAsyncSummaryNum(num int) |
int |
3 |
Number of summary processing workers |
WithSummaryQueueSize(size int) |
int |
100 |
Summary task queue size |
WithSummaryJobTimeout(timeout time.Duration) |
time.Duration |
60s |
Timeout for a single summary job |
WithKeyPrefix(prefix string) |
string |
"" |
Redis key prefix; all keys will start with prefix: |
WithAppendEventHook(hooks ...session.AppendEventHook) |
[]session.AppendEventHook |
nil |
Add event write hooks |
WithGetSessionHook(hooks ...session.GetSessionHook) |
[]session.GetSessionHook |
nil |
Add session read hooks |
Basic Configuration
Instance Reuse
If multiple components need to use the same Redis instance, register and reuse it:
With Summary
Async Persistence
Enable async persistence to improve write performance:
Storage Structure
Redis storage uses the following key structure:
Use Cases
| Scenario | Recommended Configuration |
|---|---|
| Production | Configure TTL, enable async persistence |
| Distributed deployment | Use Redis Cluster |
| High concurrency | Increase AsyncPersisterNum |
| Data persistence needed | Configure Redis persistence strategy |
Notes
- Connection: Ensure Redis service is accessible; use connection pooling
- TTL management: Redis natively supports TTL; no additional cleanup tasks needed
- Memory management: Monitor Redis memory usage; configure reasonable maxmemory
- High availability: Use Redis Sentinel or Cluster for production
- Priority:
WithRedisClientURLhas higher priority thanWithRedisInstance