AutoBot has implemented a centralized Redis client system to standardize Redis access across all components. This ensures consistent connection management, error handling, and configuration.
- Primary Module:
src/utils/redis_client.py - Database Manager:
src/utils/redis_database_manager.py
# Import centralized Redis client
from src.utils.redis_client import get_redis_client
async def your_function():
# Get Redis client for specific database
redis_client = await get_redis_client('main')
if redis_client:
await redis_client.set("key", "value")
value = await redis_client.get("key")
else:
# Handle Redis unavailable scenario
logger.warning("Redis not available, using fallback")-
scripts/utilities/npu_worker.py- Before: Direct
redis.Redis()connection - After: Uses
get_redis_client('main') - Benefits: Centralized connection management, automatic fallback
- Before: Direct
-
scripts/monitoring_system.py- Before: Manual Redis import and connection
- After: Centralized Redis client integration
- Benefits: Consistent error handling, configuration management
-
scripts/phase_validation_system.py- Before: Blocking Redis operations with manual connection
- After: Async Redis operations with centralized client
- Benefits: Non-blocking health checks, proper async handling
-
scripts/startup_coordinator.py- Before: Synchronous Redis ping for health checks
- After: Async Redis health checks with proper error handling
- Benefits: Better startup sequence management
-
scripts/utilities/test_autobot_functionality.py- Before: Manual Redis connection with hardcoded parameters
- After: Centralized client with standardized test operations
- Benefits: Consistent testing across environments
The system supports multiple Redis databases:
# Available database configurations
databases = {
'main': 0, # Primary application data
'cache': 1, # Caching layer
'sessions': 2, # User sessions
'tasks': 3, # Background tasks
'metrics': 4, # Performance metrics
'logs': 5, # Application logs
'knowledge': 6, # Knowledge base data
'vectors': 7, # Vector embeddings
'analytics': 8, # Analytics data
'temp': 9 # Temporary data
}The system automatically adapts to different deployment environments:
- WSL + Docker Desktop: Uses localhost with Docker port mapping
- Linux Native: Direct Docker IP access (192.168.65.x)
- Distributed: Custom Redis endpoints per environment
All components implement graceful fallback when Redis is unavailable:
try:
redis_client = await get_redis_client('main')
if redis_client:
# Use Redis operations
pass
else:
# Fallback to file-based storage or in-memory caching
logger.warning("Redis unavailable, using fallback storage")
except Exception as e:
logger.error(f"Redis error: {e}")
# Continue with degraded functionality- Automatic connection retry logic
- Health check integration
- Timeout handling
- Connection pooling
# Correct - Async operations
redis_client = await get_redis_client('main')
await redis_client.set("key", "value")
# Incorrect - Blocking operations
redis_client = redis.Redis() # Don't do this
redis_client.set("key", "value")redis_client = await get_redis_client('main')
if redis_client:
# Redis available
await redis_client.lpush("queue", data)
else:
# Fallback mechanism
store_in_file(data)# Use specific databases for different purposes
cache_client = await get_redis_client('cache') # For caching
session_client = await get_redis_client('sessions') # For sessions
metrics_client = await get_redis_client('metrics') # For metricstry:
redis_client = await get_redis_client('main')
if redis_client:
result = await redis_client.get("key")
return result
except Exception as e:
logger.error(f"Redis operation failed: {e}")
return None # Or appropriate fallbackAll components now use standardized Redis health checks:
async def check_redis_health():
try:
redis_client = await get_redis_client('main')
if redis_client:
await redis_client.ping()
return True
return False
except Exception:
return FalseThe test suite validates:
- Redis connectivity across all databases
- Fallback behavior when Redis is unavailable
- Performance under load
- Error recovery scenarios
- Uniform Redis access pattern across all components
- Standardized error handling
- Consistent configuration management
- Automatic connection management
- Graceful degradation when Redis unavailable
- Connection pooling and timeout handling
- Single point of configuration
- Easier debugging and monitoring
- Simplified testing procedures
- Connection pooling reduces overhead
- Async operations prevent blocking
- Efficient resource utilization
- Redis Cluster Support: For high-availability deployments
- Metrics Collection: Detailed Redis performance metrics
- Connection Monitoring: Real-time connection health
- Automatic Failover: Multiple Redis endpoints support
- SSL/TLS Support: Secure Redis connections
- Authentication: Redis AUTH integration
- Compression: Data compression for large payloads
- Sharding: Automatic data distribution
-
Redis Connection Refused
- Check Redis service status
- Verify network connectivity
- Confirm Redis configuration
-
Slow Redis Operations
- Monitor Redis memory usage
- Check network latency
- Review key expiration policies
-
Database Selection Errors
- Verify database exists in configuration
- Check Redis maxdatabases setting
- Validate database permissions
# Check Redis status
docker ps | grep redis
# Test Redis connectivity
redis-cli ping
# Monitor Redis operations
redis-cli monitor
# Check Redis configuration
redis-cli CONFIG GET databasesThe Redis standardization migration has successfully:
- ✅ Migrated 5 critical components to centralized Redis client
- ✅ Implemented consistent error handling across all components
- ✅ Established graceful fallback mechanisms
- ✅ Standardized async operations for better performance
- ✅ Created comprehensive documentation and best practices
This standardization improves AutoBot's reliability, maintainability, and performance while providing a solid foundation for future Redis-based features.