Books Deep-Read · System Design Interview Vol 2 · Ch 1

Proximity Service

System Design Interview Vol 2 · Ch 1 · Alex Xu & Sahn Lam · 2022

中文 →

What is this chapter about?

You tap "restaurants near me" and, within a second, the app has picked the few dozen around you out of two hundred million. This chapter asks: how is that actually done? The answer is slightly counter-intuitive — the hard part is not the searching. It is assigning the Earth street numbers ahead of time.

A strange fact first

The system never measures distance. It does not take your coordinates and compare them, one by one, against two hundred million shops. It carved the planet into cells long ago and gave each cell a number. When you tap the button it asks only two questions: which cell are you in, and who else is in it?

Why the old world struggled

A location is two numbers — one north–south, one east–west. Databases are excellent at lining things up by one number, the way a dictionary is sorted by spelling. They are bad at looking things up by two numbers at once. It is like asking a library for every book whose title falls between A and C and whose spine is between 3 and 5 centimetres thick: each clue narrows things down, but reconciling the two piles means going through them book by book. Do that across two hundred million shops and it is unusably slow.

The mechanism: three intuitions

① Fold the Earth in half, and write down the folds. Cut the world in two, then in two again, and again. Each cut adds one more character to the cell's number. More characters, smaller cell. The magic is this: if two numbers start the same way, the two places are close — the pair of coordinates has been squeezed into a single string, and the database is back to the one thing it does best, lining things up by a single key.

② The radius is not for measuring; it is for picking a cell size. Looking for something within 500 metres? Use small cells. Within 20 kilometres? Switch to big ones. The cells shrink the candidates from two hundred million to a few hundred; real distances get computed at the very end.

③ Grab the neighbours too. You are quite possibly standing at the edge of your cell, with the best place in the one next door. So the system never queries only your cell — it takes your cell plus the eight around it, pools the shops from all nine, and then sorts.

How to choose

There are two schools of cutting. One cuts the whole world into cells of equal size — simple, and when a shop opens or closes you edit one row. The other cuts more finely wherever the shops are dense — fine downtown, one huge cell over the desert — so each query returns a more even number of candidates, at the cost of having to recompute a whole region whenever a shop changes.

And one more honest line: cells are square and the range you want is round. The nine-cell block inevitably scoops up shops that are actually outside your radius, so at the end you still have to measure each one and throw the strays away.

One line to remember

"Find things near me" is not a search problem, it is a street-numbering problem: flatten the pair of coordinates into a single one-dimensional number ahead of time, so the database can use the old tricks it is good at — sorted lookup and prefix matching. Everything else is housekeeping around edges and around fitting a circle into squares.

Want the mechanisms, the notation and the diagrams? → Switch to the deep read