19. Search and Inverted Indexes
1
The picture to keep in your head2
19.1 Why doesn't `LIKE '%word%'` scale?3
19.2 How do you tokenise and normalise text?4
19.3 What is an inverted index?5
19.4 How does ranking work (TF-IDF, BM25 in brief)?6
19.5 How do prefix search and tries work?7
19.6 How do you keep the search index in sync with the database?8
19.7 Where does Elasticsearch fit in the big picture?9
19.8 How do you shard a search index?10
Memory card and where the analogy breaks11
Practice: five levels12
Interview questions13
Solutions14
Sources15
Code lab19. Search and Inverted Indexes
19.6 How do you keep the search index in sync with the database?
3 min read+10 XP
In the library: New books arrive, old ones are withdrawn, a title gets corrected. If nobody updates the cards, the cabinet sends people to books that aren't there. The shelves are the truth; the cards are a copy that must follow them.
In the system: The database is the source of truth; the search index is a derived copy, like a cache (Chapter 12). Ways to keep it updated:
Figure: send committed changes through a log, with versions, instead of writing to both places from the app.
- Dual write (avoid): the app writes to the database, then to the search index. If the second write fails, or two updates arrive out of order, the two drift apart silently.
- Change data capture (CDC): a tool reads the database's own change log (for example, Debezium reading the replication log) and streams every committed change to an indexer.
- Outbox pattern: the app writes the change and an "outbox" event in the same database transaction (Chapter 17), and a separate process publishes the events (Chapters 22 and 23).
- Periodic batch reindex: rebuild from scratch nightly; simple, but results are hours stale. Often used as well, to repair drift.
Details that matter:
- Out-of-order events: carry a version (or the update time) with each change, and ignore anything older than what's already indexed. In the chapter's code, a late event with version 2 arriving after version 3 is skipped, so an old title doesn't come back.
- Deletes must flow through too, or deleted books stay searchable.
- Search is eventually consistent. Elasticsearch makes new documents searchable after a refresh, about once a second by default, so a just-added book may take a moment to appear. Design the UI for that (show the new item from the database right after the user creates it).
Memory code: "The shelves are the truth; the cards follow the change log."