THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Bitcoin Core RPC Polling vs ZMQ: Hybrid Monitoring and Failure Recovery

permalink THAMER
#bitcoin#monitoring#polling#rpc#zmq

Bitcoin applications can monitor a node using periodic JSON-RPC requests, asynchronous ZMQ notifications, or a combination of both. The appropriate design depends on latency requirements, node load and the cost of an incorrect application state.

RPC polling

Polling retrieves current information at scheduled intervals. For example, getbestblockhash detects a change in the active tip, while getrawmempool returns the current local mempool contents. Wallet RPCs can provide wallet-specific transaction information where the relevant wallet is loaded.

Polling is straightforward to restart because each query retrieves current state. However, a transaction can enter and leave the mempool between polls. Repeatedly downloading a large mempool also consumes bandwidth and processing resources.

Event-driven monitoring

ZMQ can notify an application shortly after the node processes a relevant event. This reduces the need for frequent full snapshots. It does not guarantee delivery, persistence or replay. Slow subscribers can lose messages, and a network interruption can leave a cache stale without an immediately visible error.

A hybrid design

At startup, subscribe to the required topics and buffer events. Obtain the current chain and mempool snapshots through RPC. Reconcile the buffered events against the snapshot boundaries, then process live updates. Periodically compare the cached state with RPC results even when the notification stream appears healthy.

Use bounded queues and coalesce repeated refresh requests. A block notification can trigger a chain reconciliation job rather than spawning a separate worker for every event. If a queue overflows, mark the cache as uncertain and rebuild instead of silently dropping state transitions.

Operational readiness

Track the last successful RPC response, last completed reconciliation, processed block hash, queue depth and detected message gaps. Distinguish node connectivity from application synchronization. A quiet ZMQ stream is not proof that the node is disconnected, since relevant events may simply be absent.

When RPC becomes unavailable, expose stale or unknown state rather than returning a misleading zero balance. Retry using bounded exponential backoff and restore readiness only after reconciliation succeeds.

Testing failure modes

Use an isolated regtest node to simulate process restarts, mempool changes and block reorganizations. Deliberately stop the subscriber while generating events, then restart it and compare the recovered state with RPC. The key test is whether the final state converges correctly, not whether every notification was received.

Primary references

Bitcoin Core ZMQ documentation: https://github.com/bitcoin/bitcoin/blob/master/doc/zmq.md
Bitcoin Core RPC documentation: https://bitcoincore.org/en/doc/

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects