Skip to content

Stop caching every mempool tx for bitcoind - #1216

Open
benthecarman wants to merge 1 commit into
lightningdevkit:mainfrom
benthecarman:bound-bitcoind-mempool-cache
Open

benthecarman wants to merge 1 commit into
lightningdevkit:mainfrom
benthecarman:bound-bitcoind-mempool-cache

Conversation

@benthecarman

Copy link
Copy Markdown
Contributor

Ldk-server was taking a lot of memory on my server. Doing this reduced memory by 100MB for me

With a bitcoind chain source, every mempool transaction was fetched into mempool_txs_cache and kept there until it left the mempool, so the node held a full decoded copy of bitcoind's mempool (roughly 0.9 KB per transaction, i.e. hundreds of MB when the mempool is full). Only the onchain wallet consumes these transactions, and it keeps just the relevant ones.

The cache only serves transactions we may have to emit again, i.e. those that entered the mempool above our best processed height. Restrict it to those. Also stop shrinking the entries map and pre-sizing the emit buffer to the full mempool on every two-second poll, which churned multi-megabyte allocations and fragmented the heap.

@benthecarman
benthecarman requested a review from tnull October 29, 2026 23:06
@ldk-reviews-bot

ldk-reviews-bot commented Oct 29, 2026 •

Copy link
Copy Markdown

👋 Thanks for assigning @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

Comment thread src/chain/bitcoind.rs Resolved
Comment thread src/chain/bitcoind.rs Resolved
With a bitcoind chain source, every mempool transaction was fetched
into `mempool_txs_cache` and kept there until it left the mempool, so
the node held a decoded copy of every transaction in bitcoind's
mempool, which can reach hundreds of MB when the mempool is full. Only
the onchain wallet consumes these transactions, and it keeps just the
relevant ones.

The cache only needs the transactions we may still have to emit, i.e.,
those that entered the mempool above our best processed height, and
those a poll fetched before it failed, so the retry can resume where
it left off. Restrict it to those, and shrink it after pruning.

This only lowers steady-state memory use. `mempool_entries_cache`
still holds one small entry per mempool transaction, and the first
poll after startup still holds the whole mempool twice, in the cache
and in the transactions it hands to the wallet, until the next poll
prunes the cache.

Also stop pre-sizing the transaction cache and the emit buffer to the
whole mempool on every poll.

This change was made with the help of Claude Code.
@benthecarman
benthecarman force-pushed the bound-bitcoind-mempool-cache branch from a3e88eb to 50cac04 Compare November 5, 2026 19:55
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.

3 participants