How do you broadcast WebSocket messages to all clients across server nodes?

Tests WebSocket horizontal scaling and pub/sub backplanes. A strong answer names a broker like Redis, describes cross-node fan-out, and keeps connection state purely local.
WHAT THIS TESTS: This question probes your understanding of stateful connection fan-out in a distributed system. WebSocket connections are pinned to individual processes by the underlying TCP and async loop, so a naive broadcast only reaches clients on the same node. The interviewer wants to see if you recognize the architectural need for an external backplane to decouple message publishing from connection ownership, rather than treating sockets as globally addressable resources.
A GOOD ANSWER COVERS: First, acknowledge that each server node only knows about its own open sockets and that HTTP load balancers do not solve this by default. Second, propose a pub/sub broker such as Redis, Kafka, or Postgres as a backplane. Third, describe the exact flow: when a node receives a message, it publishes to a broker topic; every other node subscribes to that topic and forwards the payload only to its local clients. Fourth, emphasize that connection state remains in-memory on the node and only the small serialized payload traverses the backplane, keeping cross-node chatter minimal. Fifth, mention that libraries like fastapi-websocket-pubsub wrap this pattern by offering a PubSubEndpoint that accepts a broadcaster URI such as redis://host or postgres://host to sync events across instances transparently.
COMMON WRONG ANSWERS: Relying on IP hash sticky sessions as the complete solution, which breaks during rolling deploys or autoscaling events and still does not solve server-initiated broadcasts to all users. Suggesting a shared relational database that every node polls, which introduces high latency and violates real-time expectations. Proposing to serialize WebSocket objects or async generators into Redis directly, which fails because file descriptors and coroutine state cannot be marshaled across processes. Recommending a single giant node to avoid the problem entirely, which ignores horizontal scaling requirements.
LIKELY FOLLOW-UPS: How do you handle backplane downtime or network partition tolerance? What happens if a node crashes before it relays a message, and do you need at-least-once delivery? How would you shard topics to reduce fan-out noise on clusters with thousands of nodes? Would you use Redis Streams instead of Pub/Sub if you need persistence and replay? How do you prevent a slow consumer on one node from blocking the broadcast to others?
ONE CONCRETE EXAMPLE: In a FastAPI cluster running three uvicorn workers behind a load balancer, you initialize a PubSubEndpoint with a broadcaster string such as redis://localhost:6379. A client connects to worker one and subscribes to the alerts topic. When an HTTP POST hits worker two, worker two calls endpoint.publish on alerts. The Redis backplane propagates the event to all subscribed workers, including worker one, which then pushes the payload down the client's WebSocket. The client receives the message even though it never held a connection to worker two, and no socket state was ever moved between processes.
Source: pypi.org/project/fastapi-websocket-pubsub
Read the original → pypi.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.