fix: Batch size limiting for block persistence #17979
Open
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.
Problem Description
In sub-second block time scenarios, we identified a critical performance degradation issue related to block persistence that creates a cascading effect on system performance. The problem manifests as a vicious cycle that significantly impacts the overall node performance.
Root Cause Analysis
The issue stems from the interaction between canonical block accumulation and the persistence mechanism in
get_canonical_blocks_to_persist()
. Here's the problematic flow:get_canonical_blocks_to_persist()
function returns all available canonical blocks for persistence without size limitsUnifiedStorageWriter::commit()
becomes increasingly expensive with larger batch sizesUnifiedStorageWriter::commit()
to occasionally hang, further preventing memory cleanupCanonicalInMemoryState::remove_persisted_blocks()
The Vicious Cycle
Solution
Implementation
We introduced a batch size limit to prevent the snowball effect:
Experimental Results
Experimental environment: One million accounts, two blocks generated per second, with 6000 transactions per block
Before optimization: Accumulated canonical blocks lead to a gradual increase in memory


After optimization: Canonical blocks are released in a timely manner, and the memory usage stabilizes after reaching its peak