System-Design-Notes

[System Design] Nearby Friends

Compare to [Proximity Service], data is more dynamic.

High-level design

Untitled

Workflow

Untitled

WebSocket servers scaling

Each client maintains a persistent conn to one of the WebSocket servers, which will forward the updated info. WebSocket connections handler for each active friend subscribes to the Pub/Sub.

Stateful, must be taken when removing existing nodes. Mark a node as “draining” at LB, so that no new conns will be routed to it. Once all existing conns are closed, the server can be removed.

Pub/Sub Servers

Untitled

Scaling consideration for Pub/Sub servers

Nearby random person

Untitled

Adding/removing friends

Register a callback on a mobile client whenever a new friend is added, which sends messages to WebSocket Servers to subscribe to the new friend’s Pub/Sub channel.

Alternative to Redis Pub/Sub

Erlang is a general programming language and runtime environment built for highly distributed and concurrent applications. It has Lightweight processes and is easy to distribute among many Erlang servers. We can implement the WebSocket service in Erlang and replace the cluster of Redis Pub/Sub with a distributed Erlang application. Subscription is native in Erlang/OTP.