Case Study: WhatsApp / Messenger
২ বিলিয়ন user, real-time, end-to-end encrypted।
WhatsApp — Bangladesh-এ প্রায় সবার phone-এ। ২ বিলিয়ন active user, প্রতিদিন ১০০+ বিলিয়ন message। ১০০-জনের team এই scale handle করত। কীভাবে? Engineering brilliance + সঠিক architecture choice।
Step 1: Requirements
Functional
- 1-on-1 chat।
- Group chat।
- Online/last seen status।
- Read receipts (✓✓)।
- Media sharing (image, video)।
- Voice/video call।
- End-to-end encryption।
Non-Functional
- Real-time delivery (<100ms)।
- Massive scale (2B+ users)।
- High availability।
- Reliable delivery (no lost message)।
- Low data usage।
Step 2: Capacity Estimation
DAU: 2B
Messages/day: 100B
Messages/sec: 100B / 86400 ≈ 1.16M msg/sec
Per message: ~100 bytes (text)
Per day: 100B × 100 bytes = 10TB messages
Plus media: 5x = 50TB/day
Concurrent connections (WebSocket): ~500M
Connection servers: ~250 (2M conn each — Erlang)
Step 3: API Design
// Client connects via WebSocket
WS connection: wss://chat.whatsapp.com
// Send message
SEND { to: userId, content: "..." }
// Server pushes
RECEIVE { from: userId, content: "...", timestamp }
// Status updates
TYPING, READ, ONLINE
Step 4: Data Model
User: { id, phone, name, status, last_seen }
Conversation: { id, type (1on1/group), participants[] }
Message: {
id, conv_id, sender_id,
encrypted_content, timestamp,
delivery_status
}
Group: { id, name, admin, members[] }
Message history client-এ store; server temporary।
Step 5: Architecture
[Mobile Client]
↓ WebSocket (TLS)
[Load Balancer] (geo-routed)
↓
[Chat Server (Erlang)] ←→ [Cluster Coordination]
↓
[Message Queue] (Kafka)
↓
[Storage]: Cassandra (history), Redis (online state)
[Media]: CDN + Object Storage (S3)
কেন Erlang?
WhatsApp Erlang/BEAM VM-এ চলে। কারণ:
- Massive concurrency: Lightweight processes — ২ million per server।
- Fault tolerance: "Let it crash" philosophy + supervisor tree।
- Hot code reload: Live update — no downtime।
- Built for telecom: Real-time messaging-এর native fit।
Result: ১০-২০ engineer ১+ billion user manage।
Message Delivery Flow
1-on-1 Chat
- User A "Hello" পাঠাল।
- App-এ WebSocket-এ encrypted message।
- Chat server receive — encrypted message store (offline delivery-এর জন্য)।
- User B online হলে — push notification + message।
- User B-এর app encrypted message decrypt।
- Acknowledgment back: ✓ (sent) → ✓✓ (delivered) → ✓✓ blue (read)।
- Both online হলে — server message delete (privacy + storage)।
Offline Delivery
- User offline → message queued।
- Online হলে → APNs/FCM push notification।
- App connect → queued messages deliver।
End-to-End Encryption
Signal Protocol (Open Whisper Systems):
- Identity keys: Long-term per user।
- Pre-keys: Short-term, server-stored।
- Session keys: Per-conversation।
- Forward secrecy: Past message secure থাকে — current key compromise হলেও।
⚠️ গুরুত্বপূর্ণ: WhatsApp server message decrypt করতে পারে না — only sender + recipient।
Online Presence
- WebSocket connect = online state Redis-এ।
- Disconnect = offline।
- Last seen timestamp store।
- Privacy setting respect।
Group Chat
- Group → message → server fan-out to all members।
- Each member-এর encrypted copy।
- Maximum 1024 members (current)।
- Admin permissions।
Media Handling
- Sender file upload — temporary URL।
- Encrypted upload to CDN/S3।
- Message in chat = link + key।
- Receiver download + decrypt।
- Compression (image, video) bandwidth save।
Voice/Video Call
- WebRTC — peer-to-peer typically।
- NAT traversal: STUN/TURN servers।
- Codec: Opus (audio), VP8/H.264 (video)।
- Encrypted media stream।
Scaling Challenges
Connection Scale
- 500M concurrent WebSocket।
- Erlang advantage huge here।
- Sticky session per user।
Storage
- Server temporary storage only।
- Client device-এ history।
- Cassandra cluster — undelivered messages।
Geographic Distribution
- Multiple data center।
- User nearest server connect।
- Cross-DC routing for distant users।
Key Trade-offs
- Server-side message store vs E2E encryption privacy।
- Real-time push vs battery life।
- Delivery guarantee vs simplicity।
- Group size limit vs broadcast cost।
Real World Numbers
- ২ billion+ users।
- ১০০+ billion messages/day।
- Erlang server fleet — small relatively।
- WhatsApp ২০১৪-এ FB acquire (€19B)।
- ৫০-জন engineer-এ ৫০০M+ user reach করেছিল।
Engineering Lessons
- Right tool: Erlang for messaging.
- Simplicity scales.
- Server-light, client-heavy.
- Encryption from the start.
- Reliable > feature-rich.
📌 চ্যাপ্টার সারমর্ম
- WhatsApp = WebSocket + Erlang + E2E encryption।
- Server temporary storage; client persistent।
- Signal Protocol — forward secrecy।
- Group chat = server fan-out + per-member encryption।
- Media = CDN + encrypted upload।