Dynamic inverted indices
Dynamic inverted indices are techniques and data structures to keep an inverted index correct and fast as documents are added, changed or removed, using in-memory buffers, on-disk sorted blocks and merge strategies (including LSM-style merging) plus deletion handling (tombstones) and compaction.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Dynamic inverted indices let a search engine or an IR system keep its index up-to-date while serving queries. Engineers use these techniques when they must accept document inserts, updates or deletes online (for example, new web pages, user edits, email ingestion) while keeping query latency low and controlling disk I/O.
Key ideas
Start with definitions from first principles (plain English):
- Term: a token produced by text preprocessing (see the earlier topic "Text preprocessing: tokenisation, stemming and lemmatisation").
- Document identifier (docID): a unique integer assigned to each document. docIDs must be consistent across index components when merging.
- Posting: a record showing that a term appears in a document; at minimum it contains a docID and usually a term frequency and possibly positions.
- Postings list: the ordered list of postings for a term.
- Inverted index: the mapping from terms to postings lists (see earlier topic "TF-IDF and the inverted index").
- Dynamic inverted index: an inverted index that supports online updates: insertions of new documents, deletions, and modifications, while allowing queries concurrently.
Assumptions commonly used in derivations and algorithms below:
- Main memory is limited; the full index is too large to keep entirely in RAM.
- Disk I/O (sequential and random) dominates cost; writes are costlier than reads in many designs.
- Postings lists for a term are maintained in sorted order by docID to allow efficient merging and merge-based query algorithms (see "Query processing...").
Core approaches to support dynamism
- In-memory buffer + periodic flush and merge (simple incremental indexing).
- Keep a small in-memory inverted index (the buffer) of incoming documents. When full, write it as a new sorted disk block (called a run or segment). To answer queries, consult the on-disk index plus the in-memory buffer; to make a single global postings list, merge runs on demand or periodically.
- This is the basis of SPIMI-style block creation: create a block in memory until full, write it sorted to disk, then later merge blocks.
- Merge-on-write vs merge-on-query
- Merge-on-write: when adding a block, immediately integrate (merge) with existing on-disk data to keep a single sorted postings list per term. This gives faster queries but higher update cost.
- Merge-on-query: keep multiple runs and merge only when answering a query or during scheduled maintenance. This reduces update cost but increases query-time work.
- Log-Structured Merge (LSM)-style multi-level indexing
- Maintain multiple levels of runs. Each level is up to r times larger than the previous. New blocks are inserted at level 0 (in memory→disk). When a level becomes full, merge its runs into the next level. This gives amortized guarantees on writes and bounded number of runs to search. In a leveled LSM, there is at most one run per level; in a tiered LSM there can be multiple runs per level, which increases the number of runs to check but reduces write amplification.
- Important terms: level, size ratio r (growth factor), compaction (the merge process between levels).
Deletion handling
- Because older runs remain on disk, deletions are handled by writing a tombstone (a special record saying docID X is deleted). During merges or compaction, tombstones are applied to remove postings for deleted docIDs.
- Two common policies: lazy deletion (leave tombstones until next merge) and immediate deletion (search and remove postings in all runs; expensive). Document updates are usually implemented as inserting the new version and writing a tombstone for the old version; a per-document or per-posting sequence number/timestamp lets compaction keep only the newest copy.
Concurrency and crash-safety
- To serve queries during updates, systems use lock-free reads, read/write locks, or techniques such as copy-on-write for a segment being merged. A common engineering solution: atomically switch a pointer from old to new index after a merge, so queries either see the old stable view or the new one.
- For crash recovery, ensure that writes are durably recorded (write-ahead log, commit markers) before exposing changes.
Exact algorithmic statement universities ask for (merging two postings lists)
- Given two postings lists A and B sorted by docID with sizes m and n, the standard merge algorithm produces their union (or merged postings) in time linear in m+n by scanning both lists once and writing result entries in order. This is the basis of merging sorted runs.
What to draw for exam diagrams (describe exactly)
- Draw a memory box labelled "RAM buffer (inverted)" with arrows from incoming documents into it. Draw three disk boxes stacked vertically labelled "Segment S0 (small)", "Segment S1 (medium)", "Segment S2 (large)". Draw arrows from RAM buffer to S0 labelled "flush block". Draw arrows from S0 to S1 labelled "merge/compaction" when S0 full. Label sizes with powers of r (S0 size = M, S1 ≤ r·M, S2 ≤ r^2·M). Indicate tombstones stored in segments and a compaction arrow removing deleted docIDs.
Formulas
C_merge = m + n — cost in postings read for a linear merge of two sorted lists of sizes m and n; the number of postings written is also m + n; units: postings (count). Applies to the standard linear merge algorithm.
L = ceil(log_r (N (r - 1) / M + 1)) — number of levels L in a leveled LSM-style index with at most one run per level, when total indexed data is N postings, level-0 capacity is M postings, and the size ratio between levels is r>1; symbols: N = total postings, M = capacity of level 0 (postings), r = growth factor (no units). This comes from M (r^L - 1)/(r - 1) ≥ N.
#runs_to_check ≤ L + 1 — to answer a query for a term you must check at most one run per level plus the in-memory buffer, so at most L + 1 runs; units: count. Applies to LSM-style indexes with at-most-one-run-per-level invariant.
Amortized_writes_per_posting = O(L) — each posting may be rewritten once per level as it is compacted; L is number of levels. This gives the intuition that the total write work per posting grows with the number of levels. (Verify constants for a particular implementation.)
Notes: SI units do not apply to counts; we use counts (postings, runs) as units.
Worked examples
Example 1 — incremental indexing and merging (step-by-step). Initial on-disk index (two segments):
- Segment A: term "apple": postings [(1,2), (4,1)] (format: (docID, tf))
- Segment B: term "banana": postings [(2,1)] In-memory buffer receives documents: Doc 5: "apple banana apple" → produces postings: apple:(5,2); banana:(5,1) Doc 6: "apple" → apple:(6,1) Step 1: build in-memory inverted lists: RAM: apple: [(5,2),(6,1)]; banana: [(5,1)] Step 2: flush RAM to disk as Segment C (sorted by term then docID). After flush, segments: A, B, C. Step 3: merge postings for term "apple" lazily when answering queries or during scheduled compaction. If we merge A and C now for "apple": A.apple = [(1,2),(4,1)], C.apple = [(5,2),(6,1)] Use linear merge (C_merge = 2 + 2 = 4 postings read) → merged apple: [(1,2),(4,1),(5,2),(6,1)] Final merged postings for apple: [(1,2),(4,1),(5,2),(6,1)] (show this result when asked). All steps are numbered; you would draw RAM → write Segment C → optional merge arrow from A+C → merged segment.
Example 2 — deletion with tombstone and compaction. Start: Segment X has term "dog": [(1,1),(3,2),(8,1)] A delete request removes docID 3. Step 1: write tombstone record: delete(docID=3). Tombstone inserted into RAM and flushed to Segment Y. Step 2: during compaction between X and Y, merge postings for "dog": X.dog = [(1,1),(3,2),(8,1)] Y.tombstone = delete(3) Apply tombstone: omit docID 3 from result. Merged result: [(1,1),(8,1)]. After compaction, tombstone has been applied and need not be kept. Final postings: [(1,1),(8,1)].
Common mistakes
- Forgetting to update term frequencies or positions when merging: merges must resolve multiple versions for the same docID correctly (duplicates across runs can occur due to updates); typically keep the newest version using sequence numbers/timestamps, or combine TFs/positions only if that is the intended update semantics.
- Ignoring tombstones during queries: if tombstones are not consulted, deleted documents may appear in results.
- Not considering concurrency and atomic pointer switch when replacing segments — this causes race conditions.
- Mixing up merge cost formula: merging two lists costs m+n, not m·n.
For your exam
Kinds of questions you will get
- Definitions and short notes: define dynamic inverted index, postings, run, tombstone, compaction.
- Derivations and proofs: show linear merge cost for two sorted lists; derive number of levels L for geometric growth.
- Numericals/step-through: given small index and a set of updates, show flushes and the resulting postings lists after merges/deletions (as in examples above).
- Diagrams: draw buffer and multi-level segments, show compaction arrows and where tombstones are stored.
- Design/essay: compare merge-on-write vs merge-on-query, discuss trade-offs: update cost vs query latency, write amplification, memory usage, and concurrency.
What a full-marks answer includes
- Clear definitions of terms used.
- Correct algorithm description (flushing, creating runs, merging policy) with complexity statements and assumptions.
- Correct merge steps with counts (m+n) and final postings.
- Diagram labelled with RAM, segments, sizes and arrows for flush and merge.
- Discussion of deletion strategy and correctness (tombstones + compaction).
Quick check
- What is a tombstone in a dynamic inverted index?
- If two postings lists have sizes 7 and 5, how many postings will a linear merge read?
- In an LSM-style strategy with level-0 capacity M and growth factor r, how many levels are needed to hold N postings (formula)?
Answers: 1) a record marking a docID as deleted; 2) 12; 3) L = ceil(log_r (N (r - 1) / M + 1)).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?