WhatsApp System Design

Jolly srivastava
System Design Concepts
5 min readDec 23, 2020

Understanding the scale and features:

  1. User Base: 2 + Billions
  2. Chats
  3. Last seen
  4. Media
  5. Encrypt
  6. Call

In this article, we will see above metioned features.

Consider we have two parties, client A and client B. we need a server in between them, let’s call it a “messaging server” for them to interact /send messages back and forth.

When we know that our user base here is in terms of billions of users, having said that, we need multiple messaging servers and the client here can’t connect to messaging servers randomly. We need a load balancer.

What happens when client A tries to connect messaging servers, request first hits the load balancer and then decide which messaging servers to connect with based on load and session they were previously connected to. We need DB to save some arbitrary states. Messaging servers(MS) will talk with DB to save messages in certain conditions. The connection over here is duplex. Duplex connection means bidirectional i.e A to B and vice versa as well. The most common connection happens via a TCP connection.

Consider client A wants to send a message to client B but A is not connected with the server. At this point in time message will get save in the local database of A’s phone i.e SQLite or any other light DB in the case of android. As soon as A connects to the messaging server(MS), the message transfered to the messaging server, it looks for the connection which is responsible to send the message to client B. Now if for some reason client B is not connected with MS, this message will get save in DB and as soon as B gets connected with MS, it sends the message to B.

In these scenarios, clients are the ones who establish the connection to the server because the server will never know the address of the client but vice versa is true.

Let’s talk about msg acknowledge i.e concepts of single,double, and blue tick. Whenever we send a message to someone, we see a single tick, double tick, or a blue tick.

When client A sends a message to MS, MS sends the information to client A saying I have received your message successfully, and then a single tick is displayed. Once MS finds a successful connection with client B, a message is send to client B and client B send a message to MS that I have received message successfully and that’s when a double tick is shown and when client B reads the message then one more message is being sent to MS that client has read that particular message and MS inturn sends a message to client A saying message has been read. In all these case, a unique id is used for the message to identify what acknowledgment is responsible for which particular message.

Messaging server in-depth:

Messaging server

Every time when a connection is established from the client to the server a respective process/thread is created which is long-running and is responsible for handling this particular connection. As we can see in the above image, for client A and B we have respective process/thread with a pid and there is a respective queue associated with the process which acts as a buffer where messages can be sent by any other different processes. We will add an entry in DB about PID and device ID.

Consider A wants to send a message to client B. This message firstly will get received by process of ID-1.It will query the DB asking the PID which is responsible for handling the message for client B i.e PID 2.PID 1 send this message to queue responsible for client B and also associated with PID2 . PID 2 will keep on looking queue for the message. As soon as message is receives, PID2 sends it to client B through the connection it has already established with the client B and this is how the message reaches to client B.

What if client B is not connected to MS? There will not be any thread/process exist responsible for handling messages for client B, then this entry “PID:2” won’t be present in the DB. ‘A’ knows that he needs to send message to client ‘B’, it searches for process responsible for sending a message to client B and then finds that client B does not exist. So it takes this message and saves it to one more DB with UID and message. When client B connects with MS, a new process gets create with PID let's say 3 and client ID B, and also it looks at DB, do we have any unsend message if it will find it will send it to client B.

How do we implement LAST SEEN?

This feature is implemented using heartbeat technique. Last seen heartbeat is always sent by the client and we will save in table something like the one shown above. Consider a senario where client A is scheduled to send a heartbeat every 5 sec if a client is actively using the application only. when MS revises the heartbeat, it will update the table. As soon as the user closes the application,heattbeat entry stops, and the last entry will retain in the table. If some other client wants to know the last seen of client A, we can always read from this table and display one hour ago/two hours ago anything.

3) MEDIA:

We have all send pictures/videos/voice on WhatsApp. We can do it over the same connection. We will introduce one more server beside MS i.e HTTP server and it is going to handle all the media content.HTTP has some kind of CDN that saves all this information. Whenever we try to upload an image, it uploads the image directly to the HTTP server, and this server returns the hash of the object we uploaded and sends the hash back to the client. We will send this hash to client B and type of media info only. When a message is received by client B. using the Hash B will download the image from the HTTP server or CDN.

In this article i have tried to explain certain features of chatting application like whatsapp. There are still many fuctionality not touched by this blog.

Hope you guys like it.

Stay tuned!!

--

--