Communication Protocol for Facebook Messenger
What happens when user A sends a message to user B?
What we require is that the user sends a message to the server and the server relays that message instantly to the user that it’s intended for. However, this way breaks the model of how HTTP requests work on the internet because they can’t be server-initiated rather they have to be client-initiated. So this isn’t going to work and we need to come up with something else.
There are a few options we can use, Let’s discuss them and their trade-offs:
1. HTTP polling
Instead of just sending one request to the server, we’re going to repeatedly ask the server if there is any new information available and mostly the server will reply with “no new information available”. Then once in a while, it will respond, “Hey, I received a new message for you”. This is probably not the right solution for this problem as we’re going to send a lot of unnecessary requests to our server, which means that we’re going to have a high latency so, we are only going to receive messages when we ask for them and not when they are received by the server
2. Long polling
In this model, we are still using a traditional HTTP request but instead of resolving it immediately with the result, we are actually going to have the server hold on to the request and wait until data is available before it replies with the result. So, we sort of maintain an open connection with the server at all times. Once data is sent back, we immediately request a new connection and then we keep that open until the data is available. This model solves our latency problem So, while it’s good for some systems like notifications but it is probably not the best for a real-time chat application.
3. Websockets
This is the solution we are going to use because it was sort of designed for this application. In websockets, we still maintain an open connection with the server but instead of just a one-way connection, it’s actually a full duplex connection. So now we can send up data to the server and the server can push down data to us and this connection is maintained and kept open for the duration of the session. There are some practical limitations to this solution as how many open connections a server can have at a particular time.
Websockets is built on the TCP protocol which has about 16 bits for the port number. This means, there’s a real limitation of about 65,000 connections that any one server can have open at a time. So instead of having one API server, we are obviously going to need to have a lot of servers to handle all of these websocket connections and we’re going to need a load balancer to help balance these connections. So we are going to insert a load balancer and going to draw in some API servers as shown below.
Designing Facebook Messenger | System Design Interview
We are going to build a real-time Facebook messaging app, that can support millions of users. All the chat applications like Facebook Messenger, WhatsApp, Discord, etc. can be built using the same design. In this article, we will discuss the high-level architecture of the system as well as some specific features like building real-time messaging, group messaging, image and video uploads as well and push notifications.
Important Topics for Facebook Messenger System Design
- Requirements
- Design of Facebook Messenger
- Communication Protocol for Facebook Messenger
- API Used for Facebook Messenger
- Database Design for Facebook Messenger
- Data Types
- Scalability for Facebook Messenger
- Conclusion