Design a messaging service, like WhatsApp



This entry will discuss a high level design for messaging service, like WhatsApp. Following are the list of features our messaging service is going to support.

List of features:

1. At WhatsApp scale, lets say we need to handle around 10 Billions message sends per day and 300 Millions users. Assuming each message on average  has 160 characters, implies 10B * 160  =  1.6TB data per day , not including message metadata. If messaging service provision for 10 years, we are looking at 10*1.6B*365 ~~6 Petabytes.

2. Messaging service will support only 1:1 plain text conversations and should be extendable to add group conversations.

3. Messaging service will only handle messages less than 64Kb in size.

4. Messaging service will have low latency, high consistency and availability. Consistency has higher priority than availability.

Design Strategy :

The messaging service will expose following APIs to clients.

1. SendStatus sendMessage(senderId, receipientIds, message, ClientMessageId) : This API will be idempotent. If the client retires the message, the message will not be added twice. One way to handle this by generating a random timestamp based ID on the client which can be used to avoid the same message being  sent repeatedly. Ordering of the messages will be maintained. Means, If I send message A, and then message B, then A should always appear before B. We can use server side timestamp based approach to handle this. The parameter used in the sendMessage API is self descriptive.

Returns: SendStatus -> Status of the sendMessage Request as enum.

2. ConversationList fetchConversation(userId, offset, messageCount, lastUpdatedTimeStamp):
This API will be used to fetch and show conversations in a thread. Think of this as view you see when you open the WhatsApp. For a user, we would only want to fetch few messages in one API call at a time(Lazy Loading). The offset and messageCount parameters are used to handle this.

In most cases, our API calls are made by the users who are active on messing services. As such, they already viewed conversations till a certain time stamp and only looking for updates after the time stamp. To handle the clients which are data sensitive we are using lastUpdatedTimeStamp Param.

Returns: ConversationList{
                     List<Conversation> conversations;
                     boolean isDBupdate;
                }
     

              Conversation {
                    int conversationId;
                    List<UUID>participantIds;
                    String snippet;
                    long lastUpdatedTimeStamp;
              }

Following is the high level component diagram for the messaging system.

             

Client (Mobile App/ Browser etc) calls sendMessage API for writing a message. Application server will interpret the API call and calls database to do following. 
  •  Put in the server time stamps to handle ordering of message.
  •  Figure out the conversation to which the message should be appended based on the participants. 
  •  Figure out if the recent message exists with the clientMessageId.
  •  Store the message in database.
Similarly to read conversations, client calls fetchConversation API. Application server interprets the API call and queries the database for the recent conversations.  

Message is going to be very write heavy. Unlike photos, videos etc which are written once and consumed a lot of times by lot of clients, message are written once and consumed by the participants once. For the write heavy system with a lot of data, RDBMS usually do not perform well as every write is not just an append to a table but also an updates to the multiple indices which might required locking and hence might interfere with reads and other writes. However, there are NoSQL databases like HBase, cassandra where writes are cheaper. 

With NoSQL, we need to store data in denormalized form. Every user would have his/her own copy of message box.  That means we will store two copies of the message, one for each participant for every message send for 1:1 conversation.

To increase the efficiency we can use Caching. This is however not as easy as it seems as one of our feature is to ensure high consistency. Most distributed caching system are good with availability and eventually consistent., but not tight consistent.

Lets consider the situation if the user messages and conversations are spread across machines then it causes trouble because the changes are not atomic. Consider the case when messages for a user are on one machine and conversations are on another. When a message is added, the update request is sent to server with messages and server with conversation for this user. There could be a period when one server has processed the update but other has not. If changes through servers are not atomic, the whole system is not atomic anymore. 

One way to resolve is to make sure that the caching for a user completely resides on one server. The same server can also have other users, but user assigned to exactly one server for caching. To further ensure consistency, all the writes for that user should be directed through this server and this server updates its cache when the write is successful on database before confirming success.

For messaging service, every bytes waste has very real impact on the experience of application. By sending less data and reducing HTTPS fetches, messaging service receives updates with low latency and high reliability. In the subsequent entry we will extend the design to improve performance and data usages. Meanwhile, take care and Enjoy learning!!





