การออกแบบระบบสนทนาแบบเรียลไทม์
เรียนรู้การออกแบบระบบของแอปพลิเคชันสนทนาแบบเรียลไทม์ ตั้งแต่การเชื่อมต่อ การส่งข้อความ การจัดเก็บ ไปจนถึงการขยายระบบ
การออกแบบระบบสนทนาแบบเรียลไทม์ เป็นบทเรียน System Design Basics for Backend Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน System Design Basics for Backend Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Problem
Design a chat system like WhatsApp or Slack: users send messages that appear on recipients' devices instantly, with history, online presence, and delivery receipts — at the scale of millions of concurrent users.
Requirements
Clarify scope first:
- Functional: 1:1 and group chat, message history, presence, delivery/read receipts
- Non-functional: low latency, high availability, ordered delivery, horizontal scale
Why Not Plain HTTP?
Classic HTTP is request-response — the server cannot push. Polling wastes resources and adds latency. Real-time chat needs a persistent, bidirectional connection.
WebSockets
WebSockets upgrade an HTTP connection into a long-lived, full-duplex channel. The server can push a message the instant it arrives. This is the backbone of real-time chat.
GET /chat HTTP/1.1
Upgrade: websocket
Connection: Upgrade
# After upgrade: server can push frames anytimeConnection Servers
A fleet of connection servers holds the open WebSockets. Since a sender and recipient may be connected to different servers, you need a way to route a message from one server to another.
Routing Between Servers
A presence/session store (e.g. Redis) maps each user to the connection server holding their socket. A message broker or pub/sub forwards messages to the right server so it can push to the recipient.
user_to_server = {
'alice': 'conn-3',
'bob': 'conn-7'
}
# alice -> bob: route message to conn-7Storing Messages
Chat is write-heavy with huge volume. A wide-column store like Cassandra suits it: partition by conversation id, cluster by timestamp, so fetching recent history in order is fast.
PRIMARY KEY ((conversation_id), message_ts)
WITH CLUSTERING ORDER BY (message_ts DESC)Delivery Guarantees
Use a per-user inbox queue and acknowledgements. Mark messages sent, delivered, and read. If a recipient is offline, queue the message and deliver when they reconnect; retries plus message IDs keep it idempotent.
Ordering
Messages must appear in a consistent order. Attach a monotonic sequence or timestamp per conversation. Clients sort by it, so even out-of-order network delivery is corrected on display.
import time
base = int(time.time() * 1000)
seq = [base, base + 1, base + 2]
print('ordered message ids:', seq)Group Chat at Scale
Group messages fan out to every member. For small groups, push directly; for large ones, write once to the conversation and let members pull, or fan out asynchronously via the broker to avoid a write storm.
Putting It Together
The full picture: clients hold WebSockets to connection servers, a presence store routes via pub/sub, messages persist in a wide-column store, and queues plus receipts handle offline delivery and ordering. Each piece scales horizontally.
Quick Check
Test your understanding of real-time chat design.
Recap
You designed a real-time chat system:
- WebSockets give persistent bidirectional connections
- A presence store plus pub/sub routes messages between connection servers
- A wide-column store holds ordered history
- Queues and receipts handle offline delivery; ordering uses per-conversation sequences
คำถามที่พบบ่อย
บทเรียน “การออกแบบระบบสนทนาแบบเรียลไทม์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การออกแบบระบบสนทนาแบบเรียลไทม์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส System Design Basics for Backend Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบระบบสนทนาแบบเรียลไทม์”
เรียนรู้การออกแบบระบบของแอปพลิเคชันสนทนาแบบเรียลไทม์ ตั้งแต่การเชื่อมต่อ การส่งข้อความ การจัดเก็บ ไปจนถึงการขยายระบบ คุณปฏิบัติ System Design Basics for Backend Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน System Design Basics for Backend Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน System Design Basics for Backend Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การออกแบบระบบสนทนาแบบเรียลไทม์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน System Design Basics for Backend Developers นี้ได้ไหม
ได้ บทเรียน System Design Basics for Backend Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบบริการย่อลิงก์
- การสร้างฟีดโซเชียลมีเดีย
- การขยายแพลตฟอร์มอีคอมเมิร์ซ
- การออกแบบระบบสนทนาแบบเรียลไทม์