Whatsapp System design or software architecture

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is Mary and in this session I am going to explain the system design or architecture for the air time messaging system what's up this is a famous interview question asked by many companies like Amazon flip card and many other startups before we dive into understanding of how the system design of what's up works let's get some basic understanding of what are the components we need to build a chatot application consider our ideal scenario where there are two clients flying a and point B now if these two guys wants to exchange some information how do they want how does it us play a knows errors of point B and point B also knows the others okay so they can send the information to point B and vice versa and consider this is a small network they know there's what if this network is fake what if this network is internet if it is internet cancer there's billions of user which are using this patent application in this thing I drew it is very difficult to know the address of each and every client in the Internet right that is not feasible scenario to make the system robust and a high available what we need to do is bring something called as several in between ok this is so the work of this server on our the job of the server is to coordinate messages between all the clients which are connected to this particular server consider I have also connected one more client one two three four right if we want to send message to to what it do what it does is it comes to the server send a message and the server knows the address of the client - and it redirects the message to - and similarly if we wants to send message to this client three sends a message to suffer and server sends the message to find it this is how this is what this is how the system design for a real-time messaging system looks from thousand feet high now let's see the actual system design now let's discuss about active system design of whatsapp if you are attending an interview and if the interviewee interviewer ask you the question like designs whatsapp or design a real-time messaging platform don't jump right into the question and start answering that I need this system I need this component over here take it take some time like take some minutes and then think about all the features you want to incorporate in your design or in your application and start building around it if you're in confusion or if you have doubt please feel free to ask your interviewer that do and if support this particular feature or this particular feature what not so now let's do the same thing now for the whatsapp which we are going to design in here let's think of all the features which we want to support the first one I can think of is the question I will really ask is what is the scale at which the application works are what is your use of this user base is really important use of this user base is really important to understand how big do you how many users are actually is going to interact with your application that means that the design drastically changes the design for real-time messaging if we have a couple of was a design if you have millions of user is very different okay the second feature which I am going to think now is build really neat but do we need a feature like last seen time of our particular person like last seen you love seeing that a person was seen last three hours ago something like that doing in the future the next thing which I can think of is do we need support to send media messages when I say in media the message is like images are audio etc so the we need to support these kind of messages and the next thing I can think of is do we need to encrypt our messages anchored yes security is a lot important to keep our messages secure we need to encrypt our SMS sorry the messages so the next thing which I can think of is do we need to support why our telephony services that is very funny like for your caller video god Sam said that thinking all these features let's go ahead and design the system for Worster consider we have two bodies that is client a and point B now as I already mentioned we need a server in between these lines to establish a connection I decide the messages back and forth now let's write server and let's name this server part as messaging server now look at the feature use of this when we know that our user base is will be in terms of billions of users then definitely what messaging server we can't can't it's time to go so now with that safe we need multiple messaging server as you know when we have multiple messaging server it is horizontally dissipated or horizontally scale or distributed servers right these are the clusters also in this case the clients can't connect to the messaging server randomly that's when the load balancer comes in between we need a load balancer no balancer so when we shall go balancer what happens is when the client 8 tries to connect to the messaging server the client client a or find B or any other clients fits first in load balancer and the load balancer will decide which server to commit based on different parameters like load on the server or on the session which the client is earlier connected to and different parameters the same thing happens even when the client we tries to connect it was fits the load balancer and the load balancer will offload the request or redirect the requesters any one of the server ok now let's talk about what kind of connection ok before we talk about retention we need one more complaint that is DV we need DV to sale some arbitrary status arbitrary states the messaging servers will talk to Vivi to retrieve some stage or to save some stage or to save messages in certain conditions let's talk about how the connection works the connection over here is far as two minutes or email address SS actions me by lachemann when the connection established from the client a to messaging server either the messages can be originated in the client and flows to the messaging server or vice-versa the messages might originate from the messaging server and close to beat client ar5 p that means the messages can be audited on any other end of the connection and can send to alpha that is far as duplex function I wonder the different kind of connection we can establish there are different kind of the connections like TCP connection unity connection are you can think of WebSocket or long polling there are different kind of question the most common way of connection happens we're simply TCP connection where the socket is where the connection is established from the client to server so now let's take some scenario and see how the message flows in the system consider quite a wants to send a message to point B what happens now is playing a has the message now if the client hey a consider the client is not connected to the system as a cloud now what happens now look like it has the message and this message will be saving the local database of your application that is in Android say sequel I an iPhone may be symbolic or any other lightweight database which is used by either the message will be will stay right in your mobile and as soon as the client connects to the messaging server the caption establishes and this message will be pushed to the messaging so once the mythic server receives this message what it does is it checks for the connection which X looks for the connection or the process which is responsible to send the message to quite be enhanced it has this message over to that particular process and that message will be sent to client B for some reason if clanky is not tempted to the internet or to the messaging server what happens to this message this message will be saved into the DV okay and then as soon as the messaging server learns that the claim B has connected back to the messaging server it retrieves a message from the VB and then sends the message to client be using the connection which is established in all these scenarios the clients are the one who established the connection not the server will never try to establish connection themselves to the plane because the server will never know the address of the client but the client know the address of the server that is the reason always the client starts the tension and once the connection is established anyone can send a message at any point of the time so this is how the messaging works now let's talk about the acknowledgments the message acknowledgement in case of all the lasting features and everything so in case of water you don't see that whenever you send SMS you will see Quantic whether the message is delivered you will see two ticks whenever the message is read you will see blue tick how do all of this these things work let's go back to the same scenario the client a wants to send message to point B when the quite a sends a message the message originates from here and the message reaches to missile as soon as the messaging server receives the message the message server sends one sensor information to client a saying that I have received your message successfully but when 28 displays be signaled that the message has been received in the server and that the message has reached user once the license server finds the appropriate connection to the client be the message will be - defiantly that's right claig be sends a response to the messaging server that I have successfully received a message that's when you will see two dates that message deliver message deliver and then once the user opens Watsa or any other chatting application and read the message that's when the client sends one more message to the messaging server that the particular message has been read and the Misses server in turn transfers back that message to the client aid saying that the message you sent has been played that's when the client will show that the blue color take that message has been done right this product management works in all the case there will be a unique ID used for message to identify what acknowledgments are responsible for what particular message okay so let's discuss the messaging server in depth so I was always keep on telling that the message server will find a connection and sends the message to the client B let's discuss how exactly this messaging server works now I have written a big bar let's call this as misty Jean so so how this work messaging server questions internally so every time when when a connection is established from a planet to the server they will be a respective process I write the process in lacquer a process our trade will be created and that thread which is long-running is responsible for handling messages for this particular consider the claim he has established a connection so there is a thread or a process which is greater inside the messaging server and that will be always responsible to handle any messages transferring or acquired in receiving the acknowledgement and other stuff this guy will handle everything and they will be an eID our thread ID or something like that and they'll be a respective queue associated with every thread or process what the skewness is it is a buffer where the messages can be sent by any other different processes now let's take a hypothetical scenario that there is one more connection happen from point B as soon as the tension establishes we need to create create a thread or a process to handle the messages to this particular plane and then we are pipe or queue which is also created which saves the messages are which acts as a buffer for the messages now take a scenario allocate after are doing all these things will add entry and this would also had a PID build and I include the BB that what is the process VI e say VI DX and then respective queue queue name or q ID or something our RV I'm sorry and as far as device ID I can simply mention this UI are usually the process let's state a table called PID and UI what happens is when the connection happens there will be a process created for point a and it has a classified say consider the person ID is 1 and which is associated with plenty and there will be one more when when the connection happens from client be one more process is created and until the first PID - for the ID the you IDs B now what happens is considered a wants to send a message he sent a message tensor is sent a message and this message goes and straight this message will be received by the process of ID 1 okay how does it know because the tension is already established and this particular process our thread is keep on listening to the connection and it receives the message as and when the client is sent it will take this message and where is the BB give me the process ID which produces which is responsible to handle the messages for client B because this message is designed for time V sent by plan a it such as in the table and finds that the client B is handled by a process called - with a PID - now what this process with B ie one does this sense this message to the Q which is responsible for handing point B this message goes and sits inside the Q of the process PID - now what this process it keeps on looking at the cue as when when a message arrives to the cue it relieves that message and sense through the connection which it has re-established so the message reaches to plan B okay and the same thing happens back and forth when the message sent by the point B this process finds the UID of the client which is responsible for point a and says sum is a to the Q which is responsible for that and then the message will go back to the point a this is how the system work you can ask that what if the client B is not connected to the messaging server if the client B is not connected to the messaging server there will not be any server or any process or thread which is responsible to handle the client week okay then this entry will not also be there so then consider now what if a wants to send a message to point B now they knows that I need sent I need to send a message to point B it sends a message to point B and this message is arrived here decide to be now this particular process such as far such as in the table which process is responsible to handle find me it will never find it we'll never find a process which is responsible that's when it loves that the client B is not connected to the system at all what it does is it takes this message and saves it in one more DB go back to the CV and table which has UID vs. message so it says all the messages which are which could which it couldn't deliver to the client and it will be there and when the client B connects to the system a new process will be created with P is a three and as soon as it creates rational process is stated modesta connection is established it will update its entry that my Giri is clean and I'm responsible amended message for client B and also what it does is it looks fat the levy do we have any under er of unsent messages exactly it will find a message here and it will read and it will start sending the message to point B this is how it functions when client B was offline and came online now you have learned how misleading server works let's discuss hope implement other features which we have mentioned over here okay now let's retain this design just the way it was and then let's discuss how to implement they last seen last time how to hook implement it so what we need here how do we know that when applying ei or Phi P was lasting it's very simple all we need is a heartbeat our last seen heartbeat which will be always sent by the client and we should be keep on staying in the table consider consider we have a table which has a user ID and times time what is what is that permission really the word to save is consider this fight is configured to send a heartbeat every 5 minute once if the user is actively using the application only what it happens is when the user is using the application every 5 second once the client is keep on sending a heartbeat saying that the application is being used by the user as men the hardwood is assumed in the messaging server the messaging server keeps on adding the entry in the table shown here when a sense and heartbeat it will optic a and the last same time Sam like epoch time or over time say updated say 2018 7 one over seven and then world time okay when the next message and after they immediately after five second one more hybrid keeps on sending to the messaging server and doesn't this this particular times time will be always updated okay as soon as the user closes the application the heartbeat will be stopped and last entry was made will retain in the table if someone adds some of the clients want to know what was the time at which the playing a was lasting we can always read from this table and display that one hour ago to a record ten days ago three days ago that's it so let's discuss how do we support the third feature media so odds and voice mode or pictures where our whatsapp right how do we do that we're not going to use the same connection to send the pictures or voice because the connection is kind of like wait I think it's not that good let's use HTTP to support this particular now let's draw one more component to the existing system design that is HTTP server HTTP sorry what it does is it is going to handle all the media content which the server also should be having some kind of CDN or some kind of TV which saves all this information okay what it does is whenever we try to upload a image what kind hey does this H is not going to use the existing connections with the messaging server it is going to upload that image directly to the HTTP server once the image is uploaded to the HTTP server this server is going to return the hash or unique ID for that particular resource which we just referred it might be image or it could be a re or something we get the hash back to the point what we are going to send to client B is just a hash and the type of media information only the message will contain the hash and the media that it could be image or volume and this message will be sent to point B when the message is received on the client B the point B is going to use the hash and downloads the media from the same issued server our Syrian Network that's it it works for the video I mean it works the same way for video it was the same way for audio it was a team effort image now let's talk about our fourth feature how encryption work the intention can be of two kind right you can have one key which is shared between two parties and both part use the same key to encrypt the message and a sent a message to the other party and back and forth when the messages use the same key and decrypt this is a smooth way or the other way is you have a private key and a public key you're going to share a public key to the other party or any other parties and you will keep or escape the private key this is far as a symmetric encryption and this can also be used okay so let's let's not talk about telephony services for that we have to include all the you know white servers or the chat the video calling servers are not let let's keep the design simple let's not support this particular and I'm here this this is this is some design for what's that okay so now that we learn this some design for real-time messaging platform or whatsapp I'm going to explain you what were the technologies whatsapp in reality uses so the language used by whatsapp is for few reasons and Ike is a pretty fast and really great performance and he also supports like hard upload or you can they can think of it as updating the code on the fly and also it supports something like lightweight trade and which is ideal case where we won't help millions of functions established in a persistent connection established to your servers what's up guys have managed to handle about 10 million connections in a single messaging server that's really great and it's something which is highly impossible only if you know these system in and out and if we can tweak your kernel of the server and networking libraries etc maybe you can achieve but it's it's a really great job than my website developers and the operating system used in the messaging server is FreeBSD the reason behind previously is simple like the developers knew previously in and out so that they can get the maximum benefit out of it so they did they did go for previously apart from that the BBU which they used is amnesia which works really great with Erlang it is also a key value pair based database yeah I think the web server which they use for hanging the connection and everything is why eight of us that is at another web server I think that's about it have left all the links which I used to learn the system design of whatsapp in the description if you like this video please do subscribe and share with your friends thank you everyone
Info
Channel: Tech Dummies Narendra L
Views: 182,638
Rating: 4.8231931 out of 5
Keywords: System design, software architecture, learn system desgin, interview question, amazon interview, google interview, flipkart intervew question, myntra interview, interview, software interview, developer, python interview question, easy interview preperation, learn software architecture, wallmart interview question, linkedin interview question, whatsapp system design, whatsapp architecture
Id: L7LtmfFYjc4
Channel Id: undefined
Length: 27min 40sec (1660 seconds)
Published: Thu Jul 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.