receive: Add buffer pooling to reduce heap allocations and GC pressure#283
Merged
receive: Add buffer pooling to reduce heap allocations and GC pressure#283
Conversation
This commit implements memory optimizations for the receive handler: - Add sync.Pool for compressed/decompressed buffers and WriteRequest - Implement pool size caps (1MB/4MB) to prevent RSS inflation - Add limitedBufferWriter to enforce size limits during streaming - Add zlabelsGet helper to avoid ZLabels->PromLabels conversions - Add tenantKeyForDistribution for consistent tenant routing - Improve errorSeries counting with precomputed tenant mappings These changes reduce memory allocations in the hot path and prevent memory retention from oversized requests. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
yuchen-db
approved these changes
Feb 4, 2026
Comment on lines
+762
to
+768
| // Deep copy all label strings to detach them from pooled decode buffer. | ||
| // Required for correctness when pooled buffers are reused and for preventing | ||
| // retention of the whole request buffer via zero-copy label references. | ||
| for i := range wreq.Timeseries { | ||
| labelpb.ReAllocZLabelsStrings(&wreq.Timeseries[i].Labels, h.writer.opts.Intern) | ||
| } | ||
|
|
Collaborator
There was a problem hiding this comment.
nit: shall we detach exemplar labels from the pool as well?
Collaborator
Author
There was a problem hiding this comment.
I'll do this in a follow up PR.
yuchen-db
pushed a commit
that referenced
this pull request
Feb 26, 2026
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.
Significantly reduces memory allocations and GC pressure in the Thanos receive hot path by introducing buffer pooling and streaming size limits. Testing shows
23% heap reduction, 27x faster GC max pauses, and estimated 30% CPU reduction at production scale.