Coin Selection Strategy
Learn about Bitcoin coin selection strategies and how UTXO choice affects fees, privacy, and change.
Key Takeaways
- A coin selection strategy is the algorithm a wallet uses to choose which UTXOs to spend in a transaction, directly affecting fees, privacy, and future wallet health.
- Bitcoin Core runs multiple strategies in parallel: Branch and Bound, Knapsack, Single Random Draw, and CoinGrinder. It scores each result with a waste metric and keeps the cheapest option.
- No single strategy wins in all conditions: low fees favor consolidation (more inputs), high fees favor minimal inputs, and privacy-conscious users may prefer avoiding change outputs entirely.
What Is a Coin Selection Strategy?
A coin selection strategy is the method a Bitcoin wallet uses to decide which unspent transaction outputs (UTXOs) to include as inputs when constructing a new transaction. Because Bitcoin uses the UTXO model rather than an account model, every payment must consume one or more discrete "coins" from the wallet's pool. The choice of which coins to spend determines the transaction's size, its fee cost, whether a change output is created, and how much information is leaked about the wallet's history.
Simple wallets might just grab the first UTXOs that cover the payment amount. Production-grade wallets like Bitcoin Core use sophisticated algorithms that weigh multiple factors: current fee rates, long-term fee expectations, change output costs, and the overall health of the UTXO set. The goal is to minimize the total economic cost of a transaction, not just the immediate fee.
How It Works
At a high level, coin selection follows these steps every time a wallet builds a transaction:
- Calculate the target amount: the payment value plus estimated fees for the transaction's base size
- Run one or more selection algorithms against the available UTXO pool
- Score each candidate input set using a cost metric (Bitcoin Core uses the waste metric)
- Select the candidate with the lowest score
- If a change output is needed, add it and recalculate fees
The critical insight is that different algorithms excel in different conditions. Bitcoin Core runs all of its strategies and picks the winner, rather than relying on any single approach.
Branch and Bound (BnB)
Branch and Bound searches for an exact-match input set: a combination of UTXOs whose total value equals the payment amount plus fees, with no change output needed. It performs a bounded depth-first search over the UTXO pool, capping the search at 100,000 iterations to prevent runaway computation.
BnB was introduced in Bitcoin Core 0.17.0 (2018), based on Murch's research thesis on coin selection optimization. Before BnB, the Knapsack algorithm almost never found exact matches. Simulations showed BnB could eliminate change outputs in up to 30% of transactions.
The key advantage: no change output means a smaller transaction (lower fees) and better privacy since there is no change address to link back to the sender. The limitation: an exact match may not exist for a given payment amount, so BnB frequently returns no result.
Knapsack
Knapsack was Bitcoin Core's original coin selection algorithm. It runs multiple random walks over the UTXO pool, selecting coins that push the total past the target. It keeps the combination that minimizes overshoot: the difference between the selected amount and the target plus the minimum change amount.
Knapsack almost always produces a change output, which adds weight to the transaction and creates an additional UTXO the wallet must manage later. Despite this downside, its randomized approach provides reasonable results across a wide range of UTXO pool compositions.
Single Random Draw (SRD)
Single Random Draw shuffles the available UTXOs randomly and selects them one by one until the target is met or exceeded. If the resulting transaction would exceed the maximum allowed weight, SRD drops the least valuable inputs (using weight as a tiebreaker) until the transaction fits.
SRD tends to consolidate UTXOs naturally: by drawing randomly, it frequently includes small UTXOs that other strategies might skip. This helps keep the wallet's UTXO pool healthy over time, especially during low-fee periods when the consolidation cost is minimal.
CoinGrinder
CoinGrinder was added in Bitcoin Core 27.0 (2024) to optimize for high-fee environments. It searches for the input set with the smallest total weight, minimizing the number of input bytes and therefore the fee. While BnB optimizes for zero change and Knapsack optimizes for minimal overshoot, CoinGrinder optimizes purely for transaction size.
During fee spikes, CoinGrinder often produces the winning result because using fewer, larger UTXOs keeps the transaction compact. It complements BnB: when an exact match is not possible, CoinGrinder finds the next cheapest option.
The Waste Metric
Bitcoin Core compares candidates from all four algorithms using a waste metric measured in satoshis. The formula accounts for three components:
- Timing cost: the difference between the current fee rate and a long-term fee rate estimate, multiplied by total input weight. When current fees are above the long-term average, spending inputs now "wastes" sats. When current fees are below it, spending now is a discount.
- Excess selection: the amount paid above the target that goes to the miner as extra fees (only applies to changeless transactions)
- Change cost: the fee to create and later spend a change output
waste = (currentFeeRate - longTermFeeRate) × totalInputWeight
+ excessAmount // if no change output
+ changeCost // if change output existsThis formula naturally adapts to market conditions. When fees are low (below the long-term estimate), the timing cost turns negative, favoring solutions with more inputs: the wallet consolidates UTXOs at a discount. When fees are high, the timing cost is positive, favoring minimal input sets to avoid overpaying. For a deeper look at how fee dynamics shape these decisions, see the Bitcoin fee market dynamics research article.
Privacy Implications
Coin selection has significant privacy consequences. Every input in a transaction reveals that those UTXOs belong to the same wallet, a heuristic known as common input ownership. The more inputs a transaction uses, the more of the wallet's history is exposed.
Change outputs also leak information: they create a new UTXO that chain analysis firms can link back to the sender. Strategies like BnB that eliminate change entirely offer a privacy advantage. Coin control takes this further by letting users manually select which UTXOs to spend, avoiding the mixing of coins from different sources.
For users who prioritize privacy, techniques like CoinJoin and PayJoin break the common input heuristic. These can be combined with careful coin selection to minimize information leakage across transactions.
Use Cases
Fee Optimization
The primary use case for advanced coin selection is reducing transaction fees. A wallet with hundreds of small UTXOs (common for Lightning node operators and mining pools) can see dramatically different fee costs depending on which coins it selects. During fee spikes, CoinGrinder or BnB can save 20-40% compared to naive selection by choosing fewer, larger inputs.
UTXO Consolidation
Wallets accumulate small UTXOs over time, especially those receiving frequent payments. During low-fee periods, the waste metric encourages SRD and Knapsack to include extra inputs, naturally consolidating the UTXO pool. This prevents stranded UTXOs that cost more to spend than they are worth: a problem known as dust. For strategies on managing this proactively, see the UTXO management strategies research article.
Exchange and Payment Processor Wallets
High-volume wallets on exchanges and payment processors process thousands of withdrawals daily. Efficient coin selection combined with transaction batching can reduce on-chain fees by orders of magnitude. These operations typically run custom coin selection tuned to their specific UTXO distribution and fee targets.
Layer 2 Channel Funding
Opening Lightning channels or funding other Layer 2 constructs requires on-chain transactions. The choice of inputs affects the funding transaction's fee and the resulting channel's on-chain footprint. Wallets like LDK-based implementations let developers plug in custom coin selection to optimize for their use case.
Why It Matters
Coin selection is one of the most impactful yet least visible parts of a Bitcoin wallet. Users rarely think about which UTXOs are being spent, but the algorithm's choices compound over time. Poor coin selection leads to a fragmented UTXO pool, higher future fees, dust accumulation, and reduced privacy. Good coin selection keeps fees low, maintains a healthy UTXO set, and minimizes the information footprint of each transaction.
For wallet developers building on Bitcoin Layer 2 systems like Spark, coin selection is equally relevant when constructing on-chain transactions for channel opens, closes, and splices. The coin selection algorithm comparison research article provides a detailed analysis of how each strategy performs across different fee environments and UTXO distributions.
Risks and Considerations
No Universal Best Strategy
Each algorithm makes tradeoffs. BnB is optimal when it finds an exact match, but it fails frequently. CoinGrinder minimizes fees but may create large change outputs. Knapsack provides consistent results but rarely finds the cheapest option. The multi-strategy approach mitigates this, but the waste metric itself embeds assumptions about future fee rates that may prove wrong.
Privacy vs. Fee Tradeoffs
Consolidating UTXOs during low-fee periods is economically rational but links multiple addresses together on-chain. Users who prioritize privacy may need to accept higher fees by limiting input counts or using coin control to manually select inputs from isolated sources.
Dust and Stranded UTXOs
If a wallet accumulates many small UTXOs and fees rise sharply, those UTXOs may become uneconomical to spend: their value is less than the fee required to include them as inputs. This is the stranded UTXO problem. Proactive UTXO consolidation during low-fee windows is the standard mitigation.
Implementation Complexity
Building correct coin selection is non-trivial. Edge cases include transactions that exceed the maximum weight, UTXOs with different script types (which affect input weight calculations), and interactions with replace-by-fee where the wallet must select coins for a replacement transaction that conflicts with the original. Libraries like Bitcoin Core's built-in coin selection and the standalone coin selection libraries handle these edge cases, but custom implementations require careful testing.
This glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.