Nearby Friends
Ch. 18 Case Studies System designThe problem
'Which of my friends are near me?' is the proximity problem with two extra hard parts bolted on: the things you are locating are people, and they are moving. A nearby-places service indexes fixed points; this one indexes a stream of live positions that change as the users walk, and every answer is also a privacy decision — a friend's location is only in the result if that friend chose to share it with you.
The design still leans on a grid, but the grid is now a live index: a cell holds the set of users currently in it, and that set changes as users move. A query finds the users in my cell and its eight neighbors, intersects that set with my friends, and filters out anyone who is not visible to me. Location updates are cheap and idempotent, a move only matters when it crosses a cell edge, and a privacy rule sits on the very last hop. We will build the live index, the query, the privacy, the push/pull choice, and the movement handling, then close the loop with one 'who's nearby' answer that is both correct and private.
Requirements
Functional — what it must do
- Track: a user's current location is stored and updated as they move, replacing the previous fix.
- Find nearby: given my location, return the friends who are within a radius of me, ordered by distance.
- Respect privacy: a friend appears in the result only if they have shared their location with me — never otherwise.
Non-functional — how well it must do it
- A nearby-friends query answers in a few milliseconds, from a bounded set of cells intersected with a bounded friends list.
- A location update is cheap and idempotent: a new fix replaces the old one, and only a cell-edge crossing is worth noting.
- Privacy is enforced on the read path, per pair, so a user who revokes sharing stops appearing in others' results immediately.
Scale — how much it must take
- On the order of tens of millions of users, a large fraction actively reporting location at peak, each with a modest friends list.
Assumptions — the numbers this design is sized for
A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.
Takeaways
- 'Friends near me' is the intersection of two bounded sets — my friends and the users in my neighborhood — with a privacy gate on the result; that framing makes the whole design a lookup problem with a final check.
- The index is live, not static: a cell holds the set of users currently in it, a new fix replaces the old one, and a cell-edge crossing is the only movement worth noting.
- Sharing is per pair and revocable: a location is a (me, friend, visible) rule stored apart from the position, and a revocation deletes the rule so the friend vanishes from others' results immediately.
- Put the privacy gate on the read path, applied once to the final candidate set — that keeps the index a pure location map and makes revocation clean, with no index chase.
- Mix push and pull: push a coarse 'a friend is near' signal so the map is not grossly stale, and pull the precise, privacy-re-gated pin on demand.
- Treat a quiet user as stale, not wrong: a fix carries a timestamp, no new fix means the pin is shown as stale, and the map degrades gracefully instead of lying.
Friends near me, not places near me
The answer is the intersection of two sets — the friends I have, and the people who are nearby — with a privacy filter on the result. Fixing that framing makes the rest a lookup problem with a gate.
- 1The answer is an intersection
The result is not 'everyone nearby' and not 'everyone I know' — it is the friends who are nearby. That is the set of my friends intersected with the set of users in the cells around me. Two bounded sets, one intersection, and that intersection is the entire answer before privacy is applied.
- 2People move, so the index is live
Unlike fixed places, users change position. The cell index therefore holds the set of users currently in each cell, and that set is rewritten as users move. A cell is not a static bucket of points — it is a live membership list, and keeping it current is the core write-path job.
- 3Privacy is the last hop
A user's location is shared with specific friends, not with the world. So even a correct intersection can contain people I am not allowed to see. The privacy check runs on the result, per pair, and is what separates 'a friend who is near' from 'a person who is near'. It is applied last, to the final candidate set.
The live location index
A user's fix is geohashed and stored as a membership in that cell. A new fix replaces the old one, and the index is always 'where everyone is right now'.
- 1Geohash the fix, join the cell
When a user reports a location, the service geohashes it to the working precision and records that the user is a member of that cell. The index is a map from cell to the set of users in it, and this one write is what keeps a user 'present' in the map.
- 2A new fix replaces the old
A user has exactly one current fix. When a new one arrives, it replaces the old: the user is removed from whatever cell they were in and added to the new one. The index therefore never accumulates a history of a single user's positions — it is always a snapshot of 'where everyone is now'.
- 3The cell map, at a glance
Read the index as a map and each cell shows who is in it. My cell and its eight neighbors hold the small set of users a 'nearby' query will consider — the live index is what makes that set bounded and cheap to read, no matter how many users the system tracks in total.
Find my friends who are nearby
The query reads my cell and its eight neighbors, intersects that user set with my friends list, and applies the privacy gate. Three bounded reads and one intersection.
- 1Read the nine cells
The nearby service geohashes my location and reads my cell plus its eight neighbors from the live index. Each cell hands back the set of users in it, so nine reads return a small, bounded set of candidate people — the same handful whether the map tracks a million users or a hundred million.
- 2Intersect with my friends
The candidate users are intersected with my friends list. Most of the people physically near me are strangers, so this step drops them. What survives is the small set of friends who happen to be in my neighborhood — the raw answer before privacy is considered.
- 3Apply the privacy gate
Each friend in that set is checked: are they sharing their location with me? The ones who are kept, the ones who are not dropped. The final result is the friends who are nearby and visible to me — correct, and private. A friend who is close but not sharing is simply absent from the answer.
Sharing is per pair, revocable
A location is shared with specific friends, not globally. Granting or revoking a share is a metadata write, and the check is on the read path, so a revocation takes effect immediately.
- 1A share is a (me, friend) rule
Sharing my location with a friend is storing a rule: I, this friend, visible. The rule is a small metadata row, independent of my live position. Sharing with three friends is three rows; with thirty, thirty. The position and the permission are stored apart and joined only at query time.
- 2Revoke, and I vanish from their list
Revoking a share deletes that rule. From that moment, the privacy gate on the other friend's query sees no rule and drops me from their result. Because the check is on the read path and per pair, a revocation is immediate and global — there is no cached 'yes' to expire.
- 3Why the gate belongs on the read
Putting privacy on the read path rather than the write path is what makes revocation clean. If visibility were baked into the index at update time, a revocation would have to chase every index entry. Checking it when the result is assembled means the index stays a pure location map and the permission is applied exactly once, at the end.
Do I poll, or am I told?
Two ways to keep a friends map current: the user opens the app and asks, or the service pushes a change when a friend moves. The honest design does a little of both.
- 1Pull: open, and ask
The simplest model is pull: the user opens the friends map and the service runs the nearby query on demand. It is cheap for the system — nothing happens until someone asks — but the map is only as fresh as the last time the user looked. A friend who moved an hour ago is still shown at the old spot until a refresh.
- 2Push: a friend moved, say so
The other model is push: when a friend's cell changes, the service sends a lightweight event to the friends who can see them — 'moved to a new area', not the precise coordinate. The map stays current without a refresh. The cost is a stream of events and the need to respect privacy on each one.
- 3The honest mix
In practice the service pulls on open for the full, precise map, and pushes a coarse 'a friend entered your area' signal so the map is not grossly stale. The push carries an area, not a pin, so it is useful and light, and the precise position is still fetched on demand and re-gated by privacy.
Movement and staleness
A user moving within a cell is noise; crossing a cell edge is an event. And a user who goes quiet is shown at their last cell, marked stale — the map degrades gracefully rather than lying.
- 1Only the crossing matters
A user walking inside their current cell does not change the answer to any nearby query, so the service ignores those fixes after storing the latest. The moment they cross into a neighbor cell, that is a change: they leave one cell's set and join another's, and that is the only movement worth noting.
- 2Quiet users go stale, not wrong
A user who stops reporting does not disappear from the map; they are shown at their last known cell, flagged as stale. The staleness is a property of the data — a timestamp on the fix — and the UI can dim or label it. The map degrades gracefully instead of silently showing a pin that is a day old as if it were live.
- 3The map is a snapshot with a clock
Putting it together: the live index is a snapshot of where everyone was at their last fix, each fix stamped with a time, and a crossing is the only event that rewrites a membership. The nearby query reads that snapshot, intersects it with my friends, and the privacy gate decides who is visible — a map that is current, private, and honest about what is stale.
End to end
One 'who's nearby' answer, correct and private: my friends are read, the live cells are read, the two are intersected, and the privacy gate decides who is visible — while a friend's move quietly rewrites their cell.
- 1The whole system
Every piece in one frame. Users report fixes into the live index; a nearby query reads my cell and its neighbors, intersects with my friends, and applies the privacy gate. A friend crossing a cell edge rewrites their membership and, if they share with me, nudges my map.
- 2The query, gated and ranked
I open the friends map. The service reads my cell and its eight neighbors for the users in range, intersects that with my friends, and the privacy gate keeps only those sharing with me. The result is a small, ranked list of visible friends — people who are both near and permitted.
- 3A friend moves, the map keeps up
One of my visible friends crosses into a neighboring cell. The live index removes them from the old cell's set and adds them to the new one, and because they share with me, a coarse 'moved near you' event nudges my map. My pin for them is now current, and the crossing was the only movement that mattered.
- 4Nearby, and only the ones I may see
Both jobs in one machine: 'who's nearby' answers from a bounded set of cells intersected with my friends, and the privacy gate ensures the answer contains only friends who chose to share. The live index makes the set bounded, the intersection makes it mine, and the gate makes it private.
In the wild
Friends-location services are one of the most privacy-sensitive product categories. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.
- 1The pattern, piece by piece
Each box is a standard building block of a friends-location service. Anchoring them to the classes of production systems that run that piece turns the abstract design into 'the shape behind the map that shows your friends'.
- 2Why privacy is the load-bearing wall
Independent friends-location services converge on the same pieces, but they also converge on the same hard rule: the privacy gate is on the read path and per pair. Because a location is shared with specific people, not globally, the gate cannot be baked in at update time — it must be applied to the result, exactly once, or a revocation would have to chase the index. That convergence is the evidence the design is load-bearing.