How to Manage Nonces for Reliable Transactions
When building on Kaia or any EVM-compatible blockchain, nonce management is one of those details that can make your dApp run smoothly or lead to stuck or failing transactions. This guide explains what nonces are, why they matter, and how to handle them effectively when sending bulk or sequential transactions.
What is a Nonce?
A nonce (number used once) is the transaction counter for an account (EOA). The first transaction uses nonce 0; each subsequent transaction increments by 1. Transactions must be processed in strict order (0, 1, 2, …). This ordering prevents replay and ensures intended execution order. Wallets typically manage this for end users, but high-throughput systems or fee-delegated flows often need explicit control.
Common Nonce Issues
When transactions are sent with incorrect nonce values, several problems can occur:
Gap in nonce sequence
Later transactions get stuck waiting for the missing nonce to be filled. For example, if you send transactions with nonces 0, 1, and 3 (skipping 2), transactions 3 and beyond won't process until nonce 2 is submitted.
Duplicate nonce
Two transactions with the same nonce will compete. Whichever gets confirmed first wins. The other gets dropped.
Bulk transaction collisions
When sending multiple transactions quickly without careful nonce tracking, some may fail or stall due to nonce conflicts.
Managing Nonces for Bulk Transactions
When sending many transactions (batch operations, NFT mints, arbitrage bots), you cannot rely on wallet automation alone. You need a systematic approach.
1. Maintain an Off-Chain Nonce Store
Use a datastore (Redis, Postgres, or in-memory map) to track the next nonce per account. When starting a session, sync with the blockchain using eth_getTransactionCount
. After sending each transaction, increment your local nonce counter immediately.
This approach gives you precise control over nonce assignment and prevents conflicts when sending transactions in rapid succession.
2. Use Web3 Libraries for Automation
Libraries like ethers.js and web3.js handle nonce assignment automatically for sequential transactions. For bulk or parallel transactions, override the nonce manually from your nonce store.
3. Parallelize Transactions with Multiple Accounts
If transactions are independent, distribute them across multiple EOAs. For example, instead of one account sending 100 transactions (creating a nonce bottleneck), use 10 accounts sending 10 each. This scales your throughput significantly.
4. Implement Robust Error Handling
Detect dropped or stuck transactions (often due to low gas). Replace them with the same nonce plus a higher gas price. This process, known as "speeding up," ensures your transaction queue keeps moving.
Retry logic is critical in production environments. Build mechanisms to handle temporary failures and resubmit transactions when needed.
5. Monitor with Blockchain Explorers
Use tools like KaiaScan, OKX Explorer, or custom indexers to verify nonce progression. This helps debug when transactions fail to propagate or get stuck in unexpected states.
6. Design for Scalability
For high-throughput systems (trading bots, batch mints), design workflows that dynamically allocate new EOAs, use nonce queues to ensure ordering, and scale horizontally by distributing transactions across multiple accounts.
7. Manual Adjustment for Advanced Users
Provide an option to manually set nonces in case of stuck transactions. Most wallets (like MetaMask) already support this for power users who need fine-grained control.
Example Flow: Bulk Transaction Management
Here's how a typical nonce management system works for bulk transactions:
- Fetch the current nonce: Query your nonce manager (for example, Redis) to get the last used nonce for the sender address
- Increment and assign: Increment the nonce locally and assign it to the next transaction
- Send the transaction: Submit the transaction to the network
- Update the store: Immediately update the nonce in your datastore (Redis, database) based on the sender address
- Handle failures: If a transaction fails (for example, Tx(1)), implement retry logic with a higher gas fee using the same nonce before proceeding to Tx(2)
The key is maintaining a centralized nonce manager that tracks the last used nonce per address. Each transaction increments this value (Tx(0) uses nonce 0, Tx(1) uses nonce 1, and so on) and updates the store atomically. This prevents nonce collisions when sending transactions in rapid succession.
For estimating gas limits and prices (including choosing a higher fee when retrying), see How to estimate gas limits and prices on Kaia Wallet and MetaMask.
Conclusion
Reliable nonce management is essential for transaction-heavy systems. Use an off-chain nonce store, proper error handling (same nonce, higher fee), monitoring, and horizontal scaling to keep throughput high and avoid stalls. The same nonce rules apply to fee-delegated transactions.