C.W.K.
Stream
Lesson 02 of 07 · published

Horizontal Scaling

~12 min · production, scaling, redis

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Stateless HTTP scales by adding servers; WebSocket does not

An HTTP API can scale by adding identical servers behind a round-robin LB; each request lives entirely within its handler. WebSocket connections are persistent — adding a server adds capacity for new connections, but does nothing to bridge messages between connections on different servers. To get cross-server fan-out, you need a message bus.

Redis Pub/Sub is the standard answer

Track 4 introduced this. In production, every WebSocket server publishes outbound messages to Redis and subscribes to receive them; local fan-out happens on each server. Add servers freely; capacity scales linearly until Redis itself becomes the bottleneck (which happens at very large scale — at that point, look at Redis Cluster or Kafka).

Socket.IO ships an adapter

If you are using Socket.IO, scaling is one line: client_manager=socketio.AsyncRedisManager(...). Every sio.emit() automatically bridges across all servers. You write zero pub/sub code.

Code

Topology·text
  +----------+   +----------+   +----------+
  | Server 1 |   | Server 2 |   | Server 3 |
  | Users A,B|   | Users C,D|   | Users E,F|
  +----+-----+   +----+-----+   +----+-----+
       |              |              |
       +-------+------+-------+------+
               |    Redis Pub/Sub
               |  +-----------+
               +->| redis     |
                  +-----------+

  User A sends "hi" to room R
  Server 1 publishes "ws:room:R" -> Redis
  Redis fans out to all three servers
  Each server broadcasts locally to room R members
  -> A,B,C,D,E,F all receive "hi" within ms
Socket.IO Redis adapter·python
import socketio

sio = socketio.AsyncServer(
    async_mode='asgi',
    client_manager=socketio.AsyncRedisManager('redis://redis:6379'),
)

# That's the entire change. sio.emit() now spans all servers.

External links

Exercise

Run two Python Socket.IO servers behind one Redis. Open a client to each. Send a chat:message from server 1's client; confirm server 2's client receives it via the Redis adapter. Now stop Redis: does cross-server delivery stop? Restart Redis: does it resume?

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.