π Redis
The redis module provides robust integration with Redis Streams for background processing and event-driven architecture in the codex-bot framework.
π§ The Why
Event-Driven Architecture
In a large bot, some actions (e.g., sending notifications, processing payments) should happen in the background without blocking the main Telegram polling loop. Redis Streams provide a reliable way to decouple these actions from the bot's UI logic.
Reliability (Consumer Groups)
The RedisStreamProcessor uses Redis Consumer Groups to ensure that every message is processed at least once. It supports automatic acknowledgment (ACK) and retry scheduling (via RetrySchedulerProtocol), making the system resilient to crashes and network issues.
π The Flow
- Production: An external service or another bot feature adds a message to a Redis Stream (e.g.,
bot_events). - Consumption:
RedisStreamProcessorpolls the stream using a Consumer Group and reads a batch of messages. - Dispatching:
BotRedisDispatcherreceives the message and finds the appropriate handler based on thetypefield in the payload. - Execution: The handler (e.g.,
on_booking_confirmed) is called with the project's DI-container. - Acknowledgment: If the handler succeeds, the processor sends an
XACKto Redis. If it fails, the message is rescheduled for retry.
πΊοΈ Module Map
| Component | Description |
|---|---|
| π API Reference | Technical details for RedisRouter and BotRedisDispatcher. |
| π Stream Processor | Redis Stream polling loop (Consumer Group). |
| π Dispatcher | Redis Stream message dispatcher for the bot. |
| π Protocols | RetrySchedulerProtocol and StreamStorageProtocol. |
Last Updated: 2025-02-07