AlgoViz

Elasticsearch

medium 34 min

Elasticsearch is a distributed search and analytics engine built on Apache Lucene.

See it run

How a document becomes safe and then searchable, how a search asks every shard, and what a lost node does.

Operations

Write, then refresh

One document goes to a primary and its replica. It is safe before it can be found.

Step 1 of 5

Route

Our systemApp serversCoordinatingroutes and mergesMaster nodecluster stateElastic node 1P0, R2Elastic node 2P1, R0Elastic node 3P2, R1

shard 1

The hash of id p7 modulo 3 is 1. Its primary is on node 2.

Step 1: Route

shard = hash(doc.id) % number_of_shardsprimary(shard).apply(doc)                 # buffer + translogfor r in in_sync_replicas(shard): r.apply(doc)fsync_translogs()answer("201 Created")                     # safeevery("1s", refresh)                      # now searchable
Waits for
All in-sync copies
Watch out for
1 s delay
GET by id
sees it at once

refresh=wait_for makes one request wait for the next refresh. Forcing a refresh on every write makes many tiny segments.

In the real world

The same elasticsearch doing real jobs. Pick one and step through it.

Search for laptop, then choose Dell, without losing the other brands.

Step 1 of 3

Search

DellHPLenovohits shown
no choice503020100

100 laptops are found. The terms aggregation counts the brands.

Step 1: Search

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • A term query on a text field

    major

    The index holds lower case terms, the query is not changed, so nothing is found.

    Wrong
    {"term": {"title": "Active"}}
    Right
    {"match": {"title": "Active"}}{"term": {"status": "Active"}}   # keyword
  • The only copy of the data

    critical

    There are no transactions over documents, and a wrong mapping means building everything again.

    Wrong
    save_order(elasticsearch)
    Right
    save_order(database)   # then the change stream feeds the index
  • Thousands of tiny shards

    major

    Every shard costs memory, and every search visits each one.

    Wrong
    index_per_customer_per_day(shards=5)
    Right
    data_stream(rollover="50gb per primary shard")
  • Deep pages with from and size

    major

    Each shard must send all earlier hits again.

    Wrong
    search(from_=10000, size=20)
    Right
    search(size=20, search_after=last_sort, pit=pit)
  • Refresh after every write

    minor

    It makes many small segments and slows indexing.

    Wrong
    es.index(..., refresh=True)
    Right
    es.index(..., refresh="wait_for")   # only where needed

Interview tips

  • How to explain it

    • Each shard is a small search engine.
    • Writes: primary, replicas, translog, then refresh.
    • Searches: ask all shards, merge.
  • Before production

    • Explicit mappings and aliases.
    • Shards of 10 to 50 GB.
    • Three master-eligible nodes.
    • Snapshots and a tried restore.
  • Reach for it when

    • People search words and expect ranking.
    • Filters need counts.
    • Logs are kept and searched by time.
  • Be careful when

    • Documents change constantly.
    • Readers must see their own write at once.
    • The data is small.