ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

System Design Tutorial Example 7: System design for online messaging service like WhatsApp

prodevelopertutorial February 17, 2019

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

How WhatsApp is different from other messaging services?

In WhatsApp, the text is 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 load-balancer.

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.

7.1 Simple communication can be seen as below

System Design Tutorial Example 7: System design for online messaging service like WhatsApp

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.

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

These features can be accomplished by Acknowledgement service.

7.2.1 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.

7.2.2 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 recieved the message.

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

7.2.3 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.

7.3 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.

7.4 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.

7.5 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 recieved to UserB, UserB will download from http server. This procedure will work for all the media types.

7.6 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.

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2022 ProDeveloperTutorial.com