How do we avoid ๐๐จ๐ฎ๐๐ฅ๐ ๐๐ก๐๐ซ๐ ๐ and ๐๐จ๐ฎ๐๐ฅ๐ ๐ฌ๐ฉ๐๐ง๐๐ข๐ง๐ when dealing with money?
.
.
.
.
๐น Double Charge in Paymentย Systems
A double charge is a common problem in payment systems. The client pays $100 to buy a product, but the money gets deducted twice from the digital wallet.ย
It is usually caused by retrying the money deduction operation on errors.
We can solve this by assigning an idempotency key to the transaction, so when the server sees the same idempotency key, it knows this transaction has been processed and won't process it again. In this way, the client won't be charged twice.
.
.
.
.
๐น Double Spending in Blockchain Transfers
Double spending is a potential problem where the crypto money is spent twice. The client only has 1 BTC in the wallet, but he can spend it twice to buy two products from different providers.
It is caused by the blockchain consensus mechanism.ย
.
.
.
๐ Steps 1โ2: When 1 BTC is paid to Alice (transaction A), another transaction that pays 1 BTC to Bob can be initiated simultaneously (transaction B). The two transactions go into different unconfirmed transaction pools, waiting to be picked up and confirmed by validators.
.
.
.
๐ Steps 3โ4: Validator A picks up transaction A, confirms it, and packages it in Block 2.1. Validator B picks up transaction B, confirms it, and packages it in Block 2.2.ย
Now the blockchain has a fork. At this point, both transactions are confirmed, causing double-spending.
We need to wait for subsequent blocks to be generated so that we can decide which fork is chosen to be the main blockchain. For example, if Block 3 is appended to Block 2.1 first, then transaction A is still confirmed, but transaction B cannot be executed because the client's wallet doesn't have crypto money after transaction A is confirmed.
Normally we need to wait for 6+ blocks to confirm a transaction for safety.
๐ Over to you: Have you met similar problems in distributed systems?
โ-
Top comments (0)