リアルタイムチャットシステムの設計
リアルタイムチャットアプリケーションのシステム設計を順に見ていき、接続、メッセージ配信、ストレージ、スケーリングを扱います。
「リアルタイムチャットシステムの設計」はCoddyKit上の無料System Design Basics for Backend Developersレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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 チューターと学ぶ System Design Basics for Backend Developers — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「リアルタイムチャットシステムの設計」レッスンは無料ですか?
はい。「リアルタイムチャットシステムの設計」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、System Design Basics for Backend Developersコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 System Design Basics for Backend Developersコースには全4レッスンが含まれています。
「リアルタイムチャットシステムの設計」で何を学びますか?
リアルタイムチャットアプリケーションのシステム設計を順に見ていき、接続、メッセージ配信、ストレージ、スケーリングを扱います。 ブラウザで直接実行するハンズオンコードでSystem Design Basics for Backend Developersを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
System Design Basics for Backend Developersを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSystem Design Basics for Backend Developersは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「リアルタイムチャットシステムの設計」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSystem Design Basics for Backend Developersレッスンでコードを書いて実行できますか?
はい。すべてのSystem Design Basics for Backend Developersレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- URL短縮サービスの設計
- ソーシャルメディアフィードの構築
- Eコマースプラットフォームのスケーリング
- リアルタイムチャットシステムの設計