Scaling Real-time Events with Redis Pub/Sub
For a real-time event discovery platform like BuzzMap, latency is the enemy. When thousands of users are moving across a map, receiving geolocation-based updates every second, the backend must broadcast events with sub-50ms precision.
The Horizontal Scaling Wall
Standard WebSocket libraries like Socket.io work perfectly on a single server. However, as traffic grows, you must scale horizontally. The problem? If User A is connected to Server 1 and User B is connected to Server 2, they cannot "see" each other's events. The servers are isolated silos.
Solving the Silo with Redis Pub/Sub
To bridge this gap, I implemented a centralized message broker using Redis Pub/Sub. Instead of servers trying to talk to each other, they all subscribe to specific "channels" on a lightning-fast Redis instance.
// Broadcasting an event across the entire server swarm
const redis = require('redis');
const publisher = redis.createClient();
function broadcastToCluster(room, eventData) {
// 1. Send the event to the global Redis bus
publisher.publish('map_events', JSON.stringify({
room: room,
data: eventData
}));
}
// Every Node.js instance listens for this bus
subscriber.on('message', (channel, message) => {
const payload = JSON.parse(message);
// 2. Only broadcast to clients actually connected to THIS instance
io.to(payload.room).emit('event', payload.data);
});Geolocation Sharding
Sending every map update to every server would create a "broadcast storm." To optimize, we implemented Geospatial Sharding. Events were published to channels based on geographic quadrants (e.g., map_events:nyc:lower_manhattan).
Servers only subscribed to the channels that their active users were currently viewing, reducing unneccesary network overhead by over 85%.
High-Availability Metrics
Production stats from the US deployment
Latency
<50ms
Max Conn
10k/sec
Uptime
99.99%
Bus Speed
1M msg/s
Modern real-time systems aren't built on single servers anymore. They are built on distributed message buses. By leveraging Redis primitives, we transformed a fragile Node.js app into a resilient, globally-scalable infrastructure capable of handling high-velocity US production traffic.