Inverted Index: The Engine of Fast Log Search
An inverted index makes log search fast by mapping terms to the logs containing them, like a book's index. It powers platforms like Splunk or Elasticsearch, enabling instant searches across terabytes of data. The footgun is indexing high-cardinality fields.
Why it exists
Modern systems generate terabytes of log data daily. Searching this raw text with tools like grep is impossibly slow at scale. We need a way to find a specific error message or trace a single user's activity in seconds, not hours, without scanning every single log line.
The mental model
An inverted index is like the index at the back of a technical book. To find every mention of 'database', you don't read the whole book. You look up 'database' in the index and get a list of page numbers. In logging, the 'words' are parsed tokens (like 'error', 'user_id:123') and the 'page numbers' are the unique IDs of the log events.
How it works
When a new log arrives, the logging system parses it into individual terms or key-value pairs, called tokens. For each token, it adds the new log's ID to a list associated with that token. The index is essentially a giant dictionary mapping tokens to a list of log IDs where they appear. A search for 'service:api AND level:error' doesn't scan logs; it finds the list for 'service:api', finds the list for 'level:error', and calculates the intersection of those two lists. This is orders of magnitude faster than a brute-force text scan.
When to use it
Use an inverted index whenever you need fast, full-text search on large volumes of semi-structured documents. It is the foundational data structure for log management platforms (Splunk, Elasticsearch), document databases, and web search engines. It excels at 'needle in a haystack' lookups where you know what you're looking for.
When not to use it
The trade-off for fast reads is slower, more expensive writes. Every indexed term requires an update during ingestion. The biggest mistake is indexing high-cardinality fields, like request_id, trace_id, or nanosecond-precision timestamps. Each unique value becomes a new entry in the index, causing it to swell in size, sometimes becoming larger than the log data itself. This kills ingestion performance and balloons storage costs. For analytics requiring aggregations across all data, columnar databases are often a better fit.
One canonical example
A user reports a bug with a specific request_id. An engineer pastes this ID into their logging platform's search bar. Instead of scanning petabytes of log files, the system performs a single lookup in its inverted index for the key request_id:<value>. It instantly gets back a short list of log event IDs, retrieves them from storage, and displays them to the engineer in seconds.
Interview question
What is the main consequence of indexing high-cardinality fields, such as request_id, in an inverted index for log data?
- a.It forces the search engine to revert to brute-force text scanning for those fields.
- b.It prevents the system from performing boolean logic (AND/OR) across different indexed terms.
- c.It makes it impossible to retrieve individual log events associated with those specific field values.
- d.It significantly increases the index's storage footprint and slows down data ingestion.Correct
Why? this is the answer
Indexing high-cardinality fields causes the inverted index to swell in size, sometimes exceeding the raw data, which directly leads to ballooned storage costs and degraded ingestion performance. While performance suffers, the system still uses the index; it does not revert to brute-force scanning for indexed fields.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #logging
- #sre
- #databases
- #search
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles