Nearby Friends
Requirements: users who opt in see friends within ~5 miles with their distance, updated about every 30 s; history kept for analytics; inactive friends disappear after a few minutes.
Estimates: 100 M DAU, 10% online concurrently → 10 M users × 1 update / 30 s ≈ 330k location updates/s; each update must reach the user's online nearby friends.
client ==WebSocket==> WS servers --update--> location cache (Redis: user -> lat,lng,ts, TTL 10 min)
| -> location history (Cassandra) for analytics
+--publish--> pub/sub channel "user:<id>" (Redis pub/sub cluster, sharded by channel)
friends' WS servers subscribe to each online friend's channel; on a message: compute distance, push if within radius
Deep dives
- Pub/sub fan-out instead of querying a geo index: each online user subscribes (via their WS server) to channels of their friends; channels are cheap, and the pub/sub tier is sharded by consistent hashing on the channel id.
- Distance check happens on the subscriber's WS server (it knows its own user's location) — no global geo index needed because the candidate set is just the friend list.
- Scale of fan-out: 330k updates/s × online friends each → millions of messages/s → many pub/sub nodes; it's CPU-bound, so shard and autoscale.
- Stateful WS servers: on deploy or failure, clients reconnect and re-subscribe; drain connections gradually.
- Extension "nearby random people": add a geohash-based channel per cell and subscribe to your cell + neighbours.