LESSON 05 · Collection pipeline

Build from completed searches

Aggregate events away from serving and publish a versioned read model.

SYSTEM VIEW

The suggestion path

Diagram view
e‑4127 counted—
Prefixes—
Serving—

Follow one completed search for “camera” from the Collector into tonight’s snapshot, and watch the Query API switch to it.

  1. Someone submits a search for “camera”. The Collector records the permitted, normalized term, the time and coarse context as one event, e‑4127, and appends it to an append-only Event stream such as Kafka, away from the serving path.

    Event
    e‑4127
    Holds
    term, time, context

    What changed

    • Event e‑4127 for “camera”
    • Appended to the Event stream
  2. The Aggregator counts searches by term and window, so “camera” gets +1. Then the stream retries and delivers e‑4127 again. Its event ID is an idempotency key: the Aggregator has seen it and skips the repeat. A durable checkpoint would also prevent the double count.

    Delivered
    2
    Counted
    1
    Key
    event ID

    What changed

    • Aggregator and Term counts
    • “camera” +1
    • Repeat of e‑4127 skipped
  3. Tonight the Index builder, a batch job often run in Spark, builds ranked prefix lists from the Term counts. It writes them to the Snapshot store as an immutable snapshot, v812, with 20.2 million prefixes in this example. The Query API still serves v811.

    Snapshot
    v812, immutable
    Serving
    still v811

    What changed

    • Index builder, Snapshot store, Query API
    • v812 written, immutable
    • Query API still serving v811
  4. Before release, the job compares v812 with v811 and fails itself if the prefix count fell more than 5% or a known popular prefix came back empty. In this example the count rose 1%, from 20.0 to 20.2 million, and “ca” still has its list, so v812 passes.

    Fail if
    prefixes fall > 5%
    Or if
    a popular prefix is empty
    v812
    +1% vs v811

    What changed

    • Prefixes 20.0M → 20.2M, +1%
    • “ca” still has a list
    • v812 passes
  5. The Query API’s serving nodes load v812, pass a health check, and only then switch, so every read sees one consistent version. v811 stays in the Snapshot store: rolling back means moving the serving pointer back to it.

    Serving
    v812
    Rollback
    v811, kept

    What changed

    • v812 loaded, health check passed
    • Serving v811 → v812
    • v811 kept for rollback
  6. If “camera” surges today, the Term counts climb, but the Query API keeps serving v812 until the next build. The build cadence sets freshness: daily suits a stable catalog, and a streaming delta merged at read time is fresher but adds a second code path.

    Serving
    v812 until the next build
    Delta
    fresher, a second code path

    What changed

    • “camera” surging in today’s counts
    • Serving stays on v812
  7. Updating only the “camera” leaf would leave its ancestors’ top-K lists stale. The next build rebuilds all six lists the surge can change, “c”, “ca”, “cam”, “came”, “camer” and “camera”, together in one new snapshot. That is what the snapshot buys.

    Lists to rebuild
    6, “c” to “camera”
    Rebuilt
    together, in one snapshot

    What changed

    • Next build: 6 lists, “c” to “camera”
    • No ancestor list left stale

Space plays · ← → step · Home resets

THE IDEA

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.

DECISION POINT

A newly popular query is in today’s events but suggestions use yesterday’s snapshot. Is serving broken?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

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.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

After a broker failover, counts for all terms jump 3% in one hour, though search traffic was flat. What is the likely gap?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.