Message System & WhatsApp
Design WhatsApp:
Scenario:
- Login/Register
- Contact book/list
- 2 user send message to each other
- group chat
- User online/offline status
- History message
- 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
- Message is NoSQL, it has scale functionality (replica, maintainence, etc)
- 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?
- Add a new service - Channel Service
- For each group chat thread, we can add Channel information
- When user come online, message service will find channels that user belong to, then notify channel service to subscribe for this user.
- Channel service knows what user are alive/subscribing
- If user log off, Push Service would know, and will notify channel service remove this user from all subscription.
- Message Service receives a message
- Find corresponding channel
- send message to that channel
- Channel Service find all online users
- Send messages to Push Service to PUSH those message to user.
- Where do Channel Service store all its data/info?
- In Memory
- If fails, just reboot, message are not missing, it's just delayed.
Online Status (Push vs Pull)
- Pull is better than Push
- HeartBeat to send status to message service, online user send every 3-5 seconds
- Every 10 seconds, send a request to server to get all user status as well as notify server im alive
- Server will mark user as offline if no HearBeat within a minute.