Designing a Real-Time Chat System
Walk through the system design of a real-time chat application, covering connections, message delivery, storage, and scaling.
Designing a Real-Time Chat System is a free System Design Basics for Backend Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the System Design Basics for Backend Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
Frequently asked questions
Is the “Designing a Real-Time Chat System” lesson free?
Yes — the full text of “Designing a Real-Time Chat System” is free to read here on the web, and the System Design Basics for Backend Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the System Design Basics for Backend Developers course, upgrade to CoddyKit PRO.
What will I learn in “Designing a Real-Time Chat System”?
Walk through the system design of a real-time chat application, covering connections, message delivery, storage, and scaling. You practise System Design Basics for Backend Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start System Design Basics for Backend Developers?
No prior experience is required. System Design Basics for Backend Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Designing a Real-Time Chat System” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this System Design Basics for Backend Developers lesson?
Yes. Every System Design Basics for Backend Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Designing a URL Shortener
- Building a Social Media Feed
- Scaling an E-commerce Platform
- Designing a Real-Time Chat System