AboutBlogContact
Backend EngineeringDecember 5, 2010 2 min read 24

Redis Pub/Sub: Building Real-Time Scalable Chat Applications (2010)

AunimedaAunimeda
📋 Table of Contents

Redis Pub/Sub: Building Real-Time Scalable Chat Applications

It’s late 2010, and "Real-Time" is the new mandate for web apps. We want chat, live notifications, and collaborative editing. Node.js and Socket.io make this easy on a single server. But what happens when you have so many users that you need to scale to two, three, or ten servers?

The problem is that a user connected to Server A won't receive a message sent by a user on Server B. We need a way to synchronize messages across our entire infrastructure. Enter Redis Pub/Sub.

How Pub/Sub Works

Redis (Remote Dictionary Server) is famous for being a fast key-value store. But with version 2.0, it introduced a messaging pattern where "Publishers" send messages to "Channels," and "Subscribers" receive them.

Implementing the Message Bus in Node.js

We can use Redis as the backbone for our Socket.io application. Every time a message comes in from a client, we publish it to Redis. Every server is also listening to Redis for new messages to push out to its connected clients.

var redis = require('redis');
var io = require('socket.io').listen(80);

// Create two Redis clients: one for sub, one for pub
var sub = redis.createClient();
var pub = redis.createClient();

sub.subscribe('chat_messages');

// When we get a message from Redis...
sub.on('message', function(channel, message) {
    // ...broadcast it to all clients connected to THIS server
    io.sockets.emit('message', message);
});

io.sockets.on('connection', function(socket) {
    socket.on('message', function(data) {
        // When a client sends a message, publish it to Redis
        pub.publish('chat_messages', data);
    });
});

Why This Scales

Because Redis is incredibly fast (handling 100k+ operations per second), it can act as the "Source of Truth" for your entire cluster. It doesn't matter which server a user is connected to; they are all connected to the same Redis channel.

Beyond Chat: Real-Time Analytics

Pub/Sub isn't just for chat. You can use it to push live updates to an analytics dashboard or to trigger background tasks (like image processing) across a pool of worker servers.

In 2010, we are moving away from "Request/Response" and toward "Streams of Events." Redis Pub/Sub is the glue that makes these complex, distributed systems feel as simple as a single-threaded application.

Read Also

PostgreSQL EXPLAIN ANALYZE: Reading Query Plans Like a Senior DBAaunimeda
Backend Engineering

PostgreSQL EXPLAIN ANALYZE: Reading Query Plans Like a Senior DBA

Stop guessing why your queries are slow. Learn to read PostgreSQL query plans at a level where you can actually fix problems — seq scans, join strategies, row estimate disasters, and the N+1 you didn't know was hiding in your ORM output.

Redis Data Structures in Production: Beyond SET and GETaunimeda
Backend Engineering

Redis Data Structures in Production: Beyond SET and GET

Most teams use Redis as a glorified hash map. This guide covers the data structures that solve real production problems — sorted sets for leaderboards, streams for durable event queues, HyperLogLog for UV counting at scale, and Lua scripts for atomic operations you can't otherwise do safely.

Drizzle ORM vs Prisma in 2026: A Production Engineer's Honest Comparisonaunimeda
Backend Engineering

Drizzle ORM vs Prisma in 2026: A Production Engineer's Honest Comparison

Both ORMs are genuinely good. The choice depends on your migration discipline, whether you hit Prisma's edge runtime limitations, and how much you care about the SQL Drizzle generates vs the DX Prisma provides. Here's the honest comparison — same query, both ORMs, real trade-offs.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Get Consultation All articles