- Scenario
- Features
- Register
- Contacts
- Message
- Between two users
- Between multiple users
- Online Status
- Chat History
- Multiple Devices
- Request
- Reality: 1B active users. 750M daily active users.
- Assumption:
- 100M daily active users. 20 messages per day.
- Average QPS 100M * 20 / 86400 = 20K. Peak QPS = 20K * 5 = 100K. Each message 30 bytes. 60G data storage.
- Features
- Service
- Message Service
- Real-time Service
- Storage
- Message Table
- Basic: id; from_user; to_user; timestamp; content
- Problem: SQL query is really slow; Not extendable for group chat
- Solution: Add thread table to accelerate chat query
- Improved: id; thread_id; from_user; timestamp; content;
- Primary key: id
- Sharding key: thread_id
- NoSQL: Massive data; No modification.
- Thread Table
- Basic: id; participaint_id; created_at; updated_at
- Problem: Cannot query which threads are the user involved; Cannot have private information
- Solution: Add owner id and duplicate the thread table for all participants
- Improved: owner_id; thread_id; participaints_id; is_muted; nickname; created_at; updated_at;
- Primary key: owner_id & thread_id
- Sharding key: owner_id
- SQL: (NoSQL doesn’t support secondary index well)
- Index by:
- owner_id + thread_id (primary key)
- owner_id + updated_time (sorted according to time)
- Message Table
- Solution
- Basic Solution:
- The client sends message thread to the server;
- The server creates a thread for all receivers.
- thread_id can be unique.
- multiple users: creater_id + timestamp;
- two users: two user_id sorted;
- The server creates a message.
- The receiver pulls messages from the server.
- Realtime Service:
- Use sockets to push messages when the user is actively using the application. (Linux supports up to 1M socket connections, but is performance bound)
- Http: TCP short connection. The client can only pull data from the server.
- Socket: TCP long connection. The server can push data to the client.
- Use pull at app launch / Android GCM / IOS APNS service when the user is not actively using the application.
- Use sockets to push messages when the user is actively using the application. (Linux supports up to 1M socket connections, but is performance bound)
- Work Flow:
- A launches app, asks the webserver for a push server connection.
- A connects push server through socket.
- B sends a message to the webserver.
- The web server stores the message and sends it to the push server.
- The push server pushes the message to A.
- Basic Solution:
- Scale:
- Group chat
- Lots of participants. Lots of push requests.
Solution: Add channel service (in memory). Message service sends a message to the channel service and channel service sends a message to the subscriptions (online users).
- Lots of participants. Lots of push requests.
- Online Status
- The webserver needs to know who is online
Solution: Heartbeat - The user needs to know which friends are online
Solution: Webserver summarize
- The webserver needs to know who is online
- Group chat
Leave a comment