Backplane Pub/Sub con Redis
Conecte varias instancias de servidores WebSocket mediante un backplane pub/sub de Redis para que los mensajes lleguen a los clientes independientemente del nodo al que se hayan conectado.
Backplane Pub/Sub con Redis es una lección gratuita de WebSockets & Realtime Systems Programming en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de WebSockets & Realtime Systems Programming, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de WebSockets & Realtime Systems Programming incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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.
Preguntas frecuentes
¿La lección «Backplane Pub/Sub con Redis» es gratis?
Sí — el texto completo de «Backplane Pub/Sub con Redis» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de WebSockets & Realtime Systems Programming, actualiza a CoddyKit PRO. El curso de WebSockets & Realtime Systems Programming incluye 4 lecciones en total.
¿Qué aprenderé en «Backplane Pub/Sub con Redis»?
Conecte varias instancias de servidores WebSocket mediante un backplane pub/sub de Redis para que los mensajes lleguen a los clientes independientemente del nodo al que se hayan conectado. Practicas WebSockets & Realtime Systems Programming con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar WebSockets & Realtime Systems Programming?
No se requiere experiencia previa. WebSockets & Realtime Systems Programming en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.
¿Cuánto tiempo toma la lección «Backplane Pub/Sub con Redis»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de WebSockets & Realtime Systems Programming?
Sí. Cada lección de WebSockets & Realtime Systems Programming incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Estrategias de escalado horizontal
- Balanceo de carga de WebSockets
- Gestión distribuida del estado
- Backplane Pub/Sub con Redis