2026-01-08 11:46:34 +08:00
# Redis Connection Capability Design
## Context
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- 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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- Redis server administration
- Redis cluster management
- Advanced Redis features (e.g., pub/sub, streams) beyond basic queue operations
## Decisions
### Decision: Redis Client Library
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **What**: Use the official `redis` npm package
- **Why**: It's the official Redis client for Node.js, well-maintained, and supports all Redis commands
2026-01-13 19:45:05 +08:00
- **Alternatives considered**:
2026-01-08 11:46:34 +08:00
- `ioredis` : More features but more complex
- `node-redis` : Older, less maintained
### Decision: Connection Configuration
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **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
2026-01-13 19:45:05 +08:00
- **Alternatives considered**:
2026-01-08 11:46:34 +08:00
- Configuration files: Less flexible for containerized environments
- Hardcoded values: Not suitable for production use
### Decision: Reconnection Strategy
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **What**: Use exponential backoff for reconnection attempts
- **Why**: Exponential backoff prevents overwhelming the Redis server with reconnection attempts, while still ensuring timely reconnection
2026-01-13 19:45:05 +08:00
- **Alternatives considered**:
2026-01-08 11:46:34 +08:00
- Fixed interval reconnection: Less efficient, could overwhelm the server
- No reconnection: Not suitable for production use
## Architecture
### Redis Connection Architecture
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
```
Redis Connection Manager
├── Redis Client
├── Connection Monitor
├── Reconnection Handler
└── Configuration Manager
```
## Implementation Details
### Redis Client Initialization
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
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
2026-01-13 19:45:05 +08:00
| 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 |
2026-01-08 11:46:34 +08:00
### Reconnection Implementation
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- 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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- 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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **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
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
No migration is required as this is a new feature.
## Open Questions
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- What is the expected Redis server availability?
- Should we implement connection pooling for better performance?
2026-01-13 19:45:05 +08:00
- Should we support Redis Sentinel or Cluster for high availability?