Send across processes with Redis¶
A notification from a background worker, a chat message from a cron job, the same app on four replicas: each needs every process on one channel layer. In memory, a broadcast stays in the process that sent it. With Redis it reaches every server and every worker.
1. Run Redis¶
Every starter has a docker-compose.yml for this:
2. Point the server at it¶
The starters already switch to Redis when REDIS_URL is set:
The switch, in src/app.ts:
The switch, in app/layers.py:
On Django, set CHANNEL_LAYERS to a Redis layer the same way.
3. Send from another process¶
Each starter has a small sender. Run it with the same REDIS_URL while the browser is
open:
Chris's tab shows it, although the sender never opened a connection. Your own workers
do the same: set up the same layer, then call the kit's send function, such as
notifyUser, post_to_room or PostToRoom.
Mix languages¶
Servers in different languages share a layer when they use the same Redis and the same
prefix. A Go worker can notify browsers connected to a Python server; each kit speaks
the same messages on every server.
State that must be shared too¶
The layer carries messages. Kits that keep state need a shared store as well:
- Presence: add
redis-presence-store, or each process sees only its own members. - Chat history: give room-chat a database-backed store.
- Agent runs: the replay buffer, the one-run-per-thread claim and cancelling stay with the process running the run. Back the claim with Redis before scaling out; the ag-ui page shows how.