✏️ Explanatory Question

Walk through the request flow for a search query across millions of documents.

👁 6 Views
📘 Detailed Answer
🟢 Easy
No previous question
No next question
💡

Answer with Explanation

Read path:

  1. Client sends a query; the gateway authenticates and rate limits.
  2. Cache check - popular queries are heavily repeated, so a cache on the normalised query string often serves 30-50% of traffic outright.
  3. Query understanding - tokenise, lowercase, stem, remove stop words, apply spell correction and synonyms.
  4. Scatter phase - the query is broadcast to every shard in parallel, because the inverted index is partitioned by document. Each shard returns its own top N.
  5. Gather phase - the coordinator merges the per-shard results and re-ranks globally to produce the final top N.
  6. Hydrate - fetch titles, snippets and images for just those N results from the document store.
  7. Return, and log the query for ranking feedback.

The key insight to voice: this is a scatter-gather fan-out, so it suffers tail latency amplification - the query is only as fast as the slowest shard. Mitigate with hedged requests to replica shards and a timeout that returns partial results rather than waiting.

Write path is separate and asynchronous: a document update publishes an event, an indexing worker updates the inverted index, and the new document becomes searchable seconds later. Near-real-time indexing is the norm, and stating that search is eventually consistent is exactly the right call.

No previous question
No next question