Sub topic 7
Nonces and Transaction Ordering
This chapter assumes you understand blockchain accounts, addresses, networks, and basic transaction fees. It focuses on account-based networks such as Ethereum and Ethereum-compatible chains. Bitcoin-style UTXO networks use a different ordering model.
What a nonce is
A nonce is a sequence number attached to a transaction from an account. For an externally owned account, the first transaction usually has nonce 0, the next has nonce 1, and so on. The network uses the nonce to prevent replaying the same transaction and to order transactions from one sender.
A nonce belongs to the sender, not the recipient. Two different accounts can both send transactions with nonce 4. They do not conflict because each account has its own sequence.
A transaction with nonce 7 normally cannot be included before that account's transaction with nonce 6. The network may accept nonce 7 into a node's pending pool, but it generally waits for the missing transaction.
| Transaction | Nonce | Typical result |
|---|---|---|
| Alice sends 1 ETH | 12 | Can execute if nonces 0–11 are complete |
| Alice sends 2 ETH | 14 | Waits if nonce 13 is missing |
| Bob sends 1 ETH | 14 | Unrelated to Alice's transaction |
Account models and ordering
In an account model, the chain stores an account's balance and nonce. Every successful transaction increments the sender's nonce. A failed contract call usually still consumes the nonce because the transaction was included and its gas was used.
A UTXO model, such as Bitcoin, does not assign one sequence number to an account. Each transaction spends specific unspent transaction outputs, or UTXOs. Conflicts occur when two transactions try to spend the same UTXO. Wallets choose inputs and construct a transaction graph instead of advancing one account nonce.
Reading nonce values
Ethereum-compatible nodes commonly expose two useful counts:
latestis the next nonce after transactions included in the latest known block.pendingalso includes transactions currently known in the node's pending pool.
import { JsonRpcProvider } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const address = process.env.SENDER_ADDRESS;
const confirmedNonce = await provider.getTransactionCount(address, "latest");
const pendingNonce = await provider.getTransactionCount(address, "pending");
console.log("Next confirmed nonce:", confirmedNonce);
console.log("Next available nonce:", pendingNonce);
console.log("Transactions waiting locally:", pendingNonce - confirmedNonce);The difference is only an estimate. Another node may know about different pending transactions, and providers may use different transaction pools.
Ordering, parallel sends, and replacement
If one process sends several transactions, it should coordinate nonce assignment. Two workers that both read nonce 12 can create conflicting transactions with the same nonce. Usually only one can be included.
A transaction with a particular nonce can sometimes be replaced while it is pending. The replacement uses the same nonce and usually offers a higher fee. Replacement rules vary by network and node, so a higher fee does not guarantee acceptance. A replacement cannot change the already-used nonce.
A later transaction can remain pending because an earlier one is stuck. To resolve this, inspect the earlier transaction, then either wait, replace it with an allowed higher-fee transaction, or cancel it by sending a zero-value transaction to your own address with the same nonce. Cancellation is itself a replacement and is not guaranteed.
Common nonce errors
- Using
"latest"when you need to account for pending transactions can reuse a nonce. - Assuming a wallet and an exchange share the same transaction history can produce conflicts if both control the same address.
- Treating a failed transaction as free is incorrect. Its nonce is consumed and its gas is usually charged.
- Assuming a transaction hash proves ordering is incorrect. Inclusion in a block and the transaction's nonce determine execution order.
Exercise
Alice's nonce 20 transaction is pending, and her nonce 21 transaction is also pending. Can nonce 21 normally be included first, and what should Alice inspect?
Show answer
No. Nonce 21 normally waits for nonce 20. Alice should inspect the nonce 20 transaction, its fee settings, provider status, and whether the network permits replacing it.