Proximity Service: First Design
Start with the query any engineer would write. A table has a latitude column and a longitude column, a search asks for rows between two values of each, and for one city that works. That is what goes on the board between minutes ten and twenty, and its best feature is that you can count the rows it reads.
One search gets followed from a position to 20 results. Then the launch it suits gets a number, and the whole world gets a much bigger one. The four questions at the end are what the interviewer does with that bigger number, so try them before you open Scaling It.
One Table and a Box
Search and edit requests go through a load balancer to application servers that keep no state. Behind them is one relational database with a table of places. The table has latitude and longitude columns and one compound index on the two, sorted by latitude and then by longitude.
A search becomes a bounding box, the rectangle that just holds the circle. An edit is an update to one row, so reads and writes end at the same database.
From a Position to 20 Places
A user asks for places within 500 metres. A server chosen by the load balancer works out the box: 500 metres north, south, east and west of the user, so 1 km tall. It asks the database for rows whose latitude and longitude are both in range.
The rows come back, and the server computes the true distance to each one, because a corner of the box is about 700 metres from the user and outside the circle. It drops anything past 500 metres, sorts the rest, and returns the nearest 20 with a cursor. If nothing is left, the app asks once more at 2 km, as Scope agreed.
An edit is shorter. A PUT reaches any server, the server updates the row, and the next search sees the change.
Fair for One City
All three functions work, and an edit shows at once, which beats the hour Scope agreed. Now take a launch in one city. Assume 50,000 places in a metro area about 50 km from south to north. A 500-metre radius makes a box 1 km tall, a fiftieth of the city. That is about 1,000 rows per search. For that launch this is the right size of system, and you say so before you name a single limit.
Where It Stops
Four limits stay on the board, named and not fixed. The index narrows on one coordinate only. The database is one failure. Every read does its filtering where the writes land. And a dense downtown and an empty county get the same box.
The first limit has a number. Assume the places lie in a band about 5,000 km from south to north. A 1 km strip is one five-thousandth of that band. One five-thousandth of 80 million is 16,000 rows per search, to keep 20.
Volunteer that before you are asked, in about ten seconds: "One city is fine, at about 1,000 rows a search. Worldwide that strip is 16,000 rows, 4,000 times a second, so rows read per result is what I'd watch, and the index is what I'd change."
Changing the index will take it off the request path sooner or later, and then it can fall behind. That is a question for later, and you can hold it there by name: "I'll treat keeping the index fresh as solved for now, since we agreed an hour, and come back to it once reads are settled."
- Opening with geohash or a quadtree because the problem is known for them. A named index with no design around it gives the interviewer nothing to push on.
- Claiming that a compound index on latitude and longitude solves it. That is what the first probe tests, and this design is honest about it.
- Skipping the true-distance step, so a corner of the box counts as inside the circle and the 20 results are wrong.
- One search walked from position to box to rows to distance to 20 results, which shows the design was thought through and not recalled.
- Why it is fine for one city, said before why it is not fine for the world.
- The limit given as rows read per search, not as "it gets slow".
Knowledge Check
"Your query puts one range on latitude and another on longitude. What does the index actually do with those two conditions?"
- "It finds the box directly, because the compound index covers both of the columns."
- "Add a second index on longitude, and let the planner intersect the two results."
- "It narrows on latitude and scans that whole strip, so I'd index one cell ID instead."
- "Keep the query and cache each search by its exact coordinates for a few minutes."
"Searches come in by the thousand every second and owners edit a handful. Where should all those reads go?"
- "Split the places table across several nodes by cell, so each one takes fewer reads."
- "Keep the whole cell index in each search server's memory, built off the write path."
- "Put a search engine with geo support in front, and send every search through it."
- "Cache each cell's full records in a shared tier, and let them expire on the hour."
"Five hundred metres brings back thousands of places downtown and nothing at all out in the country. What do you do about that?"
- "Use the small 7-character cell everywhere, so that a downtown search stays cheap."
- "Keep a table of radius per city, smaller downtown and larger out in the country."
- "Return whatever the cell holds and let the client filter it and page through it."
- "Let cell size follow density, and split a cell once it holds more than a set cap."
"The user is standing a few metres from the edge of a cell, and the nearest restaurant is just across that edge. Do you find it?"
- "Read the cell and its eight neighbours, then filter all of them by true distance."
- "Drop one character from the cell ID, so that both cells fall under one wider parent."
- "Shift a second grid by half a cell, and look the user up in both of the two grids."
- "Round the user's position to the centre of the cell before doing the lookup at all."
You got correct