startup: decouple precache and mempool sync from server startup#218
Draft
DeviaVir wants to merge 1 commit into
Draft
startup: decouple precache and mempool sync from server startup#218DeviaVir wants to merge 1 commit into
DeviaVir wants to merge 1 commit into
Conversation
7d872cc to
7a82462
Compare
After a pod/node restart, electrs refused to listen on any port until it
had serially completed (1) the popular-scripts precache and (2) a full
initial mempool sync over JSONRPC. On a congested mainnet mempool this
kept instances unready for 15-30+ minutes even though the chain index
was fully usable within seconds (measured: 17 min total, of which 15 min
was mempool sync).
- Run --precache-scripts in a background thread; it is pure cache
warming and never affects correctness. The file is still read upfront
to fail fast on a bad path.
- Add --serve-during-mempool-sync (default off, no behavior change):
start the REST/Electrum servers before the initial mempool sync.
Chain-based queries are fully correct during the sync; mempool-derived
data is incomplete until the first full sync completes.
- Add GET /health/ready returning {chain_synced, mempool_synced} with
200 once the initial mempool sync has completed and 503 before that,
so load balancers / readiness probes can keep early-serving instances
out of rotation. Deployments enabling --serve-during-mempool-sync MUST
switch readiness probes from a TCP check to this endpoint.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
7a82462 to
e9f4b3b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
After a restart, electrs refuses to listen on any port until it has serially completed the popular-scripts precache and a full initial mempool sync over JSONRPC. With a congested mainnet mempool this keeps an instance unreachable for 15-30+ minutes, even though the chain index is fully usable within seconds of opening the database.
Measured on a production mainnet instance (2026-06-11): ~17 minutes from process start to first accepted connection, of which DB open + chain catch-up was 17s, precache 17s, and the initial mempool sync 15.2 min (including a bitcoind
disconnected while receivingmid-load).Recent perf work (e.g. running parallel RPC requests on the
rpc_threadspool) makes the mempool sync faster, but doesn't change the ordering: however quick the sync gets, the servers still won't accept a single connection until it finishes, and the precache still runs serially before it. This PR removes the waiting and adds the health signal needed to do that safely.Changes
--serve-during-mempool-sync(default off → zero behavior change). When enabled, the REST/Electrum servers start before the initial mempool sync. Chain/block/tx/address queries are fully correct during the sync; mempool-derived data (unconfirmed history entries, outspends,/mempool*, Electrum subscription status) is incomplete until the first full sync. Fee estimates are unaffected (they come from bitcoind'sestimatesmartfee).GET /health/ready→{"chain_synced":true,"mempool_synced":bool}, HTTP 200 once the initial mempool sync has completed, 503 before.Cache-Control: no-cache.Operational coupling (important)
Enabling
--serve-during-mempool-syncwhile health checks are plain TCP checks would route traffic to instances with an incomplete mempool view. Rollout order per deployment:GET /health/readyon the HTTP port.--serve-during-mempool-syncto the start arguments.End state: the process accepts connections ~30s after start, health checks still flip to healthy only when the mempool is synced (same externally visible behavior as today), and operators gain visibility into which startup phase an instance is in instead of staring at a closed port. Deployments fronted by redundant instances can later opt to gate health on
chain_syncedonly and accept a briefly stale mempool to cut restart downtime to ~1 minute.Notes for reviewers
Mempool::updatealready returnsOk(true)only after a full consistent snapshot; thesyncedflag is set at both success returns and is monotonic.add()holds the mempool write lock for the bulk apply; early-served requests touching the mempool can briefly block. Traffic is unaffected when health checks gate on/health/ready.cargo check --all-targets(both feature sets),cargo test --lib, CI integration suites green.