As the name implies, the double spending problem consists in executing a transaction twice while subtracting just once from your account balance.
This, of course, can only happen by means of fraud or a badly broken payment system.
Traditional payment systems, such as debit and credit cards, deploy many safeguards in order to avoid double spending. Not only are plastic money databases online 24x7, with guaranteed integrity, they also employ strong protections against similar transactions being performed too closely together.
For example, when traditional debit and credit cards are used during weekends or holidays, the transactions are batched to be committed on the next business cycle.
These batched transactions all count against your available credit or debit balance immediately - which avoids the double spending problem. Should the purchases made during off hours get lost before the batch is committed, then you would be able to double-spend.
Traditional payment systems' protection is made available thanks to databases which carry the ACID attribute.
ACID stands for Atomicity, Consistency, Isolation and Durability and most professional relational databases are ACID compliant.
Atomicity means that transactions either complete successfully or fail completely. There must be no in-between state for an Acid transaction. In case of a point of sales transaction, this property 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 against double spending.
So, how do cryptocurrencies achieve double-spending protection if they don't use traditional databases?
Let's use the Bitcoin system as an example to show how blockchain is also able to implement the ACID properties!
Bitcoin is atomic.
BTC's embedded scripting language guarantees that the very last result pushed onto a transaction stack must be TRUE. Otherwise the transaction will fail with no intermediate step possible. There is no way for a failed script to be committed onto the blockchain. Miners and full verifying nodes would reject an entire block containing a transaction with an invalid script.
Although scientists don't all agree on whether Bitcoin is consistent or an eventually consistent system, for us mere mortals it can be considered as a consistent ledger.
The only question remains about whether Bitcoin is eventually or strongly consistent - or represents some other consistency model. These are academic concepts and not truly relevant to our practical purposes.
All we care about is that the state of your Bitcoin balance must be correct before and after transactions, guaranteeing the correctness of the block chain.
Practical consistency has been proven by Bitcoin's 12 years of nearly 100% uptime.
Let's 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 as a test or as part of a fraudulent operation.
Miners will receive at least one of your transactions and, if that TX has a valid script, it will be chosen to be included in some future 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 several attempts you broadcast earlier 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. It's no big deal, because this happens all the time!
The consensus algorithm will choose one of the blocks containing that particular TX and the others will be discarded.
The same inputs to the same transaction will not be accepted on the same blockchain, ever.
The Bitcoin blockchain is indestructible and impossible to corrupt. This is proven by the science behind Bitcoin and also through its decade-plus of flawless operation.
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 local storage. Destroying the Bitcoin blockchain would require a massive unprecedented attack on 51% of the copies - impossible!
By guaranteeing the ACID properties, Bitcoin is safe against double spending.
How, exactly, the Bitcoin protocol 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 Proof of Work consensus mechanism.
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.