Chapter Sixteen · Problem Ten
Proximity Service
A database index looks like the obvious answer and reads a strip around the planet. The way out is one sortable value per patch of map, cut finer where the places are.
"Design the backend for 'restaurants near me': find businesses within a radius of the user, and let owners add and edit listings."
A place is a latitude and a longitude, and a database index on those two columns looks like the obvious answer. It is not: an index keeps one order, so it narrows on latitude and then reads everything at that latitude, a strip around the planet, to find a handful of places down the street. The query on the board looks reasonable, and the interviewer waits to see whether you can say what it reads.
What it tests is mostly reasoning about reads. Chapter 3's Range, Hash and Directory tells you what a sorted index can do, and Picking the Partition Key prices the crossing you would pay if you split the map. Chapter 2's Caching at Scale and Read Replicas and Lag carry the load. The twist is that the numbers come out small, so a good answer uses Chapter 3 to say why nothing is split.
This one runs from mid level to senior. A mid-level candidate is judged on reaching a cell index and its neighbours. A senior one is judged on density, and on saying that nothing here needs a partition yet. Three requirements pull at the design. Two dimensions have to become one sortable value, and the edge of a cell must not hide the place next door. A downtown and a prairie have to live in one scheme. And reads so outnumber writes that the index can be built away from the request path and served from memory.
Give yourself the full 45 minutes first. Start a timer, sketch your own design, and write down how many rows your query reads for one search downtown. Then compare it with these lessons, which keep the same clock: Scope has the first ten minutes, First Design runs to minute twenty, Scaling It to minute thirty, and the deep dives and curveballs take the last fifteen.