Design a Nearby Friends Service
The prompt arrives sounding familiar: "When a user opens the app, show which of their friends are nearby, and notify them when a friend shows up. Everyone is moving all the time." Within seconds most candidates have drawn a quadtree. They have recognised the shape of the question — points on a map, a radius, a search — and reached for the index that shape implies.
That recognition is the trap. In a Yelp-style nearby search the data sits still and the reads dominate, so you build a geospatial index once and query it. Here every user is simultaneously a moving write and a reader, the data is worthless within a minute of being written, and the search set is not everyone near me — it is my friends, a list of about four hundred identifiers that the social graph already maintains. The friend list is the index. A candidate who notices that in the first ten minutes designs a different and much better system than one who does not.
The Trap Built Into a Familiar Shape
Two failures show up in this question again and again:
- The Index Reflex: builds a geospatial index over all ten million active sharers and runs a radius query on every app open, then intersects the result with the friend list. It can be made to notify on arrival — query the index at write time and intersect the candidates with the friend list — but the costing is brutal: thousands of candidate rows scanned per update against the roughly thirty-eight keyed cache reads the friend list needs, and every one of those scans touches the rows of strangers who were never party to the question. Signal: pattern-matched the shape instead of reading the problem.
- The Realtime Maximalist: a durable log, a stream processor, exactly-once delivery and sub-second updates — engineering for permanence and precision on a payload with a thirty-second shelf life, for a feature whose human-perceivable freshness need is about five seconds. Signal: cannot state what guarantee the product actually requires.
The passing performance computes the fan-out arithmetic early, lets that number choose push over search, and then spends the rest of the hour on the things that genuinely bite: notifications that flap when a friend cycles along the boundary, stadiums that empty into a single map cell, an off switch that has to erase and not merely stop, and ten million phones reconnecting at once.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson