DEV Community

Luckshvadhan B
Luckshvadhan B

Posted on

Idempotency Situation

The problem is
Same transaction may be executed multiple times
This can happen due to network retry or duplicate request
This may lead to wrong balances

Initial Data
Alice has 1000
Bob has 500

Normal Transfer
Transfer 200 from Alice to Bob

After transaction
Alice becomes 800
Bob becomes 700

Duplicate Execution
Same transfer runs again

After second execution
Alice becomes 600
Bob becomes 900

Transaction is applied again

Database allows repeated execution
It does not know whether request is duplicate
Each execution is treated as a new operation

Because database only ensures consistency per transaction
It does not track duplicate requests
There is no built in mechanism to stop same transfer again

Database ensures No partial updates and Valid data state

Use unique transaction id for each request
Check if transaction already exists before processing
Reject duplicate requests

Database ensures correctness of each transaction
Application ensures transaction is not repeated

Top comments (0)