Multi-user Chat
Materials — open to everyone, no sign-in
Topic: Multi-user Chat
Interviewer: system
Level: L4 (Experienced Individual Contributor)
Additional Resources:
System Design Interview
Join Us on Wechat
Multi-user chat
[07]
Functional Requirements
1-1 chat + group chat
Support group admin
Group chat, group profile
Limit on group: max number in the group: 500
Chat messages (100,000 chars)
Pictures
[14]
Only to notify when login
Q: Can a user use multiple devices at the same time?
A: Assume one user can only use one device at a time
Non functional requirements:
Q: priority
A:
1 scalability DAU 10M, total 50M, low latency
2 low latency (10ms)
3 fault tolerance (no wrong messages sent, no duplicate)
[ throughput, storage volume ]
[23:20]
High level design
【27:30】
Database schema
Each user maps to multiple groups
Each group contains multiple users
[relational or non-relational? List is not suitable for relational]
GroupTable: groupID, groupHost, groupNotice
GroupMembership: groupID, userID
[34]
Walk through 1-1 message
Offline users: need to store the message first
[how to deliver to receiver2?]
Like to ensure the user is logged in
1:1 chat: user1-> user2
Group chat: user2 -> user1&2&3
[39]
[how does receive message service send to receiver 1,2,3?]
Add message queue
Each topic corresponds to 1 group or one chat
[42]
50k
XX M topics
When the user logs into a device, they start to subscribe to the topic
[ likely too many topics ]
[46]
Kafka: can be used as storage. Can store long retention period.
Or we can store message into the storage
DB: use sql database
Data is well structured
Data Volume is low
Store large messages in document database
10M users
[51]
[ SQL may not work well due to high throughput ?]
Add receiver service
When a user logs in, it talks to the receiver service.
Each receiver manages multiple consumers
Q: Assume we have an existing push notification service. We don’t need to worry about how to send messages.
A: it’s similar to a message queue
[FCM]
[58]
How do we map user to topic?
[ interviewer is thinking about a system like FCM.
Interviewee is thinking about a self-built system w/ websocket manager that subscribes to messages on behalf of the client
In either case, we need some mapping from user to topic. This appears missing in the database schema definition.
]
How to manage group information?
Add group manager service
Membership changes should be pushed to the receivers
What protocol to use between client and messaging service?
FCM
Audience discussion:
User can be stored in SQL database
10 million x 1 k information = 10G
50 million
Message: stored in noSQL
===
10ms delay may be too short
500ms may be more reasonable
Aggregate time
Apache Flink
Consistency - may not be the requirement
Sequence is difficult to guarantee
Timestamp cannot be guarantee sequence
Lamport timestamp
Vector timestamp
Using logic to guarantee sequence
Still not 100% solve the problem
Timeshift may go to 1 minute
===
When to store messages on the server?
Never store
Store only if receiver is offline
Complication for multiple clients for the same user. Some clients are offline, some clients are online.
Always store
===
How do we shard?
By time
By user
By chatID
===
Upcoming events:
Thursday: performance review
Friday: system design interview
1:24:49:10
1:25:47:09