I will try explain everything about whatsapp system designing in a sensible engineering way.
Once you are able to design this, you should be able to design any chat based application.
Note: Key features are mentioned as *
Overall Architecture
User Equipment is connected to a gateway in the cloud, and all the internal services will be carried out with the use of external protocol in different language. All the security mechanisms will be taken care by gateway.
We also need to track of mapping between the user equipment and the gateway. If this mapping is stored in gateway, it will be quite expensive.
User Equipment ====> Gateway
UE 1 ====> Gateway 1
UE 2 ====> Gateway 2
Maintaining a TCP session in the gateway itself costs very high. Moreover if we maintain these mapping, it is going to be replicated in all the boxes. It is also a transient information.
Since Gateways always starve for memory, we need microservice to keep these sessions (user to gateway mapping). All the information from the gateway are decoupled and stored in "sessions" microservice. This is just another router.
Also, there will be multiple services to avoid single point failure.
Sending Message from One to Another
Now, with session service in place, let's start sending the message.
Now, B received a message from A. These message transfer can't be done using HTTP but client to server protocols using which client sends a request and server sends responses. Click here to learn more about Client-Server. In my socket programming post, I've also mentioned about polling where the server will be running all time and whenever it receives any read/write request, it will process it.
Sent + Delivered + Read Receipts *
B received the message which means it has been delivered.
Last Seen & online
Load Balancer
Group Messaging *
There are 5 users drawn in the overall architecture picture, and there are two groups. One group is in red and the other in green. Two users connected to gateway 1 and 1 user connected to gateway 2. It is too complicated for session service to handle all these information, so we decoupled these information in a 'Group service' microservice.
When a User 1 (who is in Red) sends a message in a group chat, 'session service' will ask who are the other group members to the 'Group Service' microservice. Group Service microservice will tell that 10 users exist in these group with the following IDs, and Session Service runs through its own database to find to which gateway these are all connected to. With this information, it will be routing the message to each of the users one by one.
If the group has too many members, it is too complicated. In these kind of scenarios, we are in the need of batch processing. But, the chat application is a real-time app, so we limit the number of persons in the group. Session service can handle the web sockets which send these messages to the relevant users.
As group receipts for delivered and seen is expensive, so we don't want to do that.
Parser Microservice
While parsing a message, if we want to do some smart things to find out if these are authenticated or not on the gateway, push away all these responsibilities away from gateway. The smart way to send an unpassed message to any service is having the parser microservice.
Parser Microservice is placed between the gateway and sessions service. The message can be passed in two ways:
1. Via HTTP as a JSON object
2. Over TCP using websockets
Parser will convert the unpassed message to sensible message. This is basically done to avoid any overhead in the gateway.
This will reduce exponentially a gateway's work and cost in terms of developing the software.
Consistent hashing
Message Queues
Deprioritising Messages
Image Sharing
Image storage and retrieval is explained in Tinder video.
Chats are Temporary/Permanent
If the chats are stored only in user space, it gives a lot of privacy and reduces storage issues at the service provider end if it is stored only in user's application.
Mostly all the chat based application are developed in temporary basis. If I delete my Whatsapp and my friend also deletes her/his Whatsapp, those chat messages are lost forever.
Single point of Failure
Profile/Image/Email/SMS Service
Auth Service
Service Discovery/HeartBeat Maintenance
References:
https://www.youtube.com/watch?v=vvhC64hQZMk
https://medium.com/javarevisited/whatsapp-system-design-dd1cc2e6bfb4
Comments
Post a Comment