Understand the Double Spending problem

As the name implies, the double spending problem consists in executing a transaction twice while subtracting only once from your balance. This, of course, represents a failed financial system, since double spending can be repeated N times and lead to complete monetary collapse. Traditional debit and credit cards work with many safeguards in order to avoid double spending. Not only are traditional plastic money databases online 24×7, they also have in-place protections against transactions of the same amount being performed too closely together. When traditional debit and credit cards are used during weekends and holidays, the transactions are batched to be committed on the next business day, but these batched transactions all count against your available credit or debit balance instantly.

This traditional financial instrument protection is available thanks to databases with the ACID attribute. ACID stands for Atomicity, Consistency, Isolation and Durability and most professional relational databases possess some level of ACID compliance.

Atomicity means that transactions either complete successfully or fail completely. There must be no in-between state for an Acid transaction. This guarantees that a purchase will either go well and your checking account will be debited, or it won’t happen at all. There is no way for a debit card transaction to go half way and make your balance unknown. Thus, credit and debit card transactions either fail and show a message on the point of sale machine, or they go through and print a receipt.

Consistency means that the state of your account will be valid after a transaction happens. This guarantees that transactions can happen one after another correctly, always guaranteeing the correct balance and debited amount on your account.

Isolation means if you have many transactions going through at once, all of them must leave accounts valid as if they had happened one after the other. In essence, one transaction must be isolated or protected from other system activity so that once it’s done, it can be guaranteed that your system will be in the right state.

Durability means the results of transactions must last for as long as the system is in production mode. Therefore databases must be guarded against data loss, corruption or other hazards.

As you can see, a lot goes into protecting a typical financial transaction using credit or debit cards against double spending. How do cryptocurrencies achieve double spending protection? Through a similar concept!

Bitcoin is atomic. Its scripting language guarantees that the very last result pushed onto a transaction stack must be TRUE. Otherwise it fails. There is no way for a failed script to be committed onto the blockchain. Miners would refuse it and full nodes would reject the transaction.

Although scientists don’t all agree on whether Bitcoin is consistent or eventually consistent, for us mere mortals it can be considered a consistent ledger. Which means Bitcoin is consistent. The only question remains about whether Bitcoin is eventually or strongly consistent or represents some other consistency model, which are academic concepts and not relevant to our purposes on this article. All we care about is that the state of your Bitcoin balance must be correct before and after transactions, guaranteeing the correctness of the chain of transactions. Consistency has been proven by Bitcoin’s over 9 years of operation.

Bitcoin transactions are isolated. Assume you just broadcast 10 identical transactions onto the Bitcoin P2P network at the exact same time. You’re actively trying to double spend your BTC. Miners will receive at least one of your transactions and if it has a valid script, it will be chosen to be included in the next block. Once included into the next block, that block now contains a transaction with the inputs that generated your BTC balance. When another transaction arrives with the exact same inputs, it will be rejected from that block even before it is mined. The other 8 attempts will also fail because transactions are chosen for the block in a serial manner. That is, mining pools and large scale solo miners are thread-safe, they receive a large backlog of transactions but process one by one when building the next block. Now assume that two competing mining pools each chose a different identical transaction you sent. This happens all the time! The consensus algorithm will choose one of the blocks and the other will be discarded  – otherwise a fork would happen immediately. The same inputs to the same transaction will not be accepted on the same blockchain, ever.

Bitcoin is durable. The blockchain is indestructible and impossible to corrupt. Should the blockchain lose data for some reason, the entire Bitcoin system would collapse instantly. In fact, the blockchain is probably the world’s most replicated database. Every single full node has a copy of the complete blockchain on its local disk. Destroying the Bitcoin blockchain would require a massive unprecedented attack.

By guaranteeing the ACID properties, Bitcoin is safe against double spending. How, exactly, Bitcoin guarantees these properties, in technical terms, is beyond the scope of this article. In summary, the solution to consistency, isolation and atomicity all lie in the mining process. The consensus mechanism by which Bitcoin guarantees that outputs are only ever used on a single transaction on its database has been tested for over 9 years and it has withstood the test of time. In practice what a Bitcoin user needs to know is that Bitcoin guarantees that the financial balance on any Bitcoin address will be consistent after a certain number of confirmations. Some payment processors require 3 confirmations (~30 minutes), others require a more strict number of 6 confirmations (~ 1 hour). The more blocks have been mined after your transaction, the more it becomes carved in stone (this is part of the reason why some say Bitcoin is eventually consistent, but like we said scientists disagree with this logic).

There are various theoretical views of Bitcoin’s database model, be it ACID, SALT or BASE, the fact is we’re left with a ledger database in which money cannot be double spent due to Bitcoin’s amazing consensus reaching mining algorithm.

We hope you’ve found this article useful and that you were able to gain a better understanding of the double spending problem and how Bitcoin and traditional databases avoid it.