How Elasticsearch Makes Search Fast (for Your System Design Interview)
Searching billions of documents in milliseconds seems impossible until you understand the mechanics behind Elasticsearch. Three techniques make it work:
- Inverted Index
- Elasticsearch stores data in a way that maps each unique token directly to the documents containing it.
- This eliminates full scans and turns queries into direct lookups.
- It's the foundation of scalable full-text search.
- Doc Values
- After matching documents, Elasticsearch still needs to sort or aggregate efficiently.
- Doc values store field data (like price) in a compact columnar format.
- This allows Elasticsearch to read only the needed values, not entire documents, which speeds up sorting and aggregations.
- Immutable Lucene Segments
- Elasticsearch writes to immutable segment files rather than updating in place.
- Updates create a new document and mark the old one as deleted; deletes are soft until merges.
- Because segments are read-only, searches avoid locking and use memory-mapped I/O for fast, predictable reads.
- Background merges compact segments and clean up deleted docs, which keeps search fast but introduces write amplification.
Elasticsearch is excellent for search-heavy workloads, but frequent updates or high write throughput can degrade performance because of segment merges and soft deletes.