Redis Pub/Sub in Python: Async Publish and Subscribe#
Use Redis Pub/Sub in Python to broadcast messages to applications listening on a channel. FastStream uses redis-py for Redis access and lets you declare asynchronous subscribers and publishers with decorators. Pub/Sub delivers to currently connected subscribers; use Redis Streams when you need stored messages, consumer groups or acknowledgement.
Run an async Python publisher and subscriber#
Install the Redis backend and CLI:
pip install "faststream[redis,cli]"
With Redis listening on localhost:6379, save this application as redis_pubsub.py:
1 2 3 4 5 6 7 8 9 10 | |
Start the application:
faststream run redis_pubsub:app
In a second terminal, listen for the response:
redis-cli SUBSCRIBE out-channel
In a third terminal, publish JSON matching the Python handler's arguments:
redis-cli PUBLISH in-channel '{"user": "Alice", "user_id": 1}'
The subscriber processes the message and the publisher sends its return value, User: 1 - Alice registered, to out-channel. FastStream wraps outgoing messages in its binary message format, so redis-cli shows envelope bytes containing this response rather than plain text alone. Another FastStream subscriber decodes the body automatically. Keep the subscriber connected before publishing: Pub/Sub does not replay earlier messages.
For publishing directly from Python, see channel publishing. For wildcard channels such as logs.*, see pattern subscriptions.
Cluster Support
RedisClusterBroker supports Pub/Sub via a synchronous RedisCluster client. See the Cluster docs.
Delivery and limitations#
Redis Pub/Sub broadcasts each published message to the subscribers currently connected to its channel. It suits live notifications and updates when missing a message during a disconnection is acceptable.
- No replay or persistence: channel messages are not retained for subscribers that connect later.
- No processing acknowledgement: Redis Pub/Sub uses at-most-once delivery. A subscriber that disconnects or fails to process a received message cannot ask Pub/Sub to redeliver it.
- Delivery order and processing order differ: Redis delivers Pub/Sub messages in publication order, but concurrent handler execution can complete out of order.
Choose Redis Streams for retained entries, consumer groups, acknowledgement and recovery of pending messages. Choose Redis Lists for a queue based on list operations.