KatInterview

Design a Distributed Full-Text Search Engine with Boolean Queries

System DesignModel answer
HardModel answerSystem Designinverted-indexdistributed-systemsquery-parsing

Last updated

Design a scalable search system that indexes short user posts in near real-time and evaluates Boolean queries with stable cursor-based pagination.

Distributed Full-Text Search with Boolean Queries

You are building the search backend for a social platform where users publish short status updates. The search interface lets users express what they are looking for using logical conditions — combining required terms, alternative terms, and excluded terms — with support for grouping via parentheses.

For example, a user might type: (engineering OR product) AND NOT standup

Your job is to design the system end to end.

Functional Requirements

  1. Newly published posts should become searchable within roughly one second of creation.
  2. Queries may contain AND, OR, and NOT operators, arbitrarily nested with parentheses. Standard logical precedence applies.
  3. Results must be paginated. Pagination must be stable: moving from page 1 to page 2 should not skip or repeat posts because new content arrived between the two requests.

Non-Functional Requirements

  1. The system must remain available and horizontally scalable.
  2. Updates to the index should reach strong eventual consistency across replicas.
  3. Write throughput must not degrade search latency.

What Your Design Should Cover

  • Index structure: How do you represent the inverted index and organize it across machines? What are the trade-offs between centralizing the index and partitioning it?
  • Query pipeline: How do you turn a raw query string into an efficient execution plan? What data structure represents it, and what optimizations matter?
  • Ingestion flow: How does a post travel from the moment a user submits it to the moment it appears in search results?
  • Pagination strategy: How do you guarantee that pages stay consistent when the underlying index is continuously growing?

Scale Assumptions

  • Ingestion rate: 50,000 posts per second
  • Query volume: 10,000 searches per second
  • Posts average 30 tokens each
  • Your design should stay performant even when a query includes very common words like "the" or "update"

Hints

Hint 1

Start with the doc ID scheme. If IDs encode creation time and are monotonically increasing, sorting by recency and building a cursor both become trivial — figure out how to exploit that before designing anything else.

Hint 2

Think about what happens when a user queries a term that appears in millions of posts. Scanning the whole posting list is not viable. Consider how early termination during execution, combined with descending-order posting lists, lets you stop after collecting just one page of results.

Hint 3

Offset-based pagination breaks when the index is live. Sketch out what information a cursor would need to carry so that a shard can resume from exactly the right position and never include posts that were written after the first page was served.