FastAPI Backend Development Bootcamp · บทเรียน

การขยาย WebSockets ด้วยแบ็กเพลน Pub/Sub

เรียนรู้การกระจายข้อความ WebSocket ข้ามอินสแตนซ์ FastAPI หลายตัวโดยใช้แบ็กเพลน Redis pub/sub

บทเรียน 4 จาก 413 ขั้นตอน

การขยาย WebSockets ด้วยแบ็กเพลน Pub/Sub เป็นบทเรียน FastAPI Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน FastAPI Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Multi-Instance Problem

A single FastAPI process can hold WebSocket connections in memory. But in production you run multiple instances behind a load balancer.

A message arriving on instance A cannot reach a client connected to instance B unless the instances share state.

What is a Backplane?

A backplane is a shared messaging channel that every instance subscribes to. When one instance receives an event, it publishes to the backplane and all instances relay it to their local clients.

Redis Pub/Sub

Redis pub/sub is a popular backplane: a PUBLISH to a channel is delivered to every SUBSCRIBEr in real time.

PUBLISH chat:room1 "hello"
SUBSCRIBE chat:room1

Local Connection Manager

Each instance still tracks its own connected clients in memory.

class ConnectionManager:
    def __init__(self):
        self.active = []
    async def broadcast_local(self, msg):
        for ws in self.active:
            await ws.send_text(msg)

Publishing to Redis

Instead of broadcasting locally, publish incoming messages to a Redis channel.

import redis.asyncio as redis

r = redis.Redis()
async def publish(channel, message):
    await r.publish(channel, message)

Subscribing on Startup

Each instance opens a Redis subscription and listens in a background task.

async def listen():
    pubsub = r.pubsub()
    await pubsub.subscribe("chat:room1")
    async for msg in pubsub.listen():
        if msg["type"] == "message":
            await manager.broadcast_local(msg["data"])

Wiring It with Lifespan

Start the listener when the app boots using FastAPI lifespan events.

from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app):
    task = asyncio.create_task(listen())
    yield
    task.cancel()

The Full Message Flow

Client to instance A then A publishes to Redis then Redis fans out to A and B then each relays to its local clients.

Now any client receives any message regardless of which instance it connected to.

Per-Room Channels

Use one Redis channel per chat room so instances only receive messages relevant to their connected clients.

channel = "chat:" + room_id
await r.publish(channel, payload)

Avoiding Echo Loops

Because the publisher also receives its own message via the subscription, broadcast only from the listener, not directly, to avoid sending twice.

Sticky Sessions vs Stateless

With a backplane, instances become effectively stateless for messaging, so you no longer need sticky sessions on the load balancer for delivery to work.

Quick Check

Test your scaling knowledge.

Recap

You learned to scale real-time apps horizontally:

  • In-memory managers only reach local clients
  • A Redis pub/sub backplane fans messages out across instances
  • Use per-room channels and avoid echo loops

This pattern lets WebSocket apps scale to many instances behind a load balancer.

เริ่มต้นได้ฟรี

เรียนรู้ FastAPI Backend Development Bootcamp ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
21
บทเรียน
84

คำถามที่พบบ่อย

บทเรียน “การขยาย WebSockets ด้วยแบ็กเพลน Pub/Sub” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การขยาย WebSockets ด้วยแบ็กเพลน Pub/Sub” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส FastAPI Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การขยาย WebSockets ด้วยแบ็กเพลน Pub/Sub”

เรียนรู้การกระจายข้อความ WebSocket ข้ามอินสแตนซ์ FastAPI หลายตัวโดยใช้แบ็กเพลน Redis pub/sub คุณปฏิบัติ FastAPI Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน FastAPI Backend Development Bootcamp หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน FastAPI Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การขยาย WebSockets ด้วยแบ็กเพลน Pub/Sub” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน FastAPI Backend Development Bootcamp นี้ได้ไหม

ได้ บทเรียน FastAPI Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานโพรโทคอล WebSocket
  2. การใช้งาน WebSockets ใน FastAPI
  3. การสร้างแอปพลิเคชันแชทแบบเรียลไทม์
  4. การขยาย WebSockets ด้วยแบ็กเพลน Pub/Sub
← กลับไปที่ FastAPI Backend Development Bootcamp