How it works
After a completed search, a collector records a permitted normalized term, time, and coarse context. An append-only stream such as Kafka lets consumers aggregate counts by term and window. Because events may be retried, an idempotency key (a unique event ID that makes a repeat harmless) or a durable checkpoint prevents double counting. Sampling can reduce cost but must be consistent, and it distorts rare terms: if you keep 1 in 10 events, three sampled searches could represent anything from a handful to dozens.
Workers, often batch jobs in a tool like Spark, build ranked prefix lists from aggregates, validate them, and write an immutable snapshot. As an example sequence, tonight’s job writes v812, then fails itself if the prefix count fell more than 5% from v811 or a known popular prefix has become empty. Serving nodes load v812, pass a health check, and switch. They retain v811 for rollback.
The build cadence follows freshness needs: a stable catalog may refresh daily, and nothing faster is worth paying for. Trending terms may need a small streaming delta merged with the baseline at read time, which adds a second code path and its own failure modes. Updating only one trie leaf leaves its ancestor top-K lists stale: if “camera” surges, the lists for “c,” “ca,” “cam,” “came,” “camer,” and “camera” may all need changes. Rebuilding those lists together is what the snapshot buys.
A newly popular query is in today’s events but suggestions use yesterday’s snapshot. Is serving broken?
Not necessarily; it follows the freshness contract. If same-hour trends matter, shorten builds or add a bounded streaming delta and measure lag.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Collection, aggregation, building, and serving have different latency and failure needs.
- Versioned snapshots support consistent rollout; a delta buys freshness at added complexity.