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's really being asked
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.
The full answer
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.
The mistakes people make
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.
What usually comes next
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?
A 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.
Interview question
In a horizontally scaled WebSocket system, how does a pub/sub backplane enable broadcast to clients across all server nodes?
- a.By allowing nodes to serialize their WebSocket objects and async generators into the backplane for other nodes to deserialize
- b.By maintaining IP hash sticky sessions that redirect clients to whichever node originated the broadcast
- c.By propagating only the serialized payload to all nodes, which then forward it to their local connectionsCorrect
- d.By acting as a shared database that each node polls continuously for messages intended for its clients
Why? this is the answer
The backplane distributes only the serialized payload so that each node can forward it to its own locally held connections without sharing socket state. Serializing WebSocket objects or file descriptors is impossible because TCP connections and coroutine state are bound to their host process.
Just read this? Test yourself on what you have been reading.
Read the original → pypi.org
- #websockets
- #fastapi
- #redis
- #pub-sub
- #distributed-systems
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles