RedisによるPub/Subバックプレーン
RedisのPub/Subバックプレーンで複数のWebSocketサーバーインスタンスを接続し、どのノードに接続したクライアントにもメッセージを届けます。
「RedisによるPub/Subバックプレーン」はCoddyKit上の無料WebSockets & Realtime Systems Programmingレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはWebSockets & Realtime Systems Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 WebSockets & Realtime Systems Programmingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
AI チューターと学ぶ WebSockets & Realtime Systems Programming — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 47
よくある質問
「RedisによるPub/Subバックプレーン」レッスンは無料ですか?
はい。「RedisによるPub/Subバックプレーン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、WebSockets & Realtime Systems Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 WebSockets & Realtime Systems Programmingコースには全4レッスンが含まれています。
「RedisによるPub/Subバックプレーン」で何を学びますか?
RedisのPub/Subバックプレーンで複数のWebSocketサーバーインスタンスを接続し、どのノードに接続したクライアントにもメッセージを届けます。 ブラウザで直接実行するハンズオンコードでWebSockets & Realtime Systems Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
WebSockets & Realtime Systems Programmingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのWebSockets & Realtime Systems Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「RedisによるPub/Subバックプレーン」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このWebSockets & Realtime Systems Programmingレッスンでコードを書いて実行できますか?
はい。すべてのWebSockets & Realtime Systems Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 水平スケーリング戦略
- WebSocketのロードバランシング
- 分散状態管理
- RedisによるPub/Subバックプレーン