SD-16 · Back to the write-up
Design a Nearby Places and Friends
Find places from geohash cells kept in memory, and push friends’ moves through a channel per user over WebSockets.
Follow a request
Search · cache hit
Search · cell miss
Friend moves
Open nearby friends
WebSocket server dies
Owner edits a place
Clients
Edge
Services
Data
PhoneApp with GPS
Load balancerHTTP
WebSocket serversLive sockets
Search service9 cells, filter
Place serviceDetails, edits
Pub/sub ringChannel per user
Friend graphWho shares
Geo cell cacheCell → place ids
Places DBRows + geo index
Location cacheTTL 10 min
Location historyLog, 30 days
The numbers, at a glance
1 update ≈ 334,000/s10 deliveries ≈ 3.34M/s
31M
83M
2510M
7530M
6 GB geo index400 GB of place rows
A friend’s move, in order
Ana and Ben are on different WebSocket servers; the pub/sub node in between owns Ana’s channel.
Ana’s phone
Ana’s WS server
Location cache
Pub/sub node
Ben’s WS server
Ben’s phone
1 { lat, lng, ts }
2 SET loc:ana TTL 600
3 OK
4 PUBLISH user:ana
5 message
6 Ana · 1.2 km
RequestResponseAsync, does not block
How to find what is near a point
The decision this question is really about.
lat BETWEEN …
index on lat only
filter thousands of rows
+ Any SQL database.− Scans a strip of the map, 20 reads a search.
root square
split until ≤ 100 places
rebuild on start-up
+ Adapts to density.− Custom tree to build and sync on every server.
point → "9q8yyk"
9 cellscache hit 95%
filter by true distance
+ Plain strings: a B-tree, cache key or shard key.− Always read 8 neighbours.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Phone
App server
SQL database
Phone
Load balancer
Search × 9Place service
Geo cell cachePlaces DB
Phone
Load balancerWebSocket servers
Search × 9Place servicePub/subFriend graph
Geo cell cachePlaces DBLocation cache
Phone
Load balancerWebSocket × 100
Search × 9Place servicePub/sub ring × 25Friend graph
Geo cell cachePlaces DBLocation cacheLocation history
Was this walkthrough useful?Your vote tells us what to improve next.