Skip to content

[balance-overrides] replace cached with moka for caching#4418

Open
ashleychandy wants to merge 4 commits into
cowprotocol:mainfrom
ashleychandy:balance-overrides-cached-to-moka
Open

[balance-overrides] replace cached with moka for caching#4418
ashleychandy wants to merge 4 commits into
cowprotocol:mainfrom
ashleychandy:balance-overrides-cached-to-moka

Conversation

@ashleychandy
Copy link
Copy Markdown
Contributor

Description

Replaces cached::SizedCache with moka::sync::Cache in balance-overrides.

cached::SizedCache required external synchronization via Mutex, while moka::sync::Cache is internally concurrent and allows direct cache access without locking boilerplate.

Changes

  • Added a small cache.rs wrapper around moka::sync::Cache<K, V>
  • Replaced Mutex<SizedCache<K, V>> with Cache<K, V>
  • Removed lock().unwrap() and migrated cache_get/cache_set to cache.get()/insert()
  • Removed explicit clones on cache reads
  • Removed cached and cleaned up related transitive dependencies
  • Simplified test cache setup with direct .insert() calls

@ashleychandy ashleychandy requested a review from a team as a code owner May 16, 2026 00:29
Copy link
Copy Markdown

@claude claude Bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces the cached crate with moka for strategy caching within the balance-overrides crate. It introduces a thread-safe Cache wrapper around moka::sync::Cache, eliminating the need for manual Mutex management in the approval and balance detectors. No critical issues found.

@jmg-duarte
Copy link
Copy Markdown
Contributor

As this is a performance improvement (which we've discussed before) what would be your testing plan for this?

Where would you put metrics before and after so that I could ship both to prod and measure between the two?

@ashleychandy ashleychandy force-pushed the balance-overrides-cached-to-moka branch from 5b3ed18 to 872c9a0 Compare May 16, 2026 12:18
@ashleychandy
Copy link
Copy Markdown
Contributor Author

ashleychandy commented May 16, 2026

Good point. I put together a separate branch for benchmarking/instrumentation work so I didn’t have to bloat the main PR with experimental code.

Branch:
ashley/balance-overrides-moka-benchmarking

It includes:

  • cache hit/miss metrics
  • cached vs uncached latency histograms
  • cache entry/eviction metrics
  • release labels so different deployments can be compared side-by-side in prod
  • a local contention benchmark comparing moka::sync::Cache against the previous Mutex<cached::SizedCache> setup

The synthetic concurrent-read benchmark came out around:

  • moka: ~21ms
  • Mutex<cached::SizedCache>: ~72ms

Benchmark command:

cargo test -p balance-overrides compare_moka_vs_mutex_cache -- --ignored --nocapture

Though I understand the real validation still needs to come from production metrics rather than the local benchmark itself.

Do these sound like the right signals to monitor?

@github-actions
Copy link
Copy Markdown

This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed.

@jmg-duarte
Copy link
Copy Markdown
Contributor

I bring good news!

I tried this out on staging, performance improvement wasn't certainly amazing, however, it's much more stable

duration_variance

In red — cached, in blue moka

The number of traces is 100, is not amazing but its what our Tempo setup allowed me to collect

Raw Data

cached
moka

@jmg-duarte jmg-duarte changed the title replace cached with moka for caching [balance-overrides] replace cached with moka for caching May 29, 2026
Comment thread crates/balance-overrides/src/cache.rs Outdated
Comment thread crates/balance-overrides/src/cache.rs Outdated
V: Clone + Send + Sync + 'static,
{
pub(crate) fn new(max_capacity: u64) -> Self {
let data = moka::sync::Cache::builder()
Copy link
Copy Markdown
Contributor

@metalurgical metalurgical May 31, 2026

Choose a reason for hiding this comment

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

Should ensure the same admission and eviction policy is used for cache replacement (at least initially or otherwise indicated) so that they are still handling what gets cached the same way.

I think the equivalent here is:

let data = moka::sync::Cache::builder()
            .max_capacity(max_capacity)
            .eviction_policy(EvictionPolicy::lru())
            .build();

Copy link
Copy Markdown
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

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

LGTM, assuming all the pending comments are addressed.

Comment thread crates/balance-overrides/src/balance/mod.rs Outdated
@ashleychandy
Copy link
Copy Markdown
Contributor Author

@jmg-duarte @squadgazzz Thanks for the review. I've addressed the pending comments and applied the suggested changes

Copy link
Copy Markdown
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Change makes sense to me. Just a minor nit. 👍

Comment thread crates/balance-overrides/src/approval/mod.rs Outdated
@ashleychandy
Copy link
Copy Markdown
Contributor Author

@MartinquaXD Thanks for the suggestion. Propagated cache_size as u64 up to the config boundary

@ashleychandy ashleychandy requested a review from jmg-duarte June 5, 2026 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants