Skip to content

Designing A Messaging System (WhatsApp / Zalo / Messenger)

1) Problem Clarification / Làm rõ bài toán

EN

We are designing a messaging platform where users can send/receive text messages in real time.

Clarifying questions:

  • One-to-one chat or group messaging?
  • Delivery guarantees?
  • Synchronization across devices?
  • Do we support message history?
  • Do we support online/offline status?
  • Multimedia messages?

VI

Thiết kế hệ thống nhắn tin realtime như WhatsApp/Zalo.

Câu hỏi cần làm rõ:

  • Chat 1-1 hay group?
  • Yêu cầu đảm bảo gửi/nhận?
  • Đồng bộ hóa nhiều thiết bị?
  • Có lưu lịch sử không?
  • Có trạng thái online/offline không?
  • Có media (ảnh, file, voice) không?

2) Requirements Definition / Xác định yêu cầu

EN – Functional

✔ Send/receive messages
✔ Delivery acknowledgment (“seen”)
✔ Online presence
✔ History sync

VI – Chức năng

✔ Gửi/nhận tin
✔ Trạng thái đã đọc/đã xem
✔ Trạng thái online/offline
✔ Đồng bộ lịch sử

EN – Non-functional

✔ Low latency (<100 ms)
✔ High availability
✔ Scale to millions of concurrent clients
✔ Reliability (no message loss)

VI – Phi chức năng

✔ Độ trễ thấp (<100ms)
✔ Sẵn sàng cao
✔ Vài triệu connections đồng thời
✔ Không mất tin nhắn

3) Scalability Estimation / Ước lượng tải

EN

  • 100M DAU
  • 2M peak concurrent connections
  • 1:10 send→read ratio
  • 1B messages/day

VI

  • 100M người dùng active
  • 2 triệu kết nối cùng lúc
  • Tỷ lệ gửi/nhận 1:10
  • 1 tỷ messages/ngày

4) High-Level Architecture / Kiến trúc tổng quan

Mobile Client → Gateway → Chat Service → Queue (Kafka) → Message Store (DB)
                                      ↘ Presence Service (Redis pub/sub)

EN

  • Gateway terminates websocket
  • Chat service validates & routes messages
  • Queue ensures reliable delivery
  • Store persists messages

VI

  • Gateway xử lý websocket
  • Chat service định tuyến tin nhắn
  • Queue bảo đảm reliability
  • Store lưu tin nhắn

5) Protocol Choice / Lựa chọn giao thức

EN

WebSocket → bidirectional persistent channel
AMPQ/MQTT are alternatives but WS fits mobile chat best

VI

WebSocket → kênh 2 chiều persistent
AMQP/MQTT cũng được nhưng WS hợp nhất cho chat mobile

6) Message Flow / Luồng xử lý tin nhắn

EN

Sender → Chat service
 → Kafka topic “inbox.<receiver_id>”
 → Consumer group delivers to device
 → Acknowledgment returned
 → Stored in DB
 → Sync engine updates secondary devices

VI

Sender → Chat service
 → Kafka topic “inbox.<receiver_id>”
 → Consumer deliver đến device
 → Reply ack
 → Ghi DB
 → Sync qua device khác

7) Data Model / Thiết kế DB

EN

Use append-only message log:

MESSAGE (
   msg_id BIGINT,
   sender_id BIGINT,
   receiver_id BIGINT,
   content TEXT,
   status ENUM(SENT,DELIVERED,SEEN),
   ts TIMESTAMP,
   shard_key (receiver_id)
)

VI

Table append-only:

MESSAGE (
   msg_id,
   sender_id,
   receiver_id,
   content,
   status,
   ts,
   shard_key
)

Shard theo receiver_id vì inbox consumer đọc theo người nhận.

8) Delivery Guarantees / Đảm bảo gửi/nhận

EN

We use at-least-once delivery + idempotent message processing:

  • Sender retries if no ACK
  • Consumer checks duplicate IDs

VI

Dùng at least once và idempotent:

  • Sender retry nếu mất ACK
  • Consumer check duplicate theo msg_id

9) Presence Tracking / Trạng thái online

EN

Redis pub/sub
Device register heartbeat
Push presence updates to subscribed users

VI

Redis pub/sub
Device gửi heartbeat định kỳ
Push trạng thái tới subscriber

10) Group Messaging / Tin nhắn nhóm

EN

Challenges: fan-out explosion
Solution:

  • Group metadata store
  • Kafka topic per group
  • Sequential consumer fan-out messages into member inbox queues

VI

Thách thức: fan-out lớn
Giải pháp:

  • Bảng member group
  • Kafka topic cho group
  • Consumer đọc và fan-out vào inbox từng người

11) Consistency vs Latency Trade-offs

EN

Message delivery must be fast but visible state (seen/read) can be eventually consistent.

VI

Tin nhắn phải đến ngay, nhưng trạng thái đã xem có thể eventual consistency.

12) Reliability & Failure Handling

EN

Failure scenarios:

  • Mobile disconnects → store pending messages
  • Queue outage → local buffer & retries
  • Chat node fail → session rebalance

Consumer offset ensures no loss.

VI

Các lỗi:

  • Client mất kết nối → lưu tin chờ
  • Queue chết → buffer & retry
  • Node Chat fail → chuyển session

Offset Kafka tránh mất tin.

13) Observability / Giám sát

EN

Metrics:

  • message delivery latency
  • lost message %
  • queue lag
  • connection churn
  • duplicate suppression rate

VI

Metrics:

  • độ trễ gửi nhận
  • % mất tin
  • queue lag
  • churn connection
  • tỷ lệ suppress duplicate

14) Future Extensions / Mở rộng tương lai

EN

  • End-to-end encryption
  • Push-to-talk voice messaging
  • Multimedia CDN integration
  • Smart ordering with ML ranking

VI

  • Mã hóa end-to-end
  • Tin nhắn voice
  • CDN media
  • Ranking thông minh bằng ML
Published inAll

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *