Let me explain duplicate transaction First we have accounts table
Alice = 1000
Bob = 500
now we try same transfer again and again.
First transfer:
After first transfer:
Alice = 800
Bob = 700
200 is transferred successfully
let's run the transfer again
Again, the same operation is performed, and money is transferred once more
Since the database is not aware that it is a duplicate request, it will execute it over and over again, and hence duplicate transactions are allowed
The problem is, the request is executed multiple times, and hence money is deducted multiple times, which is not correct
Since, at the end of the day, the database is only ensuring that the request is executed correctly. It does not ensure that the request is not executed twice. Hence, the problem has to be addressed at the application level
In most systems, it is addressed by providing a unique ID for the transaction and checking whether the transaction has already been processed or not. Hence, even if the request is executed twice, it will not be processed twice. This way, duplicate transactions are not allowed, and the system remains consistent. This is how most payment systems handle duplicate transactions.


Top comments (0)