Design a messaging app like WhatsApp
Learn about 1:1 and group messaging implementation and design
A WebSocket Server is a lightweight server with about 65k open ports per machine. If 5k ports are used for internal communication, then there are still 60k ports available for connecting with users. This means that a single machine can hold one-on-one connections with up to 60k, and the number of web socket servers needed will depend on the expected number of active users. These machines will be distributed across different locations to minimize latency.
A WebSocket Server will be linked to a WebSocket Manager, which maintains information on which handlers are connected to which users. This manager sits on top of Redis, which stores information on which users are connected to which web socker servers.
When a user's connection with a web socket handler breaks and one connects to another handler, this information is updated in Redis through the WebSocket Manager.
In addition to communicating with the web socket manager, a WebSocket server will also communicate with a Message Service, which stores all messages in the system. The Message Service provides APIs to filter messages by user id, message-id, delivery status, etc., and it sits on top of Cassandra. The Message Service needs to be built on top of a data store that can handle ever-increasing data, as new users will be added to the system daily, and all users will have new conversations daily. Due to the finite number of APIs that the Message Service exposes to web socket handlers, Cassandra or any wide column NoSQL database would be the best-suited data store for these requirements and query patterns.
Here's the video of me explaining the design 👇
Thanks for reading this edition of the newsletter!