Skip to content

Bound RecentBlockCache by age#4587

Merged
MartinquaXD merged 4 commits into
mainfrom
bound-block-cache-by-age
Jul 3, 2026
Merged

Bound RecentBlockCache by age#4587
MartinquaXD merged 4 commits into
mainfrom
bound-block-cache-by-age

Conversation

@MartinquaXD

@MartinquaXD MartinquaXD commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

Especially on BNB we are using an insane amount of RPC calls for fetching uniswap v2 liquidity which already starts high after a restart and then grows over time until it flattens out.
Screenshot 2026-07-02 at 20 18 56
As it turns out the graph flattens out when the recently_used set of the RecentBlockCache hits its size limit of 1000 - meaning at this point we try to keep 1000 uni v2 pools up-to-date via a background task. However, in a usual auction we actually only deliver ~500 uni v2 pools to the solvers for building their solutions. So effectively we are wasting half of those RPC requests keeping pools updated that can only be used during quoting.

The reason the cache starts huge and grows even bigger is that we have a lot of long standing limit order with many different tokens (initial size) and over time market orders increase the set of used tokens even further (growth) until we hit the limit.

Changes

To keep all the relevant pools updated without suffering linear growth over time the recently_used set is now also bounded to 5 minutes. That way an order requiring new liquidity will have plenty of time to get settled and soon after the now irrelevant AMM stops getting updated.

Additionally I reduced the number of blocks a cache snapshot is kept alive to 2 (from 10). This should effectively reduce the cache size by 80%. This should also not cause any issues with liquidity that gets flushed out too soon.
This long lookback was primarily intended to have liquidity quickly available for quoting when the liquidity caches were fragmented across different orderbook instances but since they now live in the driver and the driver keeps a ton of highly relevant liquidity up-to-date for the auctions we'll still get a high cache hit rate with shorter lookback.

How to test

test run in prod to verify constant RPC calls and reduced memory usage.

I also updated the quoting with stale liquidity e2e test since we now have to unbalance the pool with a single block to uphold the original assertion with a recent block cache depth of 2 blocks.

@MartinquaXD MartinquaXD requested a review from a team as a code owner July 2, 2026 20:32
@claude

claude Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Claude encountered an error after 2m 41s —— View job


PR Review: Bound RecentBlockCache by age

  • Read changed files and surrounding code
  • Analyze correctness of age-bounding logic
  • Analyze cache block-retention change
  • Post inline comments + summary

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

Gemini encountered an error creating the review. You can try again by commenting /gemini review.

@jmg-duarte jmg-duarte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I'm looking forward to the memory savings given all the recent effort we've put in lowering the consumption

Additionally, we could consider changing the cached with moka for an additional speed boost and less unwraps in the code

@MartinquaXD MartinquaXD added this pull request to the merge queue Jul 3, 2026
Merged via the queue into main with commit f3bd475 Jul 3, 2026
20 checks passed
@MartinquaXD MartinquaXD deleted the bound-block-cache-by-age branch July 3, 2026 09:33
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants