Skip to main content

Command Palette

Search for a command to run...

Designing the facebook messenger

Published
3 min read

Hello All,

In this article, we will analyze the system design and fault tolerance of the live chat application.
Requirements -
Identify functional and non function requirements in the diagram below.

Traffic Estimation

Millions of messages are sent between users. On average, each message is around 100 characters long, and users send over 100 such messages every day. Some users have a small number of contacts in their group, while others have a significantly larger number of contacts.

High-Level Design Facebook Messenger
Message Flow

High Level Design

HTTP is client-initiated, so it is not suitable for live applications where the server needs to send messages to the receiver. Clients can't message each other directly; they always need a service/server that supports the following features:

  1. Receives messages from other clients.

  2. A Chat Service Server sends messages to the appropriate recipient.

  3. The Chat Server must hold the message for the recipient if they are not online. This ensures that messages are not lost and can be delivered once the recipient is available.

We need to consider a different protocol where the server can send messages to the recipient. There are three options:

  1. Polling - The client continuously checks the server for new messages, which is considered inefficient.

  2. Long Polling - The server maintains an open connection with the client until new data is available.

  3. WebSocket - There is always an open connection between the client and server, allowing two-way communication over a long-lasting connection. This is known as a Bidirectional Communication Protocol.

Service Type

Database Design

SQL vs NoSQL

A chat service can be implemented with both SQL and NoSQL depends on the services.

Generic data which is incrementing gradually can be stored in the relational database(SQL) which are shown in above diagram.
Chat Data mostly well suited with Key-Value (NoSQL) Database i e Cassandra. Records are sharded by partition key and records with the same partition key are sorted by sort key as shown in the below diagram.

Tables

Architecture

WebSocket server - These services are responsible for providing a port to every online user so Websocket Manager assigns the port to each of the web socket server which resides on top of the cluster.

Service Discovery - Apache Zookeeper is a popular open-source one. It registers all the available chat serv.ers and picks the best chat server for a client based on predefined criteria

Message Service - A WebSocket server also needs to communicate with another service which is a message service. Message service is basically a repository of messages on top of the database cluster. It acts as an interface to the database for other services interacting with the databases.

Group Message Service - WebSocket servers don’t keep track of groups because they only track active users. But in the group some users could be online and others could be offline so Group Service handles the group messages.

User Service - For retrieving/storing user data.
Asset Service - For media files like images/videos

Thank you!