3.3 KiB
Redis Pub/Sub Chain Implementation
This module implements a chain of services communicating through Redis Pub/Sub channels. Each service in the chain subscribes to the previous service's channel and publishes to its own channel, creating a processing pipeline.
Architecture
The implementation follows a simple chain pattern:
READER → PROCESSOR → WRITER
- READER: Generates mock data with a "red" stage and publishes to
chain:reader - PROCESSOR: Subscribes to
chain:reader, processes messages with "red" stage, updates stage to "processed", and publishes tochain:processor - WRITER: Subscribes to
chain:processor, processes messages with "processed" stage, updates stage to "completed", and publishes tochain:writer
Message Flow
Each message flows through the chain with a stage attribute that determines how it's processed:
- READER generates a message with
stage="red" - PROCESSOR receives the message, checks if
stage="red", processes it, and setsstage="processed" - WRITER receives the message, checks if
stage="processed", processes it, and setsstage="completed"
Performance
The implementation includes timing information to track how long messages take to flow through the entire chain. Sample output:
[READER] 1745176466.132082 | Published UUID: 74cf2312-25ec-4da8-bc0a-521b6ccd5206
[PROCESSOR] 1745176466.132918 | Received UUID: 74cf2312-25ec-4da8-bc0a-521b6ccd5206 | Published UUID: 74cf2312-25ec-4da8-bc0a-521b6ccd5206
[WRITER] 1745176466.133097 | Received UUID: 74cf2312-25ec-4da8-bc0a-521b6ccd5206 | Published UUID: 74cf2312-25ec-4da8-bc0a-521b6ccd5206 | Elapsed: 1.83ms
[READER] 1745176468.133018 | Published UUID: 2ffd217f-650f-4e10-bc16-317adcf7a59a
[PROCESSOR] 1745176468.133792 | Received UUID: 2ffd217f-650f-4e10-bc16-317adcf7a59a | Published UUID: 2ffd217f-650f-4e10-bc16-317adcf7a59a
[WRITER] 1745176468.134001 | Received UUID: 2ffd217f-650f-4e10-bc16-317adcf7a59a | Published UUID: 2ffd217f-650f-4e10-bc16-317adcf7a59a | Elapsed: 1.76ms
[READER] 1745176470.133841 | Published UUID: 87e1f3af-c6c2-4fa5-9a65-57e7327d3989
[PROCESSOR] 1745176470.134623 | Received UUID: 87e1f3af-c6c2-4fa5-9a65-57e7327d3989 | Published UUID: 87e1f3af-c6c2-4fa5-9a65-57e7327d3989
[WRITER] 1745176470.134861 | Received UUID: 87e1f3af-c6c2-4fa5-9a65-57e7327d3989 | Published UUID: 87e1f3af-c6c2-4fa5-9a65-57e7327d3989 | Elapsed: 1.68ms
The elapsed time shows the total time from when the READER publishes a message until the WRITER completes processing it. In the samples above, the end-to-end processing time ranges from 1.68ms to 1.83ms.
Usage
To run the demonstration:
python -m Controllers.Redis.Broadcast.implementations
This will start all three services in the chain and begin processing messages. Press Ctrl+C to stop the demonstration.
Implementation Details
The implementation uses:
- A singleton Redis Pub/Sub handler with publisher and subscriber capabilities
- Thread-based message processing
- JSON serialization for message passing
- Stage-based message processing to track progress through the chain
- Timing information to measure performance
Each service in the chain follows these steps:
- Subscribe to the appropriate channel
- Define a message handler function
- Process incoming messages based on their stage
- Publish processed messages to the next channel in the chain