Backplane Pub/Sub con Redis
Colleghi più istanze di server WebSocket usando un backplane Redis pub/sub, così i messaggi raggiungono i client indipendentemente dal nodo a cui si sono connessi.
Backplane Pub/Sub con Redis è una lezione WebSockets & Realtime Systems Programming gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento WebSockets & Realtime Systems Programming, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso WebSockets & Realtime Systems Programming include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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.
Impara WebSockets & Realtime Systems Programming con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 47
Domande Frequenti
La lezione «Backplane Pub/Sub con Redis» è gratuita?
Sì — il testo completo di «Backplane Pub/Sub con Redis» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso WebSockets & Realtime Systems Programming, passa a CoddyKit PRO. Il corso WebSockets & Realtime Systems Programming include 4 lezioni in totale.
Cosa imparerò in «Backplane Pub/Sub con Redis»?
Colleghi più istanze di server WebSocket usando un backplane Redis pub/sub, così i messaggi raggiungono i client indipendentemente dal nodo a cui si sono connessi. Eserciti WebSockets & Realtime Systems Programming con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare WebSockets & Realtime Systems Programming?
Non è richiesta alcuna esperienza precedente. WebSockets & Realtime Systems Programming su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Backplane Pub/Sub con Redis»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione WebSockets & Realtime Systems Programming?
Sì. Ogni lezione WebSockets & Realtime Systems Programming include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Strategie di scalabilità orizzontale
- Bilanciamento del carico per WebSocket
- Gestione distribuita dello stato
- Backplane Pub/Sub con Redis