Lesson 4 · Minutes 30-45

Proximity Service: Deep Dives and Curveballs

Problem

By minute thirty you have used three ideas without defending them against anything: geohash cells, a tree, and a file that changes once an hour. Minutes thirty to forty-five make you defend them. You compare the ways to cut the map, set the cap, and pick up the freshness you parked in First Design. Then the curveballs change the problem, and the first one removes the assumption you checked in your first minute.

Three Ways to Cut the Map

A fixed-length geohash is a column in any store with a sorted index, and a write is one row. It cannot adapt, and one character more or less is about 32 times the area, so any single length is badly wrong somewhere.

A quadtree is a tree of squares, where a square splits in four once it holds too many places. It follows density closely. Here it would live in memory, because a tree is not a value you can put in a plain column. Some databases do offer one as an index type, as PostgreSQL's SP-GiST does, and then you are tied to that engine.

Hierarchical cells, with S2 and H3 as the known examples, keep cells nearer equal in area from the equator to the poles. That pays off for covering shapes such as a delivery zone, and this product draws none.

Your choice here is geohash cells adapted in an in-memory tree, because the index fits in memory. If no process may hold a tree, it becomes a fixed geohash with a length chosen at query time. If the product draws polygons, it becomes hierarchical cells. Name the pick and what would move it: "I'd adapt geohash cells in a tree, because 2 GB fits in memory. If it had to stay in a store I'd use a fixed geohash, and if the product drew shapes I'd use hierarchical cells."

A scheme by requirement
The index stays in a store→A fixed-length geohash column, length chosen at query time
It fits in memory and density varies→Geohash cells in a tree, or a quadtree
The product draws shapes→Hierarchical cells
Geohash vs Quadtree

A geohash is a value you can store in a column and range over in any sorted index. Use it when the index lives in a store, or when cell sizes can be chosen at query time.

A quadtree is a structure that splits where the places are. Use it when the index fits in memory and density varies by orders of magnitude.

Choosing the Cap

A cap of 200 reads at most 1,800 places in a block of nine leaves, a mall's overfull leaf aside. A cap of 50 reads at most 450, but a split is a whole character, 32 children and not four. Downtown's leaf of about 93 places becomes 32 leaves of about 3, each 38 by 19 metres, so a block covers far less ground and a search may walk more rings to reach its 20. If every leaf of about 100 split, 800,000 would become about 26 million, about 2.6 GB at 100 bytes each. That still fits, so memory does not choose. The cap trades splits at rebuild against places checked per search, and even 1,800 at 100 nanoseconds is under 200 microseconds. So 200 stands until filtering shows up as the thing a search server spends its time on.

When a block is not enough, the next ring is 16 cells and the one after it 24, eight more at each step. That is cheap in memory, and the agreed 2 km bounds how far it goes. Past that, "nothing near" is the honest answer, and Scope already agreed it.

Keeping the Index Fresh

You held this question back in First Design. Scaling It settled the swap, so the builder's rhythm is the choice left: hourly, because an hour is the agreement. An edit made just after a build waits for the next one, so the cycle has to run a little under the hour to leave room for the build and the load.

A file every ten minutes is six times the builds for a promise nobody asked for. The trigger for going further is a tighter promise, and the third curveball below is exactly that.

The owner's own page reads the primary, so they see their edit at once even while search lags, the read-your-writes routing of Chapter 2's Read Replicas and Lag. That covers their page and not the search results, where they can still find the old hours for up to an hour.

Curveballs

"The points now move: friends and drivers, reporting every few seconds." Say first that the ratio just flipped and the rebuild is gone. Assume 2 million drivers every 4 seconds. That is 500,000 writes a second against today's 2, and at half of a cache node's 100,000 it is about 10 nodes, so the rate forces the split that the bytes never did. Keep the latest position in memory with a short TTL, a time limit that removes an entry, so a driver who stops reporting drops out alone.

Most of those writes do not change the cell. A vehicle at 10 metres a second crosses a 610-metre cell about every 60 seconds, so one report in 15 changes its cell, about 33,000 cell updates a second. Friends differ: a few hundred names are their own keys, so read those positions directly, with no index.

"Add 'open now' and a minimum rating." Put them in the index entry, where they filter before any record is fetched. A bit per hour of the week is 21 bytes, plus a byte of rating. That is another 2 GB for 80 million, and it still fits. The ring rule reads further as matches thin out. The time zone belongs to the place, not the searcher, so it goes in the entry too. Past a few filters or free text, a search engine with geo support replaces the tree, at the cost of a system to run.

"An owner's edit must show within a minute." Split the edit by what it touches. New hours change the record: write it, and remove the cached copy once the replicas have the edit, or a miss refills the old hours and they stay until the next file. It shows within seconds. A moved or new place changes the index: every server applies it from the change log on top of the last file, and the next rebuild absorbs it. Each file names the log position it was built from, so a restarted server loads the file and reads on from there, as long as the log keeps more than one cycle. What it costs: every server becomes a log consumer.

"We're launching worldwide, tomorrow." The 80 million were always worldwide, so the index does not change. It is 2 GB, so it goes to every region, with no partitioning by country and no border to cross. US east to Southeast Asia is about 225 milliseconds, more than twice the 100 Scope gave the server, and spent before the server has started, which is why search is served nearby.

Edits stay in one home region, where a far owner's save pays the round trip, and at 2 a second nobody minds. If that region goes, every other region keeps searching on its file and cache, and only edits wait. A record cache in each region fills from a replica there, before the region takes traffic, or it meets its whole share of 80,000 reads a second as misses. At 160 GB over a 10-gigabit link, that takes about two minutes. The builder's removals go to every region's cache along with the file, or that region shows old hours after its search has moved on.

How Each Level Answers

A mid-level candidate sees why two ranges do not make one box when pushed, reaches a geohash column, and reads the neighbours once the edge is raised. They put reads on replicas and a cache, and they get to density when the interviewer describes downtown.

A senior candidate asks whether the points move in the first minutes and says that storage forces no split. They raise density and the edge before either is named, give both sides a number, and move the index into memory because 2 GB allows it. They choose a scheme with its condition, and they state the stop rule.

A staff candidate starts from what a search has to return, and agrees the radius and the empty result as product decisions. They plan the index as a file with a last good copy, and the second region as no more than a copy. They ask whether the product will ever draw shapes or follow moving points, and they price that change before anyone asks.

Interview Pitfalls
  • Naming geohash, quadtree and S2 with no word on which you would build here. The interviewer hears a list where they wanted a choice.
  • Keeping the hourly rebuild when the points start moving, when a file an hour old says nothing about a driver.
  • Answering the worldwide launch with partitions by country, when the index fits in a file that every region can hold.
What Interviewers Listen For
  • A scheme picked, together with what would change the pick.
  • The inverted ratio named before anything is designed for moving points.
  • Each curveball met with the smallest change to the board, which shows you know what the design already gives you.

Knowledge Check

The interviewer says the points now move, reporting every few seconds. What is your first move?

  • Run the builder every minute, so that positions stay close
  • Say the ratio has flipped and the rebuild no longer applies
  • Partition the index by region, so that each rebuild is smaller
  • Put a queue before the primary, so it can absorb the reports

Where do "open now" and a minimum rating go, and what happens to a search?

  • In the record cache, and a search checks each one after fetching 20
  • In a search engine at once, because any filter needs a cluster
  • In the index entry, and the ring rule reads further as matches thin
  • In the client, which hides closed places from each page it gets

An owner's edit must now show within a minute. What does that change, and what does it leave alone?

  • One file a minute, and owners' saves now wait for each to publish
  • Searches go back to the primary, and the file stays for failover
  • Each edit rewrites the file, and every server fetches it again
  • Servers read the log for moved places; new hours just invalidate

"We're launching worldwide, tomorrow." What is the smallest change that keeps a search fast for a far user?

  • Copy the file to each region, with a record cache filled there
  • Partition the index by country, with a search routed to each
  • Keep one region, and put a CDN in front of the search results
  • Take edits in each region, and merge them into the primary hourly

You got correct