Skip to content

feat: shared LRU block cache across all three DBs#202

Open
EddieHouston wants to merge 1 commit intoBlockstream:new-indexfrom
EddieHouston:pr/shared-lru-cache
Open

feat: shared LRU block cache across all three DBs#202
EddieHouston wants to merge 1 commit intoBlockstream:new-indexfrom
EddieHouston:pr/shared-lru-cache

Conversation

@EddieHouston
Copy link
Copy Markdown
Collaborator

@EddieHouston EddieHouston commented Mar 31, 2026

Summary

  • Creates a single shared rocksdb::Cache passed to all three DBs (txstore, history, cache) instead of each DB creating its own
  • --db-block-cache-mb now controls the total cache size, not per-DB
  • Eliminates wasted allocation on the tiny cache_db (~3 MB of data was getting a full equal share of cache)
  • txstore and history claim cache proportional to actual usage via LRU eviction

Background

electrs maintains three RocksDB instances: txstore (~900 GB), history (~555 GB), and cache (~3 MB). Previously each got its own block cache of --db-block-cache-mb size, so the total was 3x the configured value. The cache_db's allocation sat
almost entirely unused.

With a shared cache, the same total memory is used more efficiently. txstore (which holds the bulk of the data and handles the most lookups) naturally claims the lion's share through LRU eviction, while cache_db uses only what it needs.

Migration note: --db-block-cache-mb now means total, not per-DB. To match previous behavior, triple the old value (e.g. old --db-block-cache-mb=6144 = 18 GB total → new --db-block-cache-mb=18432).

Note: future step is to convert the LRU --> HyperClockCache

Test plan

  • cargo check / cargo build with default features
  • cargo build --features liquid
  • Verify Prometheus block-cache-capacity metric is identical across all three DBs
  • Verify block-cache-usage reflects shared pool (not 3 independent pools)
  • No functional changes — same data, same queries, just shared cache

  Single shared cache for txstore, history, and cache DBs instead of
  per-DB caches. --db-block-cache-mb now controls the total cache size.
  Eliminates wasted allocation on the tiny cache_db and lets txstore
  and history claim cache proportional to actual usage via LRU eviction.
let mut block_opts = rocksdb::BlockBasedOptions::default();
let cache_size_bytes = config.db_block_cache_mb * 1024 * 1024;
block_opts.set_block_cache(&rocksdb::Cache::new_lru_cache(cache_size_bytes));
let owned_cache;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider adding a type annotation for readability:
let owned_cache: rocksdb::Cache;

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.

2 participants