Message System & WhatsApp

Design WhatsApp:

Scenario:

  1. Login/Register
  2. Contact book/list
  3. 2 user send message to each other
  4. group chat
  5. User online/offline status
  6. History message
  7. Multi-Device login

What is DAU? MAU? --> then estimate QPS and storage

100M DAU --> QPS = 20k, Peak QPS = 100k

Storage: 10 message per day per user, 30 bytes per message, 30G of storage per day.

Service

Message Service

Real-time Service

Storage

Message Table (NoSQL) - Data volume is big, no need to change data, just like log append.

Thread Table (SQL) - Chatting Table - Need multi indexing, NoSQL is not good at multi indexing

Scale

Support more user, faster response/update/refresh time

Support group chat, online status

How to scale? - Storage

  1. Message is NoSQL, it has scale functionality (replica, maintainence, etc)
  2. Thread is in SQL DB, sharding with user_id

How to scale? - Speed up

Socket Connection - Server can actively push data to client

Http Connection - Client has to ask server for data

Adding a new service - Push Service - Maintain client and server TCP connection.

When user open APP, connect Push Service and get an socket from it.

Group Chat?

  1. Add a new service - Channel Service
  2. For each group chat thread, we can add Channel information
    1. When user come online, message service will find channels that user belong to, then notify channel service to subscribe for this user.
    2. Channel service knows what user are alive/subscribing
    3. If user log off, Push Service would know, and will notify channel service remove this user from all subscription.
  3. Message Service receives a message
    1. Find corresponding channel
    2. send message to that channel
  4. Channel Service find all online users
    1. Send messages to Push Service to PUSH those message to user.
  5. Where do Channel Service store all its data/info?
    1. In Memory
    2. If fails, just reboot, message are not missing, it's just delayed.

Online Status (Push vs Pull)

  1. Pull is better than Push
    1. HeartBeat to send status to message service, online user send every 3-5 seconds
    2. Every 10 seconds, send a request to server to get all user status as well as notify server im alive
  2. Server will mark user as offline if no HearBeat within a minute.

results matching ""

    No results matching ""