Skip to content

Commit

Permalink
add scaling messenger
Browse files Browse the repository at this point in the history
  • Loading branch information
freemanzhang committed Jan 16, 2017
1 parent 9e28416 commit 8c29920
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions messenger.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
- [Thread table](#thread-table)
- [Initial solution](#initial-solution)
- [Scale](#scale)
- [Require less latency](#require-less-latency)
- [Sharding](#sharding)
- [How to speed up](#how-to-speed-up)
- [How to support large group chat?](#how-to-support-large-group-chat)
- [Speed up with Push service](#speed-up-with-push-service)
- [Socket](#socket)
- [Push service](#push-service)
- [Initialization and termination](#initialization-and-termination)
- [Number of push servers](#number-of-push-servers)
- [Steps](#steps)
- [Channel service](#channel-service)
- [How to check / update online status](#how-to-check--update-online-status)
- [Online status pull](#online-status-pull)
- [Online status push](#online-status-push)

<!-- /MarkdownTOC -->

Expand Down Expand Up @@ -95,17 +101,51 @@
- Pull server every 10 second

## Scale
### Require less latency
*

### Sharding
* According to userId

### How to speed up
* Socket connection for realtime push service

### How to support large group chat?
* Message table
- NoSQL. Do not need to take care of sharding/replica. Just need to do some configuration.
* Thread table
- According to userId.
- Why not according to threadId?
+ To make the most frequent queries more efficient: Select * from thread table where user_id = XX order by updatedAt

### Speed up with Push service
#### Socket
* HTTP vs Socket
- HTTP: Only client can ask server for data
- Socket: Server could push data to client
* What if user A does not connect to server
- Relies on Android GCM/IOS APNS

#### Push service
##### Initialization and termination
* When a user opens the app, the user connects to one of the socket in Push Service
* If a user is inactive for a long period, drops the connection.

##### Number of push servers
* Each socket connection needs a specific port. So needs a lot of push servers.
- The traffic could be sharded according to user_id

##### Steps
1. User A opens the App, it asks the address of push server.
2. User A stays in touch with push server by socket.
3. User B sends msg to User A. msg is first sent to the message server.
4. Message service finds the responsible push service according to the user id.
5. Push service sends the msg to A.

### Channel service
* In case of large group chatting
- If there are 500 people in a group, message service needs to send the message to 500 push service. If most of receivers within push service are not connected, it means huge waste of resources.
* Add a channel service
- Each thread should have an additional field called channel. Channel service knows which users are online/offline. For big groups, online users need to first subscribe to corresponding channels.
+ When users become online, message service finds the users' associated channel and notify channel service to subscribe. When users become offline, push service knows that the user is offline and will notify channel service to subscribe.
- Channel service stores all info inside memory.

### How to check / update online status
#### Online status pull
* When users become online, send a heartbeat msg to the server every 3-5 seconds.
* The server sends its online status to friends every 3-5 seconds.
* If after 1 min, the server does not receive the heartbeat msg, considers the user is already offline.

#### Online status push
* Wasteful because most users are not online

0 comments on commit 8c29920

Please sign in to comment.