System Design ( Day - 20 )

Manoj KumarManoj Kumar
4 min read

System Design of Chat App ( Whatsapp )

Features

1 . One to one Chat
2 . Group Messaging
2 . Sent, Delivered, Read recipient
3 . Online / Last Seen
4 . Image sharing
5 . Permanent / temporary chat

Design

Users will connect to the gateways from the gateway the users are connected to the individual services. through the gateway we have session service which maintains who’s connected to which box like gateways, the active connections are stored inside the session service to chat, with the help of webSockets we can establish the peer to peer connection and we can send/receive the message with each other.
let’s say user A is sending message to the user X through the webSocket they will establish the peer to peer connection. and the user A sent the message to the user X then what the user A should be notified when the message delivered to the user X, before going to the user X the message will be stored in the database if the user is offline or some errors on the server, so that the data is persistent, and the user A will be notified that the message is sent to the user A we’ll notify you after receiving the message to the user X.
When user X gets online then the message in the database will be delivered to the user x,and it will return an acknowledgement message to the database through the gateway as im received. and the sender will also be notified about the receiving of message to the end user, when that user X opens that message again the same flow will happen like after reading the receiver will acknowledge internally to the database through gateway, and through the gateway the sender will also get a read recipient.

For the Last seen and Online feature, we are going to maintain a service for the last seen and online,
for online we can check based on the users activity, like after opening the mobile application we can trigger a api and we can store the online feature and if any one wants to access that data then they can see that in the users end by this service, for the last seen, in the video he has explained as based on the activity we can maintain the last seen, but as a android developer, i know how the activity life cycle works what ill do means ill do like ill trigger an api at the onCreate method like onStart in which it will change the state of the user in the db from Offline to Online, like if user swipes the app to background then ill trigger another api to change the state in the database through the online and last seen service, when the user returns to the app then we change the state to online, for last seen in onDestroy method we can call an api with the timestamp to store the last seen date and time.

Group Messaging
In the groups we have so many people so for handling efficiently we have to make it as static like around 200-400 members per group, so that the load will be reduced, and for storing the members data we have group service, to handle and maintain the group members, through the load balancer the load around the servers are distributed through the consistent hashing.
Let’s say we have a group containing 10 members, and a user send a message to the group, then that message will goes to the session through the gateway, from that gateway it’ll go to the parser and unParser microservice, its going to take the unparserd message from the user and send it to the group service, in the group service we have the groupId and userId is stored like for one group many users, if we want to get to this group, before this we have to use the consistent hashing to find the right groupID and send it to that group only.
Through the messaging queue, the message will be sent to the users that are associated with the group, after that the user will be notified about the delivery of the message or the read recipient, if that fails to send the message to the group then also it’ll be notified like, hey i can’t able to send the message in the group, in any case the user will be notified.

0
Subscribe to my newsletter

Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manoj Kumar
Manoj Kumar