Scrivio Interview library
DesignFoundations7 min read · updated 13 November 2026

A nearby places search finds restaurants, shops and other points of interest around a location, such as cafes within 2 km, ranked by distance, rating and relevance. Place data changes slowly while searches arrive constantly from mobile apps.

Design the API, data model and spatial index of a nearby places search for 200 million places and 50,000 searches per second.

A nearby places search exposes a read API such as GET /places/search with a location, radius, category and limit, keeps places in a database of record, and copies them into a read-optimised index where each place carries a spatial cell id. A search covers the circle with a few cells, filters their few hundred candidates by exact distance and attributes, ranks them by distance, rating, popularity and text match, and returns the top 20. Because the data changes slowly and searches never stop, we scale by replicating the index widely and caching popular cells.

Why this question is asked

Almost every consumer app has a search like this: food delivery, maps, ride hailing, travel. Interviewers like it because it hides one real problem: a normal index sorts by one value, but a location is two, and we must find everything inside a circle quickly. It also tests whether we notice the load: thousands of reads for every write. In this answer, we will set the requirements and rough numbers, design the API and the data model, build the spatial index step by step, see how ranking works on the candidates, and then look at how the design scales and fails.

1Requirements and Rough Numbers

A user sends a location, a radius, optionally a category, a text query and filters such as open now, and gets up to 20 places ranked by distance and quality. Owners edit places through a separate path.

The numbers decide the design. 200 million places at about 1 KB each is roughly 200 GB of records, easy for a sharded relational database. The search index needs only an id, coordinates, a cell id, category, rating, popularity and a few flags, about 100 to 200 bytes per place, so 20 to 40 GB. That fits in memory, which matters, because 50,000 searches per second at about 100 ms at the 99th percentile rules out disk reads per candidate.

Writes are tiny: even if 1 percent of places change daily, that is about 25 updates per second. The system is read-heavy by a factor of a thousand, and the decisions below follow from that.

2The API

We separate the read API that apps call constantly from the write API that owners use. The search endpoint takes the location, the radius in metres, the filters and a limit, and returns ranked places with the computed distance so the app does not have to calculate it. A details endpoint returns the full record, and writes go through an owner-facing API that validates and stores changes in the database of record.

Java
GET /places/search?lat=52.5200&lng=13.4050&radius=2000&category=cafe&open_now=true&q=espresso&limit=20
200 OK
{
"results": [
{ "id": "p_81723", "name": "Kaffee Mitte", "distance_m": 140, "rating": 4.6, "open_now": true },
{ "id": "p_10422", "name": "Bohne", "distance_m": 610, "rating": 4.8, "open_now": true }
],
"next_cursor": "eyJvZmZzZXQiOjIwfQ"
}
GET /places/p_81723 -- full details: hours, photos, address
POST /owner/places -- create a place (owner API, authenticated)
PATCH /owner/places/p_81723 -- change hours or status

The search response carries only what a result row needs, which keeps the hot response small.

3The Data Model: a Record and a Search Copy

The places table in PostgreSQL is the record: it holds every field, enforces constraints, and is where owner edits land. We shard it by place id because the write and detail paths always know the id. The search index is a separate, derived copy shaped for one job: answering spatial queries fast. Keeping them apart means a heavy search load can never slow down an owner saving their hours, and we can rebuild the index from the record whenever we change its layout.

SQL
CREATE TABLE places (
id BIGINT PRIMARY KEY,
name TEXT NOT NULL,
category TEXT NOT NULL,
lat DOUBLE PRECISION NOT NULL,
lng DOUBLE PRECISION NOT NULL,
cell_id TEXT NOT NULL, -- for example geohash 'u33db' (precision 5, about 4.9 km)
rating REAL,
hours JSONB,
status TEXT NOT NULL, -- open, temporarily_closed, closed
updated_at TIMESTAMPTZ NOT NULL
);
-- Search index document, one per place, kept in memory and grouped by cell:
-- { id, lat, lng, cell_id, category, rating, popularity, open_flags, name_tokens }

The cell_id, computed when a place is saved, turns a two-dimensional location into a key that sorted structures understand.

4Building the Spatial Index

A spatial cell is a named tile of the map. With geohash, the world is cut into a grid, and each tile gets a short string; longer strings name smaller tiles, and tiles that share a prefix are near each other. A precision-5 geohash covers about 4.9 by 4.9 km and a precision-6 one about 1.2 by 0.6 km. Other schemes, such as quadtree nodes or Google's S2 and Uber's H3 cells, follow the same idea with different shapes, and the choice between them is its own topic.

The index is then a map from cell id to the list of places in that cell. A search for cafes within 2 km of a point does four things. First it computes the cells that cover the circle: the cell containing the point plus its neighbours, because the circle can cross a cell edge. Then it reads the candidates in those cells, filtered cheaply by category inside each list. Then it computes the exact great-circle distance for each candidate and drops those beyond 2,000 m, because a cell is a square and the corners stick out of the circle. Finally it applies attribute filters such as open now.

