Scrivio Interview library
ScenarioAdvanced7 min read · updated 13 November 2026

A places search rebuilds its spatial index every night from the main database. With 200 million places the rebuild now takes 9 hours and sometimes is not finished by morning, and business owners complain that new places and changed opening hours take a day to appear.

How do you move the places index from nightly rebuilds to near-real-time updates, and how do you shard and roll out the new index safely?

We stop rebuilding the whole index and instead stream every change from the places database, through change data capture or an outbox, into a log such as Kafka, and apply each change to the index as an idempotent upsert or delete within seconds. We shard the index by region, for example by coarse S2 cell, keep fast-changing fields like opening hours in a small separate store joined at ranking time, rebuild a shard when needed from a snapshot plus the log and switch an alias atomically, and prove the new path with shadow comparisons and a monitored freshness lag before cutting over.

Why this question is asked

A nightly rebuild is the natural first design, and it works until the data outgrows the night. The cost of a full rebuild grows with the total number of places, while the work that actually matters grows with the number of changes. Here 200 million places are rebuilt to apply perhaps 2 million changes, about 1 percent, and owners wait a day to see their edits. Interviewers use this scenario to see whether we can change a live system's data flow without breaking it. In this answer, we will see why the rebuild stopped scaling, design the streaming update path, shard the index, separate fast-changing fields, rebuild shards without downtime, and roll the whole thing out safely.

1Why the Nightly Rebuild Stopped Scaling

A full rebuild reads every row, recomputes every cell id and ranking feature, and writes a complete new index. Its duration grows with the size of the data, so as the catalogue grew from 50 to 200 million places, the job grew from about 2 hours to 9. When it overruns, searches serve yesterday's index, and a failed run means two days of staleness. Meanwhile the useful work, the 1 percent of places that changed, is a small fraction of what the job does.

So the goal is to make the index's work proportional to changes, not to size, while keeping a way to rebuild from scratch when we change the index format or suspect drift.

2Streaming Changes Into the Index

Change data capture, or CDC, means reading the database's own change log and turning each committed insert, update and delete into an event. With PostgreSQL we can use Debezium, which reads the write-ahead log through logical replication and publishes events to Kafka. If the team prefers not to depend on the log format, the transactional outbox pattern works too: the service writes the change and an outbox row in the same transaction, and a relay publishes the outbox rows. Either way, every committed change reaches a Kafka topic keyed by place id, so all changes to one place stay in order in one partition.

An indexer service consumes the topic and applies each event to the index. Two rules make this safe. Every event carries the row's version, such as an updated_at timestamp or a version counter, and the indexer ignores an event older than what the index already holds, so a replayed or duplicated event cannot undo a newer change. And deletes are explicit events, so a closed place disappears rather than lingering.

Java
void apply(PlaceChanged e) {
IndexDoc current = index.get(e.placeId());
if (current != null && current.version() >= e.version()) return; // old or duplicate: skip
if (e.deleted()) index.delete(e.placeId(), e.version()); // keeps a versioned tombstone
else index.upsert(toDoc(e)); // recomputes cell id, features
}
// event v42 applied, then v41 replayed -> skipped, index stays at v42
Step throughFrom an owner's edit to a searchable place
Owner APIPostgreSQLDebeziumKafkaIndexer
UPDATE places SET hours = ... (commit)
change in write-ahead log
PlaceChanged(id, version 42), key = place id
consume in partition order
version check, upsert into region shard
commit offset

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

Each step is retry-safe because the indexer skips events it has already applied.

Notice that the indexer commits its Kafka offset only after the index write succeeds. If it crashes in between, it rereads the event, and the version check turns the repeat into a no-op. In normal operation a change becomes searchable within a few seconds.

3Sharding the Index by Region

A single index copy of 200 million places is large to rebuild and to load. We shard it by region, for example by S2 cell at level 4 or 5, which gives cells a few hundred kilometres across, and then merge small regions and split very busy ones so shards are similar in size. A nearby search with a 2 km radius almost always falls inside one shard, and near a shard border it touches two or three. The router computes the covering cells and sends the query only to the shards that own them.

Region shards also contain failure and work. If the Tokyo shard's data looks wrong, we rebuild only Tokyo, and an overnight batch of imported places in Brazil does not touch Europe. Each shard is replicated for read throughput as before.

4Keeping Fast-Changing Fields Out of the Spatial Index

