Design a Distributed Search Engine (Elasticsearch)
hard 75 minFind the best ten of ten billion documents while people wait: shards, replicas, scoring, immutable segments, and a rebuild plan.
Video lesson · Chapter 1 of 5
A newly listed pair of headphones cannot be found yet
Theory
Read or listen first. About 1 min.
What it is
A search engine answers a question such as wireless headphones under 300 with a short list, best first. It does not read every document. It reads an inverted index, which lists the documents that hold each word.
It is distributed when the index is cut into shards on many machines. One machine, the coordinator, asks the shards and joins their answers.
Why it exists
A table scan over 20 TB of text takes far too long. An ordinary database index helps with exact values, but not with words inside text or with ranking.
This problem tests an unusual price: each search may touch a hundred machines. More shards hold more data, yet they make every search do more work and wait for the slowest one.
A short history
1970s and 1980s
Stephen Robertson, Karen Spärck Jones and others develop the probabilistic ranking ideas behind BM25.
1999
Doug Cutting writes Lucene, a search library.
8 February 2010
Shay Banon releases Elasticsearch, which puts Lucene indexes behind JSON over HTTP on many machines.
2013
The Tail at Scale, an article by Jeffrey Dean and Luiz André Barroso, explains why a request that fans out meets the slowest machine.
Words to know
- Inverted index
- From each term to the documents that contain it.
- Postings list
- The sorted document numbers of one term, often with counts and positions.
- Shard
- One part of an index. It is a complete small index by itself.
- Replica
- Another copy of a shard, for failure and for more reads.
- Segment
- A finished, immutable piece of a shard. Searches read all segments.
- Refresh
- Make recently indexed documents visible to searches.
- Scatter and gather
- Ask many shards at once, then merge their short lists.
- Doc values
- Columns stored by document, for sorting and grouping.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
Understand it
Story
The detective office
What we build
Search for a large marketplace
We build a service like Elasticsearch, used by a marketplace with ten country sites. Assumption: ten billion listings, one billion in each site, and one search stays inside one site. That last fact is precious, because it stops a search from asking every machine.
Eight abilities
Full-text search
Find words in titles and descriptions.
Indexing
Add, change and remove JSON documents.
Filters
Exact values, ranges and distance.
Aggregations
Counts and averages beside the results.
Fuzzy matching
Forgive a small typing mistake.
Near real time
A change is searchable in seconds.
High availability
Replicas answer after a failure.
Scale out
Add machines and move shards.
People meet similar services as Elasticsearch, Apache Solr, Algolia, Typesense, Meilisearch and Amazon OpenSearch Service.
The hard part
Every search pays for the split
Cutting by document makes writes easy: one document goes to one shard. It makes reads wide: any shard may hold the best result. We control that cost with fewer, larger shards, routing by site, caching, and a deadline for slow copies.
The finished system
Where the design ends up
The whole system
Read on
| Place | For |
|---|---|
| Interviews tabs | Scope, sums, API, six versions, deep dives and a mock |
| Visualize | Index, search, page, aggregate, merge, and three failures |
| Full-text search | Tokens, inverted index and BM25 from zero |
| Search system design | The search feature around an engine |
| Sharding, partitioning strategies | Choosing and moving parts |
| Replication, failure detection | Copies and elections |
| LSM tree, SSTable | The same immutable-file idea |
| Change data capture | Feeding the copy |
| Search autocomplete | Suggesting queries, a different problem |
Summary
The whole topic in one minute.
Search is precomputed reading. Analyse text once, store term to documents, and cut the index by document so that each shard answers alone. A coordinator merges short lists and fetches only the winners.
The index is a copy. The listings database owns the truth, a change log feeds the copy, and aliases make a full rebuild safe. Segments never change, so reads need no locks and deletes wait for a merge.
Count shard visits, not only searches. At 10,000 searches a second and 100 shards each, the machines do a million small searches.
- Words point to documents, so a search reads short lists instead of all text.
- Partition by document. Each shard answers alone and the coordinator merges.
- A search on 100 shards waits for the slowest one it decides to wait for.
- Segments never change. Updates write a new version and mark the old one deleted.
- The database owns the truth. The index can always be rebuilt and swapped by alias.
- Two copies in different zones, a log on each copy, and a majority to elect a leader.