Join us
@thenjikubheka ・ Aug 04,2021 ・ 4 min read ・ 2519 views
With over 2 billion users across the globe, WhatsApp messenger has become a household name and the messaging app of choice for many.
These are very impressive numbers that lead to the question; “How does WhatsApp messenger work?”
Background
In a small network, we can easily send each other messages, a perfect example would be me sending a message to my friend Dee Didi. The process is quite easy as we are on the same network and my device knows the IP address of Dee’s device.
The problem starts when I also want to send the same message to Luvo, Langa, Fani, Bongi, and Mampshika. We use different network services and due to trust issues, they will not share their IP addresses.
Such issues and others not mentioned led to the creation of the internet and server technologies. Using a server and connecting it to the internet makes it possible for us to share messages without being on the same network and the server handles the IP address issue.
When Luvo wants to send a message to Bongi, his device does this through the server which knows the IP address of Bongi’s device and just forwards the message to her.
We can all message each other through the server effortlessly but the WhatsApp scenario is not so simple, a few technicalities have to be handled.
In a WhatsApp scenario, we are all connected to multiple messaging servers which are horizontally scaled.
Multiple servers which are horizontally scaled are not enough to handle the load of WhatsApp users, so a load balancer was added.
The load balancer is responsible for offloads and redirecting requests to different servers, based on various parameters such as load on the server and sessions of the user and server.
Besides the load we need to store data, WhatsApp uses MnesiaDB as its database management system, Mnesia is highly favorable for its ability to be reconfigured on runtime.
Message Flow
The system is centered around a duplex connection system meaning messages are bidirectional. There are different types of connections namely TCP, UDP, Web Socket though the most common is TCP.
Back to my friend’s example; Langa wants to send a message to Fani, as soon as Langa connects to the system a process with the corresponding process id and the queue is created.
Fani has also connected hence a process with a corresponding process ID and a queue is created.
When the two processes are complete entries are made into the database, showing what process happens in Langa's device and what happens in Fani’s device.
What Happens If One Of The Devices Is Offline
Fani receives the message from Langa and immediately wants to forward it to Mampshika who is sleeping and has his device turned off.
There is already a process created for Langa and Fani as they are connected to the system but none as yet for Mampshika. When Fani forwards the message to Mampshika the process responsible for handling Fani’s request searches in the table for the process handling Mampshika’s request and finds nothing.
The message(s) are then saved in an additional table in the database.
After 6 hours Mampshika wakes up, and connects to WhatsApp messenger, as soon as he connects a process with the corresponding process ID and a queue is created for him that will handle all his requests.
As soon as this process is created it starts off by checking if there are any messages meant for Mampshika in the database, once it finds them they are forwarded to Mampshika's device.
Message Acknowledgment Feature
During this whole messaging marathon, we as the users see three different types of ticks.
One of the message servers will look for an appropriate connection for our devices, the messages are sent to the respective devices, which is indicated with one grey tick.
Once we are online and a process is created for our individual devices, with the message being delivered by the process, then this is indicated by two grey ticks.
Once any of us reads our message(s) then a response is sent to the messaging server to signify acknowledgment from the device.
Media
Mampshika reads the message and is lazy to type back to Fani and decides to send a voice note.
Voice notes are in the form of mp3 files which are quite heavy files these will strain our messaging servers. HTTP server(s) are used to handle media file requests be it mp3,mp4,jpg or any other media file.
The HTTP server has a CDN or database as its coworker to handle this strenuous job. Mampshika’s device will ignore the existing connection already established for the lightweight messaging requests and upload the media File to the HTTP server.
Once the voice note is uploaded to the HTTP server a unique hash is returned by the server as a message with the media type to Mampshika’s device. The message is then sent through the messaging server to Fani’s device and when Fani’s device receives this message, it uses the hash to download the media file from the HTTP server.
Also, Check Out:
Whatsapp System Design and Chat Messaging Architecture (Part 1)
Join other developers and claim your FAUN account now!
Influence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.