Lesson 1 · Minutes 0-10

Proximity Service: Scope

Problem

Minutes zero to ten here come down to six questions, and one of them decides whether you are solving this problem or a much harder one. The question says "radius" and nothing else: not how many results, not how many places, not whether those places stand still. This lesson asks those questions, writes down what is agreed, does the sums, and ends on the API.

Expect the sums to come out small. That is the finding, not a disappointment, and a candidate who says so before drawing anything has already ruled out most of what people bring to this problem.

Six Questions

Ask them in whatever order the conversation allows. Each answer moves something on the board.

  1. What does a search return: everything inside the radius, or the nearest few? This sets how much each search has to read, and it decides whether density is a catastrophe or a detail. Agreed: the nearest 20 inside the radius, by distance, with a next page.
  2. How many people search, and how often? This sets the read path. Agreed: 50 million daily users, 4 searches a day each.
  3. How many places are there, and how much is held about each? This says whether storage forces a split. Agreed: 80 million places worldwide, about 2 KB each.
  4. What radius, and what happens when nothing is inside it? The radius sets the cell size. Agreed: 500 metres by default. On an empty result the app widens once to 2 km, then says nothing is near.
  5. How soon must an owner's edit show in search? This decides whether the index can be built off the request path. Agreed: within an hour. A curveball tightens this later.
  6. Do the points ever move? Agreed: no, places stay where they are, and live positions are out for now.

The last one takes ten seconds and is the one to volunteer, because everything after it leans on the answer. You could put it like this: "Do these points ever move? If places stand still I can build an index ahead of time. If they report a position every few seconds, that whole idea is gone."

The Agreement

Three things are functional: search by position and radius, read one place, and add or edit a listing as its owner. Three more set the bar. A search answers fast and almost never fails; assume 100 milliseconds on the server. A result may be up to an hour stale. An owner's saved edit is never lost.

Then rule things out while it is cheap to object. One breath is enough: "Search by text, ranking beyond distance, reviews and photos, maps and routing, and checking that an owner really owns the place are all out, unless you want one of them in."

The Sums That Decide

Searches first. Fifty million users at 4 a day is 200 million a day. Over about 100,000 seconds, that is 2,000 a second, and 4,000 at peak, twice the average. The read path is sized to 4,000.

Now the edits. Assume 200,000 edits and new listings a day, about 2 a second. Searches outnumber them 1,000 to 1. So the writes shape nothing, and the index may be built away from them.

The estimates, and what each one decides
Edits200,000 edits and new listings a day
About 2 a second, 4 at peak. Decides nothing.
Searches1,000 searches per edit
About 2,000 a second, 4,000 at peak. At 20 record reads a search, 2 cache nodes.
Bandwidth out40 KB a search
About 80 MB a second, 160 MB at peak. One 10 Gbps link. Decides nothing.

Storage answers the question everyone assumes. Eighty million places at 2 KB is 160 GB, and 480 GB with three copies. Against an assumed 2 TB a node, each copy fits on one machine with room to spare. So nothing here is forced to split, and you say so now, before anyone reaches for shards.

The size that matters more is smaller still. To find a place, a search needs only its ID and two coordinates, which is 24 bytes. Times 80 million, that is about 2 GB before overhead. One mid-size cache node has 100 GB of memory. That decides where the index can live, and you park it until the reads come up.

The two sizes that decide
The records80 million at 2 KB
160 GB, or 480 GB with three copies, against an assumed 2 TB a node. Nothing is forced to split.
The searchable partID and two coordinates, 24 bytes
About 2 GB before overhead, against 100 GB of memory on one node. The index can live in memory.

Bandwidth takes two sentences. Twenty results at 2 KB is 40 KB a search, and at 4,000 a second that is 160 MB a second. A 10-gigabit link carries 1.25 GB a second, so this decides nothing.

Four Calls and One Entity

A search is a GET on the nearby path with a latitude, a longitude, a radius and a cursor, a token that marks how far the last page got. It returns up to 20 places, nearest first, and the next cursor. A GET on a place's ID returns that place. A POST adds a listing, and a PUT edits one, each as its owner.

The four calls
GET  /places/nearby?lat=52.37&lng=4.89&radius=500&cursor=c8
     200 {"places": [{"id": "p-81", "name": "...", "distance_m": 140}], "cursor": "c9"}

GET  /places/p-81
     200 {"id": "p-81", "name": "...", "lat": 52.371, "lng": 4.892, "hours": "..."}

POST /places        {"name": "...", "lat": 52.371, "lng": 4.892, "category": "...", "hours": "..."}
PUT  /places/p-81   {"hours": "..."}

The one entity is the place: its ID, owner, name, latitude, longitude, category, hours, and when it was updated. Only the ID and the two coordinates are searched, which is where the 24 bytes came from.

Interview Pitfalls
  • Drawing shards because 80 million sounds large, when 160 GB fits on one machine. The interviewer asks what forced them, and no number did.
  • Accepting "within a radius" without asking how many results. A radius with no limit makes downtown look impossible for the wrong reason.
  • Sizing the writes at length when there are about 2 a second. That time comes out of the read path, which is the whole problem.
What Interviewers Listen For
  • Whether the points move, asked early and with the reason it matters, which shows you know what the easy version depends on.
  • The searchable part worked out as a couple of gigabytes, unprompted, which shows you size what a search needs and not only what is stored.
  • Storage said out loud to force no split, before the word partition comes up.

Knowledge Check

The interviewer allows you one question before the board. Which answer moves this design the most?

  • Which categories of place the listings need to carry
  • How long an owner's old edits have to be retained
  • Whether the places ever move once they are listed
  • Which map provider the client draws the results on

Eighty million places at 2 KB is 160 GB. What does that number decide?

  • The places table needs shards by region from day one
  • Only a search engine cluster can hold a catalogue this large
  • The records must leave disk and be held in memory alone
  • Storage forces no split, since each copy fits on one machine

Searches outnumber edits 1,000 to 1. What does that ratio allow?

  • Building the index away from the writes, on its own schedule
  • Doing without a primary, since so few writes will need one
  • Accepting edits on any read copy, since they will rarely clash
  • Leaving open how stale a search result is allowed to be

One of these can go out of scope in a sentence, at no cost to the design. Which one?

  • Keeping an owner's edit once it has been saved
  • Saying what the app does when the radius is empty
  • Checking that an owner really owns the place
  • Returning the nearest results ahead of the others

You got correct