System design for online messaging service like whatsapp

System design for online messaging service like whatsapp

In this chapter we shall discuss system design for online messaging service.

How whatsapp is different from other messaging services?

In whatsapp, the text are end to end encrypted. Hence intermediate servers cannot read your messages.

In whatsapp, the messages are stored in server till the other party reads the message. Once other party reads the message, that message will be deleted from the server.

Features that we will discuss in this chapter is:

1. User Base
2. Last Seen
3. Sending media like images or other documents
4. Encryption

Consider that messaging service is very popular and many users will be using this service. Hence you need to scale the server horizontally. As you are scaling horizontally, you need a loadbalancer.

Hence the message will first hot the load balancer, the load balancer will check where the message is to be sent.

We also need DB to save the messages.

Simple communication can be seen as below:

So when the user, suppose UserA sends a message to UserB, once mobile is connected to internet, that message will be sent to a load balancer, then to a Message Server.
Then the message server will check if the other user i.e UserB is connected to the service. If UserB is connected, then the message will be sent to UserB. If UserB is not connected to the service, then the message will be stored in DB.
As soon as UserB is connected to the server, then the server will send that message to UserB.

This is how a message will work end to end.

Understanding feature like last seen, single tick and double tick will work?

These features can be accomplished by Acknowledgement service.

Single Tick:
Once the message from UserA reaches the server, the server will send an acknowledgement saying that the message has been received. Then UserA will display single tick.

Double Tick:
Once the message from server sends that message to UserB by appropriate connection, UserB will send an acknowledgement to the server saying that it has received the message.

Then server will send another acknowledgement to UserA, hence it will display double tick.

Blue Tick:

Once the UserB opens whatsapp and checks the message, UserB will send another acknowledgement to server saying that user has read the message. Then server will send another acknowledgement message ti UserA. Then UserA will display blue tick.

To identify all the acknowledgements, there will be unique ID attached to all the message.

Last Seen feature:

For this, we need a heartbeat mechanism. This service will be sending a heartbeat for every 5 seconds when the user is online or using the application.
When the server receives a heartbeat, it will store in another table with User_name and last seen time.

Then retrieve this information, when UserB is online.

Working of message server:

Whenever a connection is made to the server, a separate thread along with a queue[to store messages] will be created. Then there will be a separate table that will be mapping threadID with DeviceID.

So when the message is received from UserA, Thread of UserA will check the table, if UserB is connected. If UserB is connected, then the message is sent to queue of UserB. Then the thread of UserB will check if there are any messages in its queue. If there are any messages, then those messages will be sent to UserB.

If the UserB is not connected to the service, then the entry of UserB not be there in the table. When UserA sends message to UserB, as the entry is not available, the message will stored in DB. Once UserB is online, then the message will get delivered.

Understanding how media transfer works?

For sending media, we cannot use previously created thread. As threads are light weight, sending media in the same connection will not be efficient. For this you can use a HTTP connection to upload media to a http server. Then this server will return a HashID to UserA. Then we send the HashID along with media type to UserB.

When the message is received to UserB, UserB will download from http server. This procedure will work for all the media types.

For encryption, you can use:
1. One key that is shared between 2 clients.
2. One user will have a private key and share public key to other user.

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *