# Redis Connection Capability Design ## Context This design document describes the technical implementation of the Redis connection capability for the BLS Project Console, which manages the connection between the system and the Redis server for reading logs and sending commands. ## Goals / Non-Goals ### Goals - Establish and manage connection to Redis server - Provide configuration options for Redis connection - Implement automatic reconnection mechanism - Handle connection errors gracefully - Monitor and report connection status ### Non-Goals - Redis server administration - Redis cluster management - Advanced Redis features (e.g., pub/sub, streams) beyond basic queue operations ## Decisions ### Decision: Redis Client Library - **What**: Use the official `redis` npm package - **Why**: It's the official Redis client for Node.js, well-maintained, and supports all Redis commands - **Alternatives considered**: - `ioredis`: More features but more complex - `node-redis`: Older, less maintained ### Decision: Connection Configuration - **What**: Use environment variables for Redis connection configuration - **Why**: Environment variables are a standard way to configure services in containerized environments, and they allow easy configuration without code changes - **Alternatives considered**: - Configuration files: Less flexible for containerized environments - Hardcoded values: Not suitable for production use ### Decision: Reconnection Strategy - **What**: Use exponential backoff for reconnection attempts - **Why**: Exponential backoff prevents overwhelming the Redis server with reconnection attempts, while still ensuring timely reconnection - **Alternatives considered**: - Fixed interval reconnection: Less efficient, could overwhelm the server - No reconnection: Not suitable for production use ## Architecture ### Redis Connection Architecture ``` Redis Connection Manager ├── Redis Client ├── Connection Monitor ├── Reconnection Handler └── Configuration Manager ``` ## Implementation Details ### Redis Client Initialization 1. Server reads Redis configuration from environment variables 2. Server creates a Redis client instance with the configuration 3. Server attaches event listeners for connection events (connect, error, end, reconnecting) 4. Server attempts to connect to Redis ### Configuration Parameters | Parameter | Default Value | Environment Variable | Description | | -------------------- | ------------- | ---------------------------- | --------------------------------------------- | | host | localhost | REDIS_HOST | Redis server hostname | | port | 6379 | REDIS_PORT | Redis server port | | password | null | REDIS_PASSWORD | Redis server password | | db | 0 | REDIS_DB | Redis database index | | connectTimeout | 10000 | REDIS_CONNECT_TIMEOUT | Connection timeout in milliseconds | | maxRetriesPerRequest | 3 | REDIS_MAX_RETRIES | Maximum retries per request | | reconnectStrategy | exponential | REDIS_RECONNECT_STRATEGY | Reconnection strategy (exponential, fixed) | | reconnectInterval | 1000 | REDIS_RECONNECT_INTERVAL | Base reconnection interval in milliseconds | | maxReconnectInterval | 30000 | REDIS_MAX_RECONNECT_INTERVAL | Maximum reconnection interval in milliseconds | ### Reconnection Implementation - Use the built-in reconnection feature of the `redis` package - Configure exponential backoff with: - Initial delay: 1 second - Maximum delay: 30 seconds - Factor: 1.5 - Log each reconnection attempt with timestamp and delay ### Error Handling - **Connection errors**: Log the error, update connection status, and attempt to reconnect - **Command errors**: Log the error, update the command status, and notify the user - **Timeout errors**: Log the error, update connection status, and attempt to reconnect ### Connection Monitoring - Track connection status (connecting, connected, disconnected, error) - Log status changes with timestamps - Expose connection status via API endpoint - Update UI with connection status changes via Server-Sent Events (SSE) ## Risks / Trade-offs ### Risk: Redis Server Unavailability - **Risk**: If the Redis server is unavailable for an extended period, the system won't be able to read logs or send commands - **Mitigation**: Implement proper error handling and reconnection logic, and notify users of the issue ### Risk: Misconfiguration of Redis Connection - **Risk**: Incorrect Redis configuration could lead to connection failures - **Mitigation**: Validate configuration on startup, log configuration values (excluding passwords), and provide clear error messages ### Risk: Performance Impact of Reconnection Attempts - **Risk**: Frequent reconnection attempts could impact system performance - **Mitigation**: Use exponential backoff to reduce the frequency of reconnection attempts, and limit the maximum reconnection delay ## Migration Plan No migration is required as this is a new feature. ## Open Questions - What is the expected Redis server availability? - Should we implement connection pooling for better performance? - Should we support Redis Sentinel or Cluster for high availability?