Design Chatting System like iMessage
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-04-30
Problem Statement
Design a real-time messaging system where users can send 1-to-1 and group messages (text, media, files), with low latency, message synchronization across devices, and end-to-end encryption. The system should support online/offline delivery, push notifications, and scalability to millions of concurrent users.
🔶 Functional Requirements
1.User & Device Management
•Users register via phone/email.
•Each user may be logged in on multiple devices (phone, tablet, desktop).
•Presence indicator (online, offline, typing).
2.Messaging
•Send/receive real-time messages (text, media, documents).
•1-to-1 chat and group chat support.
•Read receipts (sent, delivered, read).
•Typing indicators.
3.Synchronization & Delivery
•Messages sync across all logged-in devices.
•Messages should be delivered instantly if recipient is online.
•If offline, messages should be queued and delivered when user reconnects.
4.Security
•End-to-End Encryption (E2EE) for all messages.
•Secure storage of chat history.
•User authentication and authorization.
5.Scalability & Performance
•Must support millions of concurrent users.
•Latency target: <200ms message delivery.
6.Media Support
•Send images, audio, video, and documents.
•Handle compression, upload to media servers, and link-based retrieval.
7.Additional Features (Optional)
•Message search (full-text).
•Voice and video calls.
•Stickers/reactions.
🔶 Non-Functional Requirements
•High Availability: 99.99% uptime.
•Durability: No message loss, even during server crashes.
•Scalability: Handle millions of chats per second.
•Security & Privacy: Strong encryption, zero message leaks.
•Fault Tolerance: Message retries and recovery.
•Consistency: Ensure exactly-once delivery (avoid duplicates).
🔶 Constraints
•Must handle high concurrent writes (messaging is write-heavy).
•Messages should not be lost in network failures.
•Must deduplicate messages in case of retries.
•Offline messages must be stored for X days before expiry.
•Optimize for mobile devices (battery, bandwidth).
🔶 Example Use Cases
1.User A sends a text to User B → B instantly receives it on all devices.
2.User C sends a message to a group chat → all group members get it in real time.
3.User D is offline → messages get queued and delivered when D reconnects.
4.User E logs in from phone + laptop → chat syncs across both devices.
5.Media message sent → uploaded to media server, and recipient fetches via secure link.
6.User F deletes the app → messages are wiped but stored for history on other devices.
👉 Summary:
"Design a scalable, real-time chatting system like iMessage that supports 1-to-1 and group messaging with end-to-end encryption, synchronization across multiple devices, offline delivery, media sharing, and high availability under millions of concurrent connections."