System Design
Interviews
Architecture
Backend
Scalability
Ask anything about this article
Hi! I've read this article.
What would you like to know?
@farhan
This is one of the most commonly asked system design questions. Here's a comprehensive answer that will impress any interviewer.
┌──────────┐ ┌──────────────┐ ┌──────────────┐
│ Client │────▶│ API │────▶│ Message │
│ (App) │◀────│ Gateway │◀────│ Service │
└──────────┘ └──────┬───────┘ └──────┬───────┘
│ │
┌──────┴───────┐ ┌──────┴───────┐
│ WebSocket │ │ Message │
│ Manager │ │ Queue │
└──────┬───────┘ └──────┬───────┘
│ │
┌──────┴───────┐ ┌──────┴───────┐
│ Presence │ │ Database │
│ Service │ │ (Cassandra) │
└──────────────┘ └──────────────┘
sql-- Messages table (partitioned by conversation)
CREATE TABLE messages (
conversation_id UUID,
message_id TIMEUUID,
sender_id UUID,
content TEXT,
content_type TEXT, -- 'text', 'image', 'video'
media_url TEXT,
status TEXT, -- 'sent', 'delivered', 'read'
created_at TIMESTAMP,
PRIMARY KEY (conversation_id, message_id)
) WITH CLUSTERING ORDER BY (message_id DESC);
Message Status Flow:
✓ SENT → Stored in server database
✓✓ DELIVERED → Received by recipient's device
✓✓ READ → Recipient opened the chat (blue ticks)
SET user:{userId}:online true EX 30 // Expires in 30 seconds
// Client sends heartbeat every 25 seconds to renew
| Component | Technology | Reasoning |
|---|---|---|
| Messages DB | Cassandra | Write-heavy, partitioned by conversation |
| User DB | PostgreSQL | Relational data, strong consistency |
| Cache | Redis | Presence, recent messages, sessions |
| Media | S3 + CDN | Large files, global distribution |
| Queue | Kafka | Async message processing, reliability |
| Search | Elasticsearch | Message search within chats |
100B messages / 86,400 seconds = ~1.15M messages/second
Per message: ~1KB average
Storage: 100B × 1KB = 100TB/day = 36.5PB/year
Solution:
- Cassandra cluster with 500+ nodes
- Partition by conversation_id
- TTL on messages older than 30 days (archive to cold storage)
Signal Protocol (used by WhatsApp):
1. Key Exchange (X3DH):
- Each user generates identity key pair
- Pre-keys shared via server
2. Message Encryption:
- Double Ratchet Algorithm
- New key for every message
- Forward secrecy: compromised key can't decrypt past messages
3. Server sees:
❌ Message content
✅ Sender, recipient, timestamp
✅ Encrypted blob
This design handles WhatsApp-level scale. Practice explaining it in 35-40 minutes.