KatInterview

Sharded Full-Text Search for Multi-Tenant Tables

System DesignModel answer
MediumModel answerSystem Designsearchshardingcdc

Last updated

Design a near-real-time, sharded full-text search layer over large structured tables with sub-200ms reads and petabyte scale.

Context

You are designing the search subsystem for a product where customers keep structured records in tables (think spreadsheet-database hybrids). Users expect to type a keyword and instantly see matching rows from their own tables, while rows are being edited, imported, and deleted constantly.

What to design

A search service that indexes row content from a primary transactional database and answers keyword queries scoped to a table, a base (group of tables), or an entire workspace.

Functional requirements

  • Keyword search across multiple text columns of a table, plus simple equality filters on columns.
  • Ranked, paginated results with highlighted snippets.
  • Edits become searchable in near real time (on the order of a second or two, p99).
  • Search scope is resolved server-side from the caller's permissions.

Non-functional requirements

  • p99 query latency under 200 ms for the common case.
  • Scale to roughly 1 PB of indexed content.
  • High availability: survive loss of a node and of an availability zone.
  • Handle heavy skew — most tables are tiny, a few are hundreds of GB.

Questions your design must answer

  1. How do row mutations travel from the primary database into the index, and how do you avoid lost or duplicated updates?
  2. How do you partition the index across machines given extreme table-size skew?
  3. Describe the full request path from client to aggregated results.
  4. How do you trade visibility latency against indexing cost, and how would you give an editing user read-your-writes?

Scenarios to reason about

  • A user edits one cell in a table with 1 million rows — when does it show up in search?
  • One table holds 500 GB of data — how is it spread and searched efficiently?

Hints

Hint 1

Separate correctness (the primary DB is the source of truth, feed the index via change data capture) from freshness (how often segments are made visible) — conflating them is the classic mistake.

Hint 2

Document-based sharding keeps each row in exactly one shard; term-based sharding forces every update and query to touch many shards and creates hot posting lists.

Hint 3

Most tables are small: route them together by base so a query hits a single shard. Give only oversized tables their own hash-partitioned shard set to avoid blanket scatter-gather.