Designing Facebook Messenger
Let's design an instant messaging service like Facebook Messenger where users can send text messages to each other through web and mobile interfaces.
We'll cover the following
1. What is Facebook Messenger?#
Facebook Messenger is a software application which provides text-based instant messaging services to its users. Messenger users can chat with their Facebook friends both from cell-phones and Facebook’s website.
2. Requirements and Goals of the System#
Our Messenger should meet the following requirements:
Functional Requirements:
- Messenger should support one-on-one conversations between users.
- Messenger should keep track of the online/offline statuses of its users.
- Messenger should support the persistent storage of chat history.
Non-functional Requirements:
- Users should have real-time chat experience with minimum latency.
- Our system should be highly consistent; users should be able to see the same chat history on all their devices.
- Messenger’s high availability is desirable; we can tolerate lower availability in the interest of consistency.
Extended Requirements:
- Group Chats: Messenger should support multiple people talking to each other in a group.
- Push notifications: Messenger should be able to notify users of new messages when they are offline.
- 3. Capacity Estimation and Constraints#
Let’s assume that we have 500 million daily active users and on average each user sends 40 messages daily; this gives us 20 billion messages per day.
Storage Estimation: Let’s assume that on average a message is 100 bytes, so to store all the messages for one day we would need 2TB of storage.
20 billion messages * 100 bytes => 2 TB/day To store five years of chat history, we would need 3.6 petabytes of storage.
2 TB * 365 days * 5 years ~= 3.6 PB Other than the chat messages, we would also need to store users’ information, messages’ metadata (ID, Timestamp, etc.). Not to mention, the above calculation doesn’t take data compression and replication into consideration.
Bandwidth Estimation: If our service is getting 2TB of data every day, this will give us 25MB of incoming data for each second.
2 TB / 86400 sec ~= 25 MB/s Since each incoming message needs to go out to another user, we will need the same amount of bandwidth 25MB/s for both upload and download.
High level estimates:
Continue
4. High Level Design#
At a high-level, we will need a chat server that will be the central piece, orchestrating all the communications between users. When a user wants to send a message to another user, they will connect to the chat server and send the message to the server; the server then passes that message to the other user and also stores it in the database.
The detailed workflow would look like this:
- User-A sends a message to User-B through the chat server.
- The server receives the message and sends an acknowledgment to User-A.
- The server stores the message in its database and sends the message to User-B.
- User-B receives the message and sends the acknowledgment to the server.
- The server notifies User-A that the message has been delivered successfully to User-B.
1 of 8
2 of 8
3 of 8
4 of 8
5 of 8
6 of 8
7 of 8
No comments:
Post a Comment