Not all fields change at the same rate. A place's location and category almost never change, but opening hours, holiday closures, temporary closures and wait times change often, and some change every few minutes. Writing those into the spatial index means constant updates and segment churn in engines such as OpenSearch, for no spatial reason.

So we keep them in a small, fast key-value store, for example Redis or a replicated in-memory table, keyed by place id. The spatial index returns a few hundred candidates, and the ranker fetches their fast fields in one batched lookup, then filters by open now and scores. That lookup costs about a millisecond for 300 ids and keeps the large index quiet.

5Rebuilding a Shard Without Downtime

We still need full rebuilds sometimes: a new index format, a new ranking feature, or repair after a bug. The trick is to build alongside the live version and switch only when the new one has caught up. We record a log position, take a consistent snapshot of the region's rows as of that position, build a new shard version from the snapshot, then replay the Kafka changes from the recorded position until the new version is current. Then we point an alias, the name the router uses, at the new version in one atomic step, and keep the old version for a while so we can switch back.

Step throughZero-downtime rebuild of one region shard
  1. Record the log positionKafka offset or database LSN
  2. Snapshot the region's rowsconsistent as of that position
  3. Build shard version v8alongside live v7
  4. Replay changes from the positionuntil lag is under a few seconds
  5. Shadow results match v7?
    YesSwitch alias places-tokyo to v8NoInvestigate, keep v7 live
  6. Keep v7 for rollbackdelete after a few days

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

The alias switch is the only moment the router changes, and it can be reversed.

Because the replay starts from the snapshot's own position, no change made during the build is missed, and the version check makes any overlap harmless.

6Rolling Out Safely and Measuring Freshness

We run the streaming path in shadow first: it maintains the new index while the nightly index still serves users. A shadow service sends a sample of real queries, say 1 percent, to both and compares the top 20 results. We expect differences, because the new index is fresher, so we look at the overlap rate and inspect the cases that differ most. When the overlap is high and every difference we inspect is explained by freshness, we move traffic region by region, starting with a small one.

Freshness becomes a first-class metric: for each event, the time from database commit to searchable, measured by the indexer and reported as percentiles. A target such as 99 percent of changes searchable within 60 seconds, with an alert when lag grows, replaces the old question of whether the night job finished. We keep a weekly full rebuild of each shard as a consistency check: comparing it with the live shard catches any drift the stream has introduced.

7Follow-up questions

Why not just make the nightly job faster with more machines?

That buys time but not freshness. Parallelising could bring 9 hours down to 2, but owners would still wait up to a day, and the cost keeps growing with the catalogue. Streaming changes fixes both, because the work follows the change rate and updates land in seconds.

What if the indexer falls hours behind, for example after an outage?

Searches keep working on a slightly stale index, and the indexer catches up by consuming the backlog faster than new events arrive, since applying an upsert is cheap. If the backlog is longer than Kafka's retention, we rebuild the affected shards from a snapshot plus the log instead, which is why retention should comfortably exceed the rebuild time.

8Conclusion

In this answer, we moved the places index from nightly rebuilds to a stream of changes. CDC or an outbox publishes every committed change to Kafka keyed by place id, and an idempotent indexer applies versioned upserts and deletes within seconds. The index is sharded by coarse region so queries touch one or few shards, fast-changing fields live in a separate store joined at ranking time, and shards are rebuilt from a snapshot plus the log and switched with an alias. Shadow comparisons, gradual cutover and a freshness lag metric make the rollout safe, and occasional full rebuilds remain only as a check.

What to say in the interviewAbout 80 seconds aloud

Open with

The nightly rebuild does work proportional to 200 million places to apply about 2 million changes, so it will only get worse. I would make the index follow changes instead.

Then, if they want more

Every committed change goes from PostgreSQL to Kafka, either by CDC with Debezium or through an outbox, keyed by place id so each place's changes stay ordered. An indexer applies them as upserts and deletes, carrying a version so duplicates and replays are skipped, and commits the offset after the write. That makes edits searchable in seconds.

I would shard the index by region, using coarse S2 cells, so a search hits one or a few shards and I can rebuild one region alone. Opening hours and temporary closures go into a small fast store like Redis that the ranker joins for the few hundred candidates, so they do not churn the spatial index.

For rebuilds I record a log position, snapshot, build a new version, replay from that position and switch an alias atomically, keeping the old one for rollback. I would roll out in shadow, comparing top results on sampled queries, move region by region, and alert on freshness lag, with a weekly full rebuild as a consistency check.

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 "How do you move the places index from nightly rebuilds to near-real-time updates, and...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/e0cd3a694f/