0Pricing
WebSockets & Realtime Systems Programming · Lesson

Pub/Sub Backplane with Redis

Connect multiple WebSocket server instances using a Redis pub/sub backplane so messages reach clients regardless of which node they connected to.

Pub/Sub Backplane with Redis is a free WebSockets & Realtime Systems Programming 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 WebSockets & Realtime Systems Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Multi-Instance Problem

When you scale horizontally, clients connect to different server instances. A message published on node A will not reach a client connected to node B unless the nodes share it.

What a Backplane Does

A backplane is a shared channel every instance subscribes to. When one node receives a message, it publishes to the backplane, and all nodes deliver it to their local clients.

Why Redis Pub/Sub

Redis pub/sub is fast, simple, and widely available. Publishers send to a channel; all subscribers receive it instantly, making it a natural backplane.

Two Redis Connections

Redis requires separate clients for publishing and subscribing, because a subscribed connection cannot run normal commands.

const pub = redis.createClient();
const sub = redis.createClient();
await pub.connect();
await sub.connect();

Subscribing on Startup

Each instance subscribes to the shared channel when it boots.

await sub.subscribe('ws-broadcast', (message) => {
  deliverLocally(JSON.parse(message));
});

Publishing Across Nodes

Instead of broadcasting only to local sockets, publish to Redis so every node hears it.

function broadcast(payload) {
  pub.publish('ws-broadcast', JSON.stringify(payload));
}

Delivering Locally

The subscribe callback fans the message out to the clients connected to that specific instance.

function deliverLocally(payload) {
  for (const client of localClients) {
    if (client.readyState === 1) client.send(JSON.stringify(payload));
  }
}

Avoiding Echo Loops

Since the publishing node also receives its own message via subscribe, deliver only through the backplane path so each message is sent once, not twice.

Scoping by Room

For room-based apps, include the room in the payload and let each node deliver only to its local members of that room, keeping the backplane channel count small.

pub.publish('ws-broadcast', JSON.stringify({ room: 'chat-1', data }));

Limits of Redis Pub/Sub

Redis pub/sub is fire-and-forget: offline subscribers miss messages, and there is no persistence. For durability or replay, consider Redis Streams or a dedicated broker.

Best Practices

Run a healthy backplane:

  • Use separate pub and sub clients
  • Route all broadcasts through the backplane
  • Scope messages by room to reduce work
  • Know that pub/sub does not persist messages

Quick Check

Test your backplane knowledge.

Recap

You wired up a Redis pub/sub backplane:

  • Use separate publish and subscribe clients
  • Publish broadcasts to a shared channel
  • Each node delivers to its local clients
  • Scope by room and accept fire-and-forget limits

Your WebSocket app now scales across many instances.

Frequently asked questions

Is the “Pub/Sub Backplane with Redis” lesson free?

Yes — the full text of “Pub/Sub Backplane with Redis” is free to read here on the web, and the WebSockets & Realtime Systems Programming 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 WebSockets & Realtime Systems Programming course, upgrade to CoddyKit PRO.

What will I learn in “Pub/Sub Backplane with Redis”?

Connect multiple WebSocket server instances using a Redis pub/sub backplane so messages reach clients regardless of which node they connected to. You practise WebSockets & Realtime Systems Programming 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 WebSockets & Realtime Systems Programming?

No prior experience is required. WebSockets & Realtime Systems Programming 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 “Pub/Sub Backplane with Redis” 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 WebSockets & Realtime Systems Programming lesson?

Yes. Every WebSockets & Realtime Systems Programming 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. Horizontal Scaling Strategies
  2. Load Balancing WebSockets
  3. Distributed State Management
  4. Pub/Sub Backplane with Redis
← Back to WebSockets & Realtime Systems Programming