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.

3 Comments

  1. Bhavya
    Log in to Reply

    Thank you for information in an easy, understandable language.

    April 4, 2019
  2. Vishal Gupta
    Log in to Reply

    Nice Description
    Thanks……

    September 25, 2019
  3. sadegh
    Log in to Reply

    I was very excited when I saw you say the explanations that I had already drawn in my mind. such content is rare on the net. thank you!

    September 17, 2020

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT