Ever wondered how your WhatsApp actually works behind the scene? This blog will uncover the magic behind a real time messaging platform supporting 200 million users worldwide.
To understand this, Let’s start with how 2 clients(mobile,desktop etc.) can communicate via Internet. They must know their IP addresses.
But imagine if it’s possible to know every IP in a large netowork of mobile devices. Obviously,No.
Then we can have a single server, which will get the message from user A and give it back to user B. A simple client-server architecture.
But again a situation where we have millions of smartphones which will cause heavy traffic on our server. The solution could be using multiple servers. Something like this:
A load balancer will decide which user will be served by which Messaging server. A kind of Captain.
Now comes the question, What type of communication is needed between client and server. HTTP protocol? No, because it allows only client to send a request to the server in unidirectional way. Here we need something that allows the server to send message to user B ( the client). Something bidirectional. So another protocol Web Sockets comes to rescue.
Sender Receiver Scenarios in WhatsApp
- When sender A is online , but not the receiver B. The message will be stored in a database by the server, waiting B to be online.
- When sender A sends a Whatsapp but he is not connected to Internet. Then what? Message is stored in A’s local storage and sent to server once online.
- When both A and B are online. That’s great. NO database or local storage need. Msg from A to server , and then to B.
Last Seen/Online - How does that work?
What’s App is spying on you.. 😀
It could work like this — Our app might be sending a kind of pulse Evey 5 sec , or whenever an activity is done by an online user ( Scroll, Change tab, Typing..). A timestamp against the user ID is recorded . And U know the Last SEEN..
What about storing heavy Images and Videos??
Media is large size ! Storing that in DB ,for millions of user’s is not a good and viable option. The HTTP request comes to rescue here..
Here , The sender A sends the image/video to a Media Server which actually sends a certain hash code of that media to the WhatsApp Server, which could be sent to the receiver B.
So, There is no need to store this heavy media in a database.Instead, the receiver B can download that media by making a HTTP request to Media Server. Clever, Isn’t it?
More Technical — Sent, Delievered, Seen
What happens is When client A sends a message to B, A process is created on the server with certain id ( Pid) and has a queue which is kind of buffer , to keep track of the message. And a particular entry for the process Id and Client I’d is saved into database.
For example - When A sends message to WhatsApp server, Status will be ‘Sent’. When the server sends it to B , status changes to ‘Delievered’. When read by B , the message is ‘SEEN’.
Tech Behind the Messaging App
- Progaramming language- Erlang . It’s super fast, support for lightweight threads to handle 200 million connections.
- Operationg System — FreeBSD. It is open source!
- Database — AMNESIA. A handy key-value storage.
- Web Server --YAWS(Yet Another Web Server)
That’s it. The very high-level overview of How the What’s App works !
Hope you like it.Feel free to clap, share and share your opinions.
Never Stop Learning !