[ OK ]583f0ec7-58ad-4a8f-b673-cdd901fdcdfd — full content available
[ INFO ]category: System Design difficulty: unknown freq: first seen: 2026-04-12
[UNKNOWN][SYSTEM DESIGN]High Frequency
$catproblem.md
Design Messenger
Problem Statement
Design a Messenger-like 1-to-1 chat system similar to Facebook Messenger or WhatsApp. At minimum, the system should support:
sending and receiving text messages in near real time
storing messages persistently
retrieving message history
handling online/offline states and delivery receipts
Constraints
The system should be able to scale to millions of users.
Ensure low latency for message delivery.
The system should be highly available and fault-tolerant.
Examples
User A sends a message to User B. The message should be delivered to User B in near real time.
User A goes offline. When User A comes back online, they should be able to see the messages they received while they were offline.
User A sends a message to User B. User B should receive a delivery receipt once the message is delivered.
Hints
Consider using a publish-subscribe model for real-time messaging.
Use a database with strong consistency and horizontal scalability, such as Cassandra or MongoDB.
Implement caching to improve read performance and reduce database load.
Use a service like Amazon SNS or Google Pub/Sub for message delivery.
Solution
Architecture Overview:
Frontend: Mobile and web clients built using React Native or Flutter for cross-platform compatibility.
Backend: Microservices architecture with services for user authentication, message storage, and real-time messaging.
Database: Cassandra or MongoDB for message storage with strong consistency and horizontal scalability.
Caching: Redis or Memcached for caching message history and improving read performance.
Message Delivery: Amazon SNS or Google Pub/Sub for delivering messages in real time.
User Authentication:
Implement OAuth 2.0 for secure user authentication.
Use JWT tokens for maintaining user sessions.
Message Storage:
Store messages in a NoSQL database like Cassandra or MongoDB with strong consistency and horizontal scalability.
Use a schema design that supports efficient retrieval of message history.
Real-Time Messaging:
Use a publish-subscribe model for real-time messaging.
Implement WebSockets for maintaining persistent connections between clients and servers.
Use a message broker like Amazon SNS or Google Pub/Sub for delivering messages in real time.
Offline Handling and Delivery Receipts:
Use a combination of caching and database storage to handle offline messages.
Implement a delivery receipt system using a publish-subscribe model to notify senders when messages are delivered.
Scalability and Fault Tolerance:
Design the system to be stateless to enable easy horizontal scaling.
Use load balancers to distribute traffic across multiple instances of each service.
Implement replication and data partitioning in the database to ensure fault tolerance and high availability.
Monitoring and Logging:
Implement centralized logging using tools like ELK Stack or Splunk for monitoring system health and debugging issues.
Use monitoring tools like Prometheus and Grafana to track system performance and resource usage.
By following these steps, you can design a scalable, fault-tolerant, and real-time Messenger-like chat system that meets the requirements outlined in the problem statement.