Thursday, April 15, 2021

System design: Facebook messager | Grokking system design | My review on April 15 2021

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:

  1. Messenger should support one-on-one conversations between users.
  2. Messenger should keep track of the online/offline statuses of its users.
  3. Messenger should support the persistent storage of chat history.

Non-functional Requirements:

  1. Users should have real-time chat experience with minimum latency.
  2. Our system should be highly consistent; users should be able to see the same chat history on all their devices.
  3. 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:

    Total messages20 billion per day
    Storage for each day2TB
    Storage for 5 years3.6PB
    Incomming data25MB/s
    Outgoing data25MB/s

 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:

  1. User-A sends a message to User-B through the chat server.
  2. The server receives the message and sends an acknowledgment to User-A.
  3. The server stores the message in its database and sends the message to User-B.
  4. User-B receives the message and sends the acknowledgment to the server.
  5. The server notifies User-A that the message has been delivered successfully to User-B.
Request flow for sending a message

1 of 8

Request flow for sending a message

2 of 8

Request flow for sending a message

3 of 8

Request flow for sending a message

4 of 8

Request flow for sending a message

5 of 8

Request flow for sending a message

6 of 8

Request flow for sending a message

7 of 8

Request flow for sending a message

No comments:

Post a Comment