What is Double Spending
There are two ways to complete any transaction, either online or offline. When you make use of physical currency say coins or paper notes, it is called offline transaction or physical transaction. And any transaction that takes place online is a digital or online transaction.
Assume a person orders coffee for Rs. 100 from a restaurant, takes coffee pays the cash and comes back home. Now, is it possible for that person to use the same Rs 100 for some other purpose? Clearly, the answer is no. But what if the answer is yes?
When a person is able to use the same currency for more than one transaction, it is called a double-spend problem. It is not possible in a physical transaction or cash but is possible and occurs in digital currencies like Bitcoin.
How Bitcoin Handles Double-Spending Problem
Since Bitcoin is basically a digital file it’s easier to duplicate than actual money which means some people can manipulate their way to paying more than once with the same Bitcoin, this is what is known as double spending, how can this be avoided?
Let’s say I have only one Bitcoin and I decide to send it to you this transaction, let’s call it transaction A and it goes into the unconfirmed transactions pool and is waiting to be confirmed. At the same time I send one Bitcoin to Bob this transaction, let’s call it transaction B also goes into the unconfirmed transactions pool, so I sent out one Bitcoin twice once to you and once to Bob.
When the transactions are taken out of the unconfirmed transactions pool and put into the blockchain, they are checked for validity, so let’s say transaction A is pulled out of the pool it seems to be valid since I have one Bitcoin and it is inserted into the blockchain. Now, transaction B is pulled out of the pool it is invalid since I don’t have any more bitcoins to spend and it does not get confirmed.
But what if the validation process of A and B happen simultaneously meaning both transactions will show that I have the money needed well, in this case, we will have two branches of the blockchain and a race will begin, the first one to achieve the next block of confirmations will win.
If they reach the next block simultaneously as well we will have another race and so on. This is why it’s recommended to wait until six confirmations before considering a transaction complete. Since it’s highly unlikely that this race will happen more than six times so, in the end, we will have a clear winner and only one transaction will be confirmed.
Until this race is resolved, both transactions are at risk of getting cancelled.
Source: https://www.thecoinrepublic.com/2023/05/01/double-spending-problem-and-how-it-is-solved-by-bitcoin/