Nonce Management for Batch Senders
Bottom line: A nonce is per-address, strictly sequential, and one gap stalls everything behind it. Batched senders must allocate nonces from a single serialised source, persist them, and reconcile against eth_getTransactionCount before each burst.
Why gaps are fatal
Transactions mine strictly in nonce order. Nonce 7 pending means 8, 9, 10 sit unmined even at generous fees. A crashed sender that allocated 7 but never broadcast blocks the address until 7 is filled or replaced.
Allocation
Never call eth_getTransactionCount(addr, "pending") per worker — concurrent workers read the same value and collide. Allocate from a local counter seeded once, persisted to disk immediately:
1 | class Nonce: |
Seed with "pending" so in-flight transactions are counted, then reconcile every 30 s against "latest": if onchain + broadcast > local, raise local.
Recovery
On restart, take the max of persisted counter and both onchain counts. Then fill any gap with a 0-value self-transfer at current fees before resuming real work.
Gaps left more than an hour: fill them, do not wait. An empty block window will not clear them for you.
Parallelism
Nonces can be allocated ahead of broadcast, mined in order. Broadcast interval should exceed maxFee propagation or later transactions land before earlier ones at the same node and get queued locally — same result, different failure signature.
What breaks under load
- Reorgs returning a nonce to unused: reconcile by hash, not by count
- A provider returning stale
pending: reconcile against two providers and take the max - Two senders on one address: give each its own address, not a shared nonce counter
The discipline
One address per sender, one serialised allocator, persisted counter, 30 s reconciliation, gap fill on start. Every “randomly stuck transactions” report traces back to a missing one of those four.
Operating notes
Persist the counter before broadcasting, not after. A counter written after sendRawTransaction loses the increment if the process dies mid-call, and the next start reuses the nonce and strands both. Write, fsync, then broadcast.
Reconcile every thirty seconds against pending, not latest. Pending includes the mempool, which is where the in-flight transactions are; latest lags a block and always under-reports.
Gap fill
On start, take the maximum of the persisted counter and both on-chain counts. Walk from the on-chain latest nonce upward; every gap becomes a zero-value self-transfer at the 80th-percentile priority fee. Do not wait for gaps to clear on their own — an empty block will not consume them.
Concurrency budget
One address per sender. Two senders sharing an address need a distributed lock on the counter or they will collide. If the throughput requires more than about 50 transactions per minute, add addresses rather than tightening the interval.
Alerting
Alert on: counter minus on-chain latest greater than 20, any gap open longer than ten minutes, and a reconcile that raises the counter by more than 5 in one pass. The first two are stalls, the third is a second sender you did not know about.
FAQ
Why do my transactions sit unmined even though I set a high fee?
Transactions mine strictly in nonce order, so a single pending nonce blocks everything behind it regardless of fee. If nonce 7 is pending, nonces 8, 9, and 10 stay unmined even at generous fees. A crashed sender that allocated a nonce but never broadcast blocks the address until that nonce is filled or replaced.
Why shouldn’t each worker call eth_getTransactionCount to get its own nonce?
Concurrent workers calling eth_getTransactionCount(addr, "pending") read the same value and collide on the same nonce. Instead, allocate from a local counter seeded once and persisted to disk immediately. The counter should be seeded with "pending" so in-flight transactions are counted.
How do I recover nonces after a sender crashes and restarts?
On restart, take the maximum of the persisted counter and both onchain counts. Then fill any gap with a 0-value self-transfer at current fees before resuming real work. Gaps left more than an hour should be filled rather than waited out, since an empty block window will not clear them.
Should I reconcile against “pending” or “latest”?
Reconcile every thirty seconds against "pending", not "latest". Pending includes the mempool, which is where in-flight transactions are, while latest lags a block and always under-reports. Seed with "pending" as well, then reconcile against "latest" to raise the local counter when onchain plus broadcast exceeds it.
What should I do if two senders need to share one address?
Give each sender its own address rather than a shared nonce counter. Two senders sharing an address need a distributed lock on the counter or they will collide. If throughput requires more than about 50 transactions per minute, add addresses rather than tightening the broadcast interval.











