KatInterview

Boolean Keyword Search Engine for a High-Volume Short-Post Feed

System DesignModel answer
MediumModel answerSystem Designinverted-indexsearchsharding

Last updated

Design a sharded, near-real-time inverted index that answers AND/OR/NOT keyword queries over a stream of 10K new short posts per second within 200 ms.

Build a Boolean Keyword Search Backend for Short Posts

A social product lets people publish short text updates. You are asked to design the backend that lets users find those updates with boolean keyword expressions such as (coffee OR tea) AND NOT cold.

Scale and goals

  • About 10,000 new posts per second arrive continuously.
  • Search requests must finish in under 200 ms.
  • Queries support AND, OR and NOT.
  • New posts should become searchable quickly, and removed posts should disappear quickly (soft deletion). A lag of a few seconds is acceptable (eventual consistency).
  • The service must stay highly available and evaluate queries efficiently.

What to cover in your answer

  1. Index layout: What does the inverted index look like? How are posting lists stored and fetched for a term?
  2. Query evaluation: How is a nested boolean expression executed? How do you intersect and merge lists cheaply?
  3. Partitioning: How would you shard the index? Compare splitting by document with splitting by term.
  4. Freshness and correctness: How does asynchronous indexing work, and how do you make update processing idempotent (duplicates, retries, reordering)?
  5. Failure handling: What happens when a node dies, and how do you avoid losing search coverage?

State your assumptions (retention window, query rate, ranking) and size the system with rough numbers.

Hints

Hint 1

Start with back-of-envelope math: posts per day, tokens per post, postings written per second, and compressed index size for your retention window. This tells you whether the index fits in memory.

Hint 2

If document IDs are assigned in arrival order within a shard, posting lists are append-only and sorted, and newest-first ordering comes for free. Think about how skip pointers and an 'advance to at least X' iterator make AND cheap.

Hint 3

For idempotency, attach a version to every event and keep a post-to-document map. Compare incoming versions with the stored one so replays and out-of-order events become harmless, and let a durable log (with offsets stored alongside snapshots) drive recovery.