Expand description
Redis-based distributed state backend implementation
This module provides a production-ready Redis state backend with comprehensive features for distributed state management including connection pooling, cluster support, TTL management, batch operations, and metrics tracking.
§Features
- Connection Pooling: Automatic connection management with configurable pool size
- Cluster Support: Works with both standalone and clustered Redis deployments
- TTL Management: Per-key expiration with automatic cleanup
- Batch Operations: Efficient pipeline support for bulk operations
- Retry Logic: Configurable retry strategy for transient failures
- Metrics: Comprehensive operation tracking and statistics
- Atomic Operations: MULTI/EXEC transactions for consistency
- Health Checks: Connection monitoring and auto-reconnection
§Example Usage
use processor::state::{RedisStateBackend, RedisConfig, StateBackend};
use std::time::Duration;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Configure Redis backend
let config = RedisConfig::builder()
.url("redis://localhost:6379")
.key_prefix("myapp:")
.default_ttl(Duration::from_secs(3600))
.pool_size(10, 50)
.build()?;
// Create backend
let backend = RedisStateBackend::new(config).await?;
// Store state with TTL
backend.put_with_ttl(b"session:123", b"user_data", Duration::from_secs(1800)).await?;
// Retrieve state
if let Some(data) = backend.get(b"session:123").await? {
println!("Session data: {:?}", data);
}
// Batch operations
let keys = vec![b"key1", b"key2", b"key3"];
let values = backend.batch_get(&keys).await?;
// Get statistics
let stats = backend.stats().await;
println!("Hit rate: {:.2}%", stats.hit_rate() * 100.0);
Ok(())
}Structs§
- Redis
Config - Redis connection configuration
- Redis
Config Builder - Builder for RedisConfig
- Redis
State Backend - Redis state backend with connection pooling and advanced features
- Redis
Stats - Statistics for Redis backend operations