How to Design the Backend of WeChat
Notes from Tiger’s Lecture
How to communicate between two users?
Client Server model
1 | User 1 <== connection ==> User 2 |
How to connect
1 | A <==> B |
Could we build WeChat without Server
How to save traffic
- user persistent connection
- P2P 不稳定 & 法律监管 & 带宽环境变好
- 批量压缩
- web socket in http
pull push model 消息延迟加载,先推送新消息的计数,等打开 app 才刷新消息的具体内容,如果消息多的话还可以压缩以便进一笔节省流量
What data need to be save in client
- message
- account
- contacts
Sync between two clients (mobile/desktop)
timestamp,但是用户的时间可能是错的,所以用逻辑时钟/版本号
user pull version or server push
How to build API
Scenario
- send message
- account
- read message
- 红包
- log in/out
- 朋友圈
- location
=> chat, account, feed
client ==> 顶层业务 API ==> 逻辑 API / DATA API (可并行串行,微信用并行) ==> DATA
How to communicate between services?
common gate way interface
1 | API |
1 | Micro service |
How to support QQ message
How to support group chat
异步 RPC
disruptor
设计广播/feed模式
假设群内有 u1 u2 u3,群发就是 u1-u2,u1-u3,…,etc,而不是 u1-channel (逻辑复杂),微信群设人数上限
How to configure API
How to store data
- message
- account
- contacts
Should we save them together
What is the key
- message, NoSQL
- account, SQL
- contacts, SQL
Why do we need memory
在内存里进行复杂的 join 等操作
Is one data center enough
容灾
How to monitor the number of registered users
MySQL count
How to monitor the number of online users
log,上线了就+1离线了就-1,注意掉线的情况
How to notify the numbers to team
What are the steps for log
分布式 log
- log统计代码
- 保存在本地或者发送到 log server 上 (每台机器上有个进程专门处理 lock,加减等操作)