Comments

  1. Bonjour,


    I am shocked, shocked, that there is such article exist!! But I really think you did a great job highlighting some of the key #topic in the entire space.



    We were experimenting with AWS and somehow linked existing accounts. If I click the button to say close account then I get a message stating:


    I look forward to see your next updates.


    ,Merci

    ReplyDelete
  2. Howdy Mate,


    Design a messaging service, like WhatsApp being contrived to exist for many projects simply so it can be run will be the first to hit the wall, but those projects where the functions to make existing transactions cheaper in real world applications will find the elusive real world demand.

    I recently attended the AWS Summit London 2018. During the afternoon session about "Open Source at AWS" there were some resources mentioned for persuading your company that open source can be a great benefit to them.
    Unfortunately this wasn't one of the slide sets which has been made available after the event.

    Follow my new blog if you interested in just tag along me in any social media platforms!

    Cheers,
    Radhey

    ReplyDelete
  3. Hey,


    I learnt so much in such little time about Geekibuti - Help you to get Dream Job!! Even a toddler could become smart reading of your amazing articles.


    I cannot connect to the Windows EC2 instance through remote desktop, only a list of errors I got (see the attached screenshot). I am sure I have started the instance and I have waited long enough. What's the matter? AWS Training





    It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
    Keep up the good work!


    Gracias
    Ajeeth

    ReplyDelete
  4. Hello Buddie,


    Your writing shines! There is no room for gibberish here clearly you have explained about Design a messaging service, like WhatsApp. Keep writing!
    The last few day I am trying to set up SES and Workmail. SES email sending works, but I can not receive any emails. I have been lifted out the sandbox so the SES sending limit is good. Also, all my domains are verified. I tried to google around and did not found any solution. When I send an email from my google account to my amazon account (domain with route53) the email bounce and I receive this: AWS Training USA







    Thanks a lot. This was a perfect step-by-step guide. Don’t think it could have been done better.


    Thank you,
    Ajeeth

    ReplyDelete
  5. Salemetsiz Be,


    11/10!! Your blog is such a complete read. I like your approach with Geekibuti - Help you to get Dream Job!! . Clearly, you wrote it to make learning a cake walk for me.


    I like the support for S3 in the AWS console, especially being able to apply ACLs at time of upload. However, it would be very good if the view was sortable by last modified date, as I am typically working with the last 10-15 files uploaded. AWS Training USA





    It was cool to see your article pop up in my google search for the process yesterday. Great Guide.
    Keep up the good work!


    Cheers,
    Ajeeth

    ReplyDelete
  6. Howdy Mate,


    A really interesting, clear and easily readable Design a messaging service, like WhatsApp article of interesting and different perspectives.I will clap. So much is so well covered here. AWS Training USA


    I am trying to connect to ECS through ArcGIS Cloud Builder on Amazon Web Services. I have installed the Cloud Builder on my laptop, get authorization to use ESRI AMI, created group, user, got the access key and the secret access key but when trying to connect to AWS using Cloud Builder with I get the following message:





    But nice Article Mate! Great Information! Keep up the good work!


    Thank you,
    Ajeeth

    ReplyDelete
  7. Greetings Mate,

    Three cheers to you ! Hooray!!! I feel like I hit the jackpot on Design a messaging service, like WhatsApp

    I created a new AWS account but then realised a better thing to do would be to change the email address of an existing account to the new email address. I closed the new account but can't change the email address of the existing account to the new email as it now says "The new e-mail address you have provided is already in use. AWS Training USA Please use a different e-mail address."

    Very useful article, if I run into challenges along the way, I will share them here.


    ,Merci
    Radhey

    ReplyDelete
  8. Could you explain why consistency has higher priority over availability in this case?

    ReplyDelete
  9. Good. I am really impressed with your writing talents and also with the layout on your weblog. Appreciate, Is this a paid subject matter or did you customize it yourself? Either way keep up the nice quality writing, it is rare to peer a nice weblog like this one nowadays. Thank you, check also ekspedisi and cek resi dan cek ongkir

    ReplyDelete

Post a Comment