Progettare un sistema di chat in tempo reale
Esamini la progettazione di sistema di un’applicazione di chat in tempo reale, occupandosi di connessioni, consegna dei messaggi, archiviazione e scalabilità.
Progettare un sistema di chat in tempo reale è una lezione System Design Basics for Backend Developers gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento System Design Basics for Backend Developers, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso System Design Basics for Backend Developers include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Domande Frequenti
La lezione «Progettare un sistema di chat in tempo reale» è gratuita?
Sì — il testo completo di «Progettare un sistema di chat in tempo reale» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso System Design Basics for Backend Developers, passa a CoddyKit PRO. Il corso System Design Basics for Backend Developers include 4 lezioni in totale.
Cosa imparerò in «Progettare un sistema di chat in tempo reale»?
Esamini la progettazione di sistema di un’applicazione di chat in tempo reale, occupandosi di connessioni, consegna dei messaggi, archiviazione e scalabilità. Eserciti System Design Basics for Backend Developers con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare System Design Basics for Backend Developers?
Non è richiesta alcuna esperienza precedente. System Design Basics for Backend Developers su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Progettare un sistema di chat in tempo reale»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione System Design Basics for Backend Developers?
Sì. Ogni lezione System Design Basics for Backend Developers include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Progettare un URL shortener
- Creare un feed per i social media
- Scalare una piattaforma e-commerce
- Progettare un sistema di chat in tempo reale