Redis Database Separation Implementation
Overview
Following the task management system replacement, the Redis configuration has been updated to use separate databases for different purposes, providing better isolation and reliability as outlined in the task management replacement document.
Database Allocation
The system now uses the following Redis database separation:
- Database 0 (REDIS_CACHE_DB): Application cache
- API response caching
- General application cache operations
-
Used by: Main API Redis client (
app/core/redis.py) -
Database 1 (REDIS_TASKS_DB): TaskiQ queues and job state
- Task queue management
- Job state persistence
-
Used by: TaskiQ services (
services/shared/taskiq_redis.py) -
Database 2 (REDIS_HEALTH_DB): Health monitoring
- Service health status
- Health metrics and heartbeats
-
Used by: Health monitoring system (
app/core/health_redis.py,app/services/health_consumer.py) -
Database 3 (REDIS_NOTIFICATIONS_DB): Real-time notifications and pub/sub
- Tracker queue invalidation messages
- Real-time notification channels
- Used by: Notification system (
app/core/tracker_queue_invalidation.py)
Configuration Changes
Environment Variables
The following environment variables have been added to both main API and services configurations:
REDIS_CACHE_DB=0
REDIS_TASKS_DB=1
REDIS_HEALTH_DB=2
REDIS_NOTIFICATIONS_DB=3
Updated Files
- Main API Configuration (
app/core/config.py) - Added multi-database environment variables
-
Synchronized with services configuration
-
Main Redis Client (
app/core/redis.py) - Updated to use configurable cache database instead of hardcoded database 0
-
Added proper null checks for CacheManager methods
-
Tracker Queue Invalidation (
app/core/tracker_queue_invalidation.py) - Updated to use notifications database (DB 3) for pub/sub operations
-
Implemented cluster-agnostic Redis client using URL-based connections
-
Services Configuration (
services/shared/config.py) - Already configured with multi-database support
Benefits
Isolation
- Cache operations don't interfere with task queues
- Health monitoring data is separate from application cache
- Pub/sub notifications have dedicated database space
Reliability
- Cache flushes (
FLUSHALL) only affect the cache database, not tasks or health data - Task queue persistence is isolated from volatile cache data
- Health monitoring continues independently of other operations
Cluster Compatibility
- All Redis clients use cluster-agnostic URL-based connections
- Proper handling of both standalone and cluster Redis deployments
- Consistent connection patterns across all services
Frontend and Admin Panel Impact
No changes required for frontend or admin panel applications:
- Frontend and admin panel do not directly connect to Redis
- All Redis operations are handled by the backend API
- Existing functionality continues to work without modification
Deployment Notes
Docker Compose
The compose.yml file already includes the necessary environment variables for multi-database configuration.
Redis Configuration
Ensure your Redis/Dragonfly instance supports multiple databases:
- Standalone Redis: Supports databases 0-15 by default
- Redis Cluster: Database separation handled at the application level
- AWS Valkey: Supports multiple databases in standalone mode
Migration
No data migration is required as this is a configuration change that maintains backward compatibility while adding proper database separation.
Verification
To verify the configuration is working correctly:
- Check Redis connections in logs:
- Main API should connect to database 0 for cache
- Notifications should connect to database 3 for pub/sub
-
Health monitoring should connect to database 2
-
Test cache operations:
- API caching should work normally
-
Cache flushes should not affect other databases
-
Test real-time features:
- Tracker queue notifications should work
- Health monitoring should continue functioning
- SSE endpoints should work normally
Troubleshooting
Connection Issues
- Verify Redis/Dragonfly is configured to support multiple databases
- Check that environment variables are properly set
- Ensure Redis authentication is configured correctly
Cluster Mode
- In cluster mode, database separation is handled at the application level
- All clients use cluster-compatible connection patterns
- Hash tags are used where appropriate for key distribution
This implementation provides the foundation for the reliable, cluster-compatible Redis architecture required for the new task management system.