Modern System Design

System Design of WhatsApp

3 min read

 

WhatsApp is the chat based application and the aim of this article to help you understand the building blocks and design of WhatsApp. This will enable you to build design of any chat based application.

“WhatsApp is an instant messaging and voice-over-IP service owned by technology conglomerate Meta. It allows users to send text, voice messages and video messages, make voice and video calls, and share images, documents, user locations, and other content.” Lets get started.

 

Gathering Requirements:

 

Functional:

  • One to one chat
  • Group Messaging
  • Sent + delivery + read receipt
  • Online/Last seen

 

Non Functional :

 

  • Consistency
  • Availability
  • Partition Tolerance

 

Capacity Planning:

 

Number of users on the application every month: 1 Billion

Number of active users per second at peak traffic: 650, 000

Number of messages per second at peak traffic: 40 Million

 

Servers in session microservices = (chat msg * Latency )/ concurrent connections per server

If there are 100K concurrent connection per server and latency is 20 ms. It will be 8 servers or add few extra for handling the failure.

 

 

 

High Level Design:

 

 

One to One Chat Process-

 

 

 

 

  1. Users A will send request to gateway. Now Gateway needs to identify the User B location or Gateway location of User B.
  2. We will create the Session Microservice that will know which user is associated with which Gateway. This microservice will be backed by multiple servers to avoid single point of failure.
  3. User A will send request eg sendmessage(UserB) to Gateway and this gateway will process it to session microservice.
  4. This session service will work as a router and store the User-Gateway mapping in the database. And figure out the details about user B getaway and send the request to gateway and send the msg to User B.
  5. HTTP is client to server protocol. We can only send the msg from client to server not vice versa. So we need another protocol that use TCP, so WebSocket’s is  accurate and useful when we are building the chat applications.
  6. Using Websocket, server can send the message to User B.
  7. Now user A can send the message to User B.

 

 

 

Sent + Delivery + Read Receipt:

 

 

  1. When User A message goes to session service via gateway. And session service successfully redirect the message to user B’s gateway. It will send back the response to User A that message it sent.
  2. Once gateway 2 sends the message to user B. UserB will send the response that message is received and gateway will send the delivery receipt to session service and back to User A for delivery response.
  3. When the user B, opens the chat. Gateway will send the response back to session service that message is read.

 

 

 

 

Group Message Chat Process:

 

 

 

  1. Sending a group message mostly the same process as sending one to one chat but here session service will send the request to group service. It will use consistent hashing to reach to right group service box.
  2. Group service will have mapping of groupid linked to multiple userid. It will use message queue that make sure that message should not be lost in case of any failure.
  3. Once we have the group Id message will ask the session service for gateway details of userids and send the message to respective users.

 

 

Thank you for reading. We hope this gives you a good understanding. Explore our Technology News blogs for more news related to the Technology front. AdvanceDataScience.Com has the latest in what matters in technology daily.

Tagged , , , , , , , , , , , , , , , , , , , , , , ,