Step throughOne search through the spatial index
  1. Compute covering cellsthe cell of (lat, lng) plus the 8 neighbours
  2. Read candidate listsonly places with category = cafe
  3. Exact distance checkdrop anything beyond 2,000 m
  4. Attribute filtersopen now, not closed
  5. Enough candidates?
    YesRank the few hundred leftNoWiden the radius or use larger cells
  6. Return top 20with distance in metres

6 steps in order. Play them, or step through them one at a time.

Cells turn a two-dimensional search into a few key lookups; the exact check fixes the cells' rough edges.

So the index never scans 200 million places. A city cell might hold a few hundred cafes, and checking a few hundred distances takes microseconds.

5Ranking the Candidates

Distance alone gives poor results: the nearest cafe might have a 2.1 rating. So after filtering we score each of the few hundred candidates with a formula such as score = 0.4 x closeness + 0.3 x rating + 0.2 x popularity + 0.1 x text match, where each part is normalised to the range 0 to 1 and closeness falls off as the distance grows. The weights are tuned with experiments, and a learned model can replace the formula later.

The reason ranking is affordable is the candidate set. Scoring 300 places is cheap; scoring 30,000 is not. In dense areas we cap each cell's list by popularity before scoring.

6Scaling the Reads

With 20 to 40 GB of index and 50,000 searches per second, we keep a full copy of the index in memory on each search node and run many identical nodes behind a load balancer, in every region we serve. If one node handles about 3,000 searches per second, we need around 17 nodes for the load and run 25 or more for headroom and failures.

Caching adds another layer. Searches cluster around the same places, such as city centres, stations and airports, so we cache results for popular rounded locations and queries in Redis for 30 to 60 seconds, and cache the candidate lists of hot cells in each node. Rounding the coordinates to about 100 m before using them as a cache key raises the hit rate a lot.

Step throughRead path for a search
  1. Mobile appsends lat, lng, radius, filters
  2. API gatewayauthentication, rate limits, rounding for cache keys
  3. Result cache (Redis)popular rounded queries, 30 to 60 s
  4. Search nodesfull in-memory index copy each, many replicas per region
  5. Change feedstreams place updates into every index copy
  6. PostgreSQLplaces record, sharded by place id

6 layers, from the top down. Step through them one at a time.

Reads never touch the database of record; writes reach the index through the change feed.

7Failure Handling and Trade-offs

If a search node dies, the load balancer stops sending it traffic and the others absorb the load, which is why we keep spare capacity. If the change feed lags, searches still work and only freshness suffers, so we monitor the lag in seconds. If the database of record is down, only owner edits fail.

The main trade-off is freshness against simplicity: a copy seconds behind the record is fine for places, which lets us replicate freely. We also pick the cell precision so a typical search reads 9 cells and a few hundred candidates.

8Follow-up questions

Why not just use PostGIS for the searches?

PostGIS with a GiST index answers radius queries well at small and medium scale. At 50,000 searches per second we would need many replicas of the full 200 GB database. A dedicated in-memory index holds only the 20 to 40 GB search needs, scales by adding cheap copies, and keeps search load off the record.

What happens in the countryside, where a 2 km circle has no cafes?

The first pass returns too few candidates, so the service widens the search, for example to 5 km and then 20 km, or switches to coarser cells, until it has enough results or reaches a maximum. The response tells the app the radius that was actually used so it can say that results are farther away.

9Conclusion

In this answer, we saw that a nearby places search is read-heavy, with places in PostgreSQL as the record and an in-memory copy keyed by spatial cells. A search covers the circle with a few cells, checks exact distance and filters on a few hundred candidates, and ranks them by distance, rating, popularity and text match. Because the data changes slowly, we scale by replicating the index widely and caching hot queries.

What to say in the interviewAbout 80 seconds aloud

Open with

200 million places and 50,000 searches a second against a few dozen writes, so this is a read problem. The API is GET places search with lat, lng, radius, category, filters and a limit, returning ranked results with distance, plus GET place by id for details and a separate owner API for writes.

Then, if they want more

The record lives in PostgreSQL, sharded by place id, and each place gets a spatial cell id, for example a geohash. From the record I build a slim search index, about 100 to 200 bytes per place, so 20 to 40 GB, and keep a full copy in memory on each search node.

A search computes the cells covering the circle, including neighbours for the edge cases, reads the candidates, does an exact distance check and the filters, and ranks a few hundred candidates by a weighted score of distance, rating, popularity and text match, returning the top 20. If there are too few results I widen the radius.

I scale with index replicas per region, a short Redis cache for popular rounded queries, and a change feed that keeps the index seconds behind the record.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design the API, data model and spatial index of a nearby places search for 200 million...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/6318e0ebe2/