Design A Chat System

Design A Chat System

Polling is a technique that the client periodically asks the server if there are messages available. Depending on polling frequency, polling could be costly. It could consume precious server resources to answer a question that offers no as an answer most of the time.

Screen Shot 2022-09-08 at 11.04.42 PM.png

Long polling Screen Shot 2022-09-08 at 11.06.10 PM.png WebSocket* Screen Shot 2022-09-08 at 11.08.13 PM.png

Screen Shot 2022-09-08 at 11.11.25 PM.png *High-level design

Stateless Services Stateless services are traditional public-facing request/response services, used to manage the login, signup, user profile, etc. These are common features among many websites and apps. Stateless services sit behind a load balancer whose job is to route requests to the correct services based on the request paths. These services can be monolithic or individual

Stateful Service The only stateful service is the chat service. The service is stateful because each client maintains a persistent network connection to a chat server. In this service, a client normally does not switch to another chat server as long as the server is still available. The service discovery coordinates closely with the chat service to avoid server overloading. We will go into detail in deep dive.

**Screen Shot 2022-09-08 at 11.06.58 PM.png

Two types of data exist in a typical chat system

  1. The first is generic data, such as user profile, setting, user friends list. These data are stored in robust and reliable relational databases. Replication and sharding are common techniques to satisfy availability and scalability requirements.
  2. The second is unique to chat systems: chat history data. It is important to understand the read/write pattern. Screen Shot 2022-09-08 at 11.19.24 PM.png

Message synchronization across multiple devices

Screen Shot 2022-09-08 at 11.20.07 PM.png Screen Shot 2022-09-08 at 11.21.37 PM.png

Screen Shot 2022-09-08 at 11.22.01 PM.png

Screen Shot 2022-09-08 at 11.23.29 PM.png