A scalable, fault-tolerant, distributed messaging platform inspired by WhatsApp.
- One-to-One Messaging
- Group Messaging
- Online Presence
- Message Delivery Acknowledgements
- Media Sharing
- Push Notifications
- Audio Calling
- Video Calling
- Group Video Calling
- Multi-Device Synchronization
- Problem Statement
- Functional Requirements
- Non-Functional Requirements
- Capacity Estimation
- Core APIs
- Data Model
- High-Level Architecture
- WebSocket Architecture
- Service Discovery
- Presence Service
- Message Routing
- Online Message Delivery
- Offline Message Delivery
- Message Acknowledgements
- Multi-Device Synchronization
- Group Messaging
- Media Sharing
- Push Notification Service
- Audio Calling
- Video Calling
- Group Video Calling
- Scaling Strategy
- Reliability
- Security
- Observability
- Future Improvements
Design a globally distributed messaging platform capable of supporting millions of users simultaneously.
Users should be able to:
- Send messages instantly
- Create groups
- Share media
- See online status
- Receive notifications
- Synchronize across multiple devices
- Make voice calls
- Make video calls
The system must provide:
- Low latency
- High availability
- High durability
- Horizontal scalability
Support:
- One-to-one messaging
- Group messaging
- Rich media messages
- Message persistence
Users can view:
- Online
- Offline
- Last Seen
Messages support:
SENT
DELIVERED
READ
Notify users about:
- New messages
- Missed calls
- Group invitations
Support:
- Images
- Videos
- Audio files
- Documents
Support:
- Initiate call
- Accept call
- Reject call
- End call
- Missed call tracking
Support:
- One-to-one video calls
- Group video calls
- Camera controls
- Screen sharing (future)
| Requirement | Target |
|---|---|
| Availability | 99.99% |
| Latency | <100ms |
| Scalability | Horizontal |
| Durability | High |
| Reliability | High |
| Fault Tolerance | High |
Assumptions:
100M Daily Active Users
10M Concurrent Connections
50B Messages / Day
Average Traffic:
~600K Messages / Second
Peak Traffic:
1M+ Messages / Second
POST /messagesRequest:
{
"senderId": "user1",
"receiverId": "user2",
"content": "Hello"
}GET /conversations/{id}/messagesGET /users/{id}/presencePOST /calls/startPOST /calls/endusers
-----
id
phone_number
name
created_atconversations
-------------
id
type
created_atmessages
--------
id
conversation_id
sender_id
content
type
status
created_atgroups
------
id
name
owner_id
created_atgroup_members
-------------
group_id
user_id
rolecalls
-----
id
caller_id
receiver_id
type
status
started_at
ended_at
duration Clients
│
▼
API Gateway
│
▼
Load Balancer
│
┌──────────────┬──────────────┬──────────────┐
│ │ │ │
▼ ▼ ▼ ▼
Chat Service Presence Service Call Service Notification Service
│ │ │
└──────────────┼──────────────┘
▼
Kafka Cluster
▼
┌────────────┬────────────┐
▼ ▼ ▼
Postgres Redis Object Storage
Persistent WebSocket connections are used for:
- Real-time messaging
- Presence updates
- Typing indicators
- Delivery acknowledgements
- Call signaling
Benefits:
- Low latency
- Bi-directional communication
- Reduced connection overhead
Responsibilities:
- Discover active chat servers
- Route users to correct nodes
- Support horizontal scaling
Possible solutions:
- Consul
- Kubernetes Service Discovery
- etcd
Tracks:
Online
Offline
Last Seen
Redis stores:
user:{id}:presence
Example:
{
"status": "online",
"lastSeen": "2026-06-08T10:00:00Z"
}Flow:
Sender
│
▼
WebSocket
│
▼
Chat Service
│
▼
Receiver Connection
If receiver is offline:
Store Message
Send Notification
Deliver Later
Sender
│
▼
Chat Service
│
▼
Receiver Online
│
▼
Instant Delivery
Sender
│
▼
Chat Service
│
▼
Database
│
▼
Receiver Reconnects
│
▼
Pending Messages Delivered
States:
SENT
DELIVERED
READ
Flow:
Message Sent
│
▼
Delivered
│
▼
Read
Users may have:
- Mobile
- Tablet
- Desktop
Message fan-out:
User
├── Phone
├── Tablet
└── Desktop
All active devices receive updates.
Components:
- Groups
- Members
- Admins
Flow:
Sender
│
▼
Group Service
│
▼
Member Fanout
Media is stored separately.
Client
│
Upload
│
▼
Object Storage
│
▼
URL Generated
│
▼
Message Contains URL
Store:
- Images
- Videos
- Audio
- Documents
Possible storage:
- AWS S3
- MinIO
Responsible for:
- New messages
- Missed calls
- Group invitations
Platforms:
- FCM
- APNS
Caller
│
▼
Signaling Service
│
▼
Receiver
WebRTC
Components:
- WebRTC
- STUN Server
- TURN Server
- Signaling Service
Call Flow:
1. Call initiated
2. Receiver notified
3. SDP Exchange
4. ICE Exchange
5. Peer Connection Established
6. Audio Streaming Starts
Uses:
WebRTC
Additional Components:
- Video Encoder
- Video Decoder
- Adaptive Bitrate Controller
Flow:
Caller
│
▼
Signaling
│
▼
Receiver
│
▼
WebRTC Session
Mesh architecture does not scale.
Instead use:
SFU (Selective Forwarding Unit)
Architecture:
Participant A
Participant B
Participant C
│
▼
SFU
▲
Participant D
Participant E
Possible SFU Solutions:
- LiveKit
- Janus
- mediasoup
- Jitsi
All services remain stateless.
Used for:
- Presence
- Sessions
- Caching
Used for:
- Message events
- Notification events
- Call events
Shard by:
User ID
or
Conversation ID
Techniques:
- Retries
- Dead Letter Queues
- Replication
- Backpressure
- Circuit Breakers
Authentication:
JWT
Authorization:
Role-Based Access Control
Future:
End-to-End Encryption
Metrics:
- Active Connections
- Messages/sec
- Message Delivery Latency
- Active Calls
- Video Sessions
- Kafka Lag
- Redis Latency
Tools:
- Prometheus
- Grafana
- OpenTelemetry
- End-to-End Encryption
- Status / Stories
- Message Reactions
- Live Location Sharing
- Message Search
- AI Assistant
- Screen Sharing
- Voice Notes
- Community Groups
| Component | Technology |
|---|---|
| Backend | Go |
| API | gRPC |
| Realtime | WebSocket |
| Voice/Video | WebRTC |
| Database | PostgreSQL |
| Cache | Redis |
| Queue | Kafka |
| Storage | S3 / MinIO |
| Monitoring | Prometheus |
| Visualization | Grafana |
| Containers | Docker |
| Orchestration | Kubernetes |
MIT License