Design Topics in
Instant Messenger
Shijie Zhang
A bit about myself
Approach
History
Now
Problem def
Group chat - Scenario
Problem def: Comparison on group chat scenario
| Snapchat | Broadcast chatroom | Slack | What we want to design | |
Max chat group size | 256 | No membership concept. 11.2M for Royal wedding) | 10K level (e.g. Microsoft Teams limitations 25K) | Fixed group membership. 500 | |
Send multi-media | Voice, video, image (filters) | Yes (most typical files) | Chat only and reactions | All file types | No |
Anything security / Privacy | Disappearing msgs | E2E encryption | No | No consumer functions. But enterprise compliance | No |
History msg on cloud | No | No. All on client devices | No. Even fine to drop msgs. No search func | Yes. Complex query demands | 1.Stores on cloud. 2.Search around a time range |
Typical IM system architecture
Outline
Group chat - Scenario
Group chat - Basic components
Group chat - Scenario
Group chat - User2 as online receivers
Group chat - User2 as online receivers
Group chat - User2 as online receivers
Group chat - Scenario
Group chat - User3 as offline receivers
Group chat - Scenario
Group chat - Message roaming
| | Online/Offline message buffer | Message roaming |
Diff | Sync triggers |
|
|
Freq | High | Low | |
Retention limit |
|
| |
Solution | Storage pattern |
|
|
Storage engine |
Group chat - Message roaming
Overall naive flow
Outline
Group chat - Storage
| | Online/Offline message buffer | Message roaming |
Diff | Sync triggers |
|
|
Freq | High | Low | |
Retention limit |
|
| |
Solution | Storage pattern |
|
|
Storage engine |
Group chat - Simplified storage requirements
For a given user, load all active group chat after last sync
For each active group chat, load msgs
after last sync
Group chat - Initial scheme with write amplification
Group chat - Only store one copy of msg_content
Group chat - Storing only the latest msg id per user/group
Only store last msg id?
Group chat - Storing only the latest msg id per user/group
Could we do
better?
NonFunc
NonFunc - Realtime
NonFunc - Realtime - Protocol
| Short polling | Long polling | Websockets | MQTT | SSE | Webhook | Streaming protocol |
Throughput | Low | Low | Moderate | Low | Moderate | Low | High |
Birectional | No | No | Yes | Yes | No | Yes | No |
Initiator | Client | Client | Client | Client | Client | Server | Server |
Example use case | REST | REST | Chat/location apps, HTML5 | IOT | Live events update | Email notification | Kafka based logs |
NonFunc - Realtime - DB latency
NonFunc - Realtime - App layer heartbeat
NonFunc - Ordering
NonFunc - Ordering
NonFunc - Ordering
NonFunc - Ordering - MessageID
| UUID | Meituan Leaf snowflake | |||
Unique dim | Global | Global | Global | Per user | Per chat |
Increasing | No | Yes | Yes | Yes | Yes |
Continuous | No | Yes | No(for security) | No (for perf) | No |
Internal | NA | DB step | Snowflake | Customized | Customized |
NonFunc - Ordering
NonFunc - Ordering
NonFunc - Reliability
NonFunc - Reliability - Resend and dedup
NonFunc - Reliability - Local sequence counter
NonFunc - Scalability
NonFunc - Scalability - Batch operations
NonFunc - Scalability - Batch operations
Future topics
Summary
References: