Design a Chat System
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2019-06-09
Problem Statement
Designing a real-time chat system that allows users to send and receive messages reliably, supporting 1:1 direct chats and group chats. The system should scale to millions of concurrent users while maintaining low latency, high availability, and message durability.
✅ Functional Requirements
1.<span></span>User Management
2.<span></span>Register/login users.
3.<span></span>Presence indicator (online/offline/last seen).
4.<span></span>Messaging
5.<span></span>1:1 chat between two users.
6.<span></span>Group chat with multiple participants.
7.<span></span>Support for text, media (images, files, voice notes, videos).
8.<span></span>Typing indicators & read receipts.
9.<span></span>Message search in history.
10.<span></span>Delivery Semantics
11.<span></span>At-least-once delivery.
12.<span></span>Ordering guarantees within a conversation.
13.<span></span>Offline message storage & sync when user comes online.
14.<span></span>Notifications
15.<span></span>Push notifications for new messages when app is in background.
16.<span></span>Security
17.<span></span>End-to-end encryption for message confidentiality.
18.<span></span>Authentication & authorization (users cannot read others’ messages).
⚙️ Non-Functional Requirements
1.<span></span>Scalability → Millions of concurrent users.
2.<span></span>Latency → P95 message delivery under 200ms.
3.<span></span>Durability → No message loss, even if servers crash.
4.<span></span>High availability → 99.99% uptime.
5.<span></span>Consistency trade-offs → Eventual consistency is acceptable for read receipts, but not for message delivery.
🔐 Constraints & Assumptions
1.<span></span>Messages can be large (up to 25 MB media).
2.<span></span>Users may belong to thousands of groups (Slack-like use case).
3.<span></span>System should support multi-device sync (user logged in on web + mobile).
4.<span></span>A user may send up to 50 messages/sec peak (group spam scenario).
🧱 Key Components to Design
1.<span></span>API Gateway → Authentication, rate-limiting, routing.
2.<span></span>Chat Service → Core messaging logic (send, receive, delivery tracking).
3.<span></span>Message Queue (Kafka/Pulsar) → Reliable, ordered message delivery.
4.<span></span>Storage →
5.<span></span>Hot storage: Redis/Memcached for recent chats.
6.<span></span>Cold storage: Cassandra/DynamoDB for history.
7.<span></span>Blob store (S3/GCS) for media.
8.<span></span>Notification Service → Push notifications, email fallbacks.
9.<span></span>Presence Service → Online/offline tracking (pub-sub model).
10.<span></span>Search & Indexing → ElasticSearch for message search.
11.<span></span>Encryption Layer → End-to-end encryption, key management.
📈 SLAs & Success Metrics
1.<span></span>Message delivery success rate: > 99.99%.
2.<span></span>Latency: <200ms for text, <500ms for media.
3.<span></span>Durability: Zero message loss, even during failures.
4.<span></span>Scale: 100M DAU (daily active users).
🔄 Trade-offs to Discuss
1.<span></span>Message ordering (per-conversation ordering vs. global ordering).
2.<span></span>Database choice (SQL for strong consistency vs. NoSQL for scalability).
3.<span></span>At-least-once vs. exactly-once delivery (complexity vs. reliability).
4.<span></span>Multi-device sync (stateful sessions vs. stateless w/ sync).
👉 This is an HLD round problem statement. The expectation is that you’ll:
1.<span></span>Draw an architecture diagram (API Gateway → Chat Service → Queue → Storage).
2.<span></span>Discuss scalability bottlenecks (hot partition for popular group chats, fan-out problem).
3.<span></span>Explore trade-offs (latency vs. reliability, storage vs. cost).