Design a messenger (System Design round)

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2022-02-15

Problem Statement

Messaging platforms (WhatsApp, Messenger, Signal, Slack) are core communication tools. They must support real-time message delivery, group chats, media sharing, read receipts, presence, and offline reliability — all at scale with millions of concurrent users.


✅ Functional Requirements


1.1:1 Chat
•Real-time text messaging between users.
•Messages must be delivered in order (per conversation).
2.Group Chat
•Support multiple participants.
•Broadcast messages to all group members.
•Handle group metadata (add/remove members, group name, admins).
3.Message Types
•Text, emojis, files, images, videos, voice notes.
•Support message reactions.
4.Message Delivery Guarantees
•At-least-once delivery.
•Read receipts (sent, delivered, seen).
5.Presence & Typing Indicators
•Show online/offline status.
•Show “typing…” when composing.
6.Offline Support
•Store messages if recipient is offline.
•Sync on reconnection.
7.Search & History
•Fetch conversation history.
•Search by keyword/user/date.


❌ Non-Functional Requirements


•Scalability: Handle millions of concurrent connections.
•Low Latency: Message delivery <200ms ideally.
•High Availability: No single point of failure.
•Durability: Messages must not be lost.
•Security: End-to-end encryption, authentication.


🧱 System Components


1.Client App (Mobile/Web/Desktop)
•UI for chats, media, presence.
•Maintains persistent WebSocket connection.
2.Gateway Service
•WebSocket/HTTP long-lived connection handling.
•Routes messages to backend services.
3.Messaging Service
•Core service to handle message delivery.
•Stores metadata (timestamps, sender, recipient).
•Guarantees ordering per conversation.
4.Queue/Streaming Layer
•Kafka/Pulsar for reliable delivery and retries.
•Helps with decoupling sender/receiver availability.
5.Storage Layer
•Metadata DB (Cassandra/DynamoDB) → message index, user info.
•Blob Store (S3/HDFS) → images, videos, files.
•Cache (Redis/Memcached) → recent conversations, presence.
6.Presence & Notification Service
•Track user online/offline.
•Push notifications for offline users (via FCM/APNS).
7.Group Management Service
•Handles membership, roles, group metadata.
8.Search Service
•Inverted index (ElasticSearch, OpenSearch).
•Enables keyword search in conversations.


🔄 Example Flow: Sending a Message


1.Alice sends message → Client sends via WebSocket → Gateway → Messaging Service.
2.Messaging Service persists to DB → pushes to Kafka topic.
3.If Bob is online → Gateway pushes via WebSocket.
4.If Bob is offline → Message stored → Push Notification sent.
5.Delivery and read receipts updated in DB → synced back to Alice.


⚖️ Trade-offs & Design Considerations


•Storage: SQL (consistency) vs NoSQL (scale, eventual consistency).
•Message Ordering: Partitioning by conversation ID to preserve order.
•Offline Delivery: Queue unacknowledged messages; how long to retain?
•End-to-End Encryption: Should servers store encrypted blobs or plaintext?
•Scalability: How to scale WebSocket servers (millions of persistent connections)?
•Reliability: Replay queues vs exactly-once semantics (very hard at scale).


🧪 Open Discussion Points


•Should messages be deleted from servers after delivery (like Signal) or persisted forever (like WhatsApp)?
•How to handle multi-device sync (phone + laptop + tablet)?
•Should media delivery be peer-to-peer (like WebRTC) or always via server?
•What’s the sharding strategy for large group chats (thousands of members)?


👉 This HLD problem tests real-time systems, distributed storage, queues, consistency, and security trade-offs — very common in FAANG/fintech system design rounds.

Solution

Comments