0Pricing
FastAPI Backend Development Bootcamp · Lesson

Scaling WebSockets with a Pub/Sub Backplane

Learn how to broadcast WebSocket messages across multiple FastAPI instances using a Redis pub/sub backplane.

Scaling WebSockets with a Pub/Sub Backplane is a free FastAPI Backend Development Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the FastAPI Backend Development Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Multi-Instance Problem

A single FastAPI process can hold WebSocket connections in memory. But in production you run multiple instances behind a load balancer.

A message arriving on instance A cannot reach a client connected to instance B unless the instances share state.

What is a Backplane?

A backplane is a shared messaging channel that every instance subscribes to. When one instance receives an event, it publishes to the backplane and all instances relay it to their local clients.

Redis Pub/Sub

Redis pub/sub is a popular backplane: a PUBLISH to a channel is delivered to every SUBSCRIBEr in real time.

PUBLISH chat:room1 "hello"
SUBSCRIBE chat:room1

Local Connection Manager

Each instance still tracks its own connected clients in memory.

class ConnectionManager:
    def __init__(self):
        self.active = []
    async def broadcast_local(self, msg):
        for ws in self.active:
            await ws.send_text(msg)

Publishing to Redis

Instead of broadcasting locally, publish incoming messages to a Redis channel.

import redis.asyncio as redis

r = redis.Redis()
async def publish(channel, message):
    await r.publish(channel, message)

Subscribing on Startup

Each instance opens a Redis subscription and listens in a background task.

async def listen():
    pubsub = r.pubsub()
    await pubsub.subscribe("chat:room1")
    async for msg in pubsub.listen():
        if msg["type"] == "message":
            await manager.broadcast_local(msg["data"])

Wiring It with Lifespan

Start the listener when the app boots using FastAPI lifespan events.

from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app):
    task = asyncio.create_task(listen())
    yield
    task.cancel()

The Full Message Flow

Client to instance A then A publishes to Redis then Redis fans out to A and B then each relays to its local clients.

Now any client receives any message regardless of which instance it connected to.

Per-Room Channels

Use one Redis channel per chat room so instances only receive messages relevant to their connected clients.

channel = "chat:" + room_id
await r.publish(channel, payload)

Avoiding Echo Loops

Because the publisher also receives its own message via the subscription, broadcast only from the listener, not directly, to avoid sending twice.

Sticky Sessions vs Stateless

With a backplane, instances become effectively stateless for messaging, so you no longer need sticky sessions on the load balancer for delivery to work.

Quick Check

Test your scaling knowledge.

Recap

You learned to scale real-time apps horizontally:

  • In-memory managers only reach local clients
  • A Redis pub/sub backplane fans messages out across instances
  • Use per-room channels and avoid echo loops

This pattern lets WebSocket apps scale to many instances behind a load balancer.

Frequently asked questions

Is the “Scaling WebSockets with a Pub/Sub Backplane” lesson free?

Yes — the full text of “Scaling WebSockets with a Pub/Sub Backplane” is free to read here on the web, and the FastAPI Backend Development Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the FastAPI Backend Development Bootcamp course, upgrade to CoddyKit PRO.

What will I learn in “Scaling WebSockets with a Pub/Sub Backplane”?

Learn how to broadcast WebSocket messages across multiple FastAPI instances using a Redis pub/sub backplane. You practise FastAPI Backend Development Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start FastAPI Backend Development Bootcamp?

No prior experience is required. FastAPI Backend Development Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Scaling WebSockets with a Pub/Sub Backplane” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this FastAPI Backend Development Bootcamp lesson?

Yes. Every FastAPI Backend Development Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. WebSocket Protocol Fundamentals
  2. Implementing WebSockets in FastAPI
  3. Building a Real-time Chat Application
  4. Scaling WebSockets with a Pub/Sub Backplane
← Back to FastAPI Backend Development Bootcamp