perf(ingester): lazy regex evaluation on head postings cache miss#7553
Open
alanprot wants to merge 1 commit into
Open
perf(ingester): lazy regex evaluation on head postings cache miss#7553alanprot wants to merge 1 commit into
alanprot wants to merge 1 commit into
Conversation
When the expanded postings cache misses on the head block, regex matchers on high-cardinality labels (e.g. pod with 400K+ values) dominate query cost. This PR defers expensive regex matchers to a lazy per-series evaluation when a selective equality matcher already narrows the result set significantly. On cache miss, splitMatchersForHeadWithConfig splits matchers into: - Selective matchers (equality, low-card regex) for postings lookup - Lazy matchers (high-card regex) applied per-series via LabelValueFor A cost-ratio gate decides when deferral is worthwhile: - Simple regex (single contains, prefix): cardinality > selectivePostings * 6 - Complex regex (multi-substring, capture groups): cardinality > selectivePostings * 2 Label cardinality lookups are cached in an expirable LRU (60s TTL) to avoid repeated LabelValues calls under load. Benchmark (realistic pod names, 413K cardinality, 9K selective postings): - Eager: 62ms, 29.8MB per query - Lazy: 14ms, 12.6MB per query (4.5x faster, 58% less memory) New flags (disabled by default with max-cardinality=0): - blocks-storage.expanded_postings_cache.head.lazy-matcher-max-cardinality - blocks-storage.expanded_postings_cache.head.lazy-matcher-simple-cost-ratio - blocks-storage.expanded_postings_cache.head.lazy-matcher-complex-cost-ratio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lazy regex evaluation on head postings cache miss
When the expanded postings cache misses on the head block, regex matchers on high-cardinality labels (e.g.,
podwith 400K+ values) dominate query cost — the regex runs against every label value to build the posting list.This PR defers expensive regex matchers to a lazy per-series evaluation when a selective equality matcher (like
__name__=) already narrows the result set significantly.How it works
On cache miss,
splitMatchersForHeadWithConfigsplits matchers into:LabelValueForafter the selective postings are resolvedA cost-ratio gate decides when deferral is worthwhile:
cardinality > selectivePostings × 6cardinality > selectivePostings × 2Label cardinality lookups are cached in an expirable LRU (60s TTL) to avoid repeated
LabelValuescalls under load.Benchmark results (realistic pod names, 413K cardinality, 9K selective postings)
Configuration
Three new flags (all disabled by default —
max-cardinality=0):Testing
TestLazyMatchersFuzz): 300 fuzzed queries + injected regex patterns compared between eager and lazy instances — 450+ lazy triggers, zero mismatchesChecklist
CHANGELOG.mdupdated - the order of entries should be[CHANGE],[FEATURE],[ENHANCEMENT],[BUGFIX]docs/configuration/v1-guarantees.mdupdated if this PR introduces experimental flags