Skip to content

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

  1. Main API Configuration (app/core/config.py)
  2. Added multi-database environment variables
  3. Synchronized with services configuration

  4. Main Redis Client (app/core/redis.py)

  5. Updated to use configurable cache database instead of hardcoded database 0
  6. Added proper null checks for CacheManager methods

  7. Tracker Queue Invalidation (app/core/tracker_queue_invalidation.py)

  8. Updated to use notifications database (DB 3) for pub/sub operations
  9. Implemented cluster-agnostic Redis client using URL-based connections

  10. Services Configuration (services/shared/config.py)

  11. 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:

  1. Check Redis connections in logs:
  2. Main API should connect to database 0 for cache
  3. Notifications should connect to database 3 for pub/sub
  4. Health monitoring should connect to database 2

  5. Test cache operations:

  6. API caching should work normally
  7. Cache flushes should not affect other databases

  8. Test real-time features:

  9. Tracker queue notifications should work
  10. Health monitoring should continue functioning
  11. 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.