DEV Community

Sharmila devi
Sharmila devi

Posted on

Idempotency Situation

When I build a wallet system, I realized one common issue — same request can happen multiple times. This usually happens because of network retry or user clicking twice.

This is where Idempotency becomes important.
My Situation
Let’s say:
Alice → ₹1000
Bob → ₹500
I send ₹200 from Alice to Bob.

First Transfer
UPDATE accounts SET balance = balance - 200 WHERE name = 'Alice';
UPDATE accounts SET balance = balance + 200 WHERE name = 'Bob';

Now:
Alice → ₹800
Bob → ₹700
Same Request Happens Again

Due to retry, the same query runs again:
UPDATE accounts SET balance = balance - 200 WHERE name = 'Alice';
UPDATE accounts SET balance = balance + 200 WHERE name = 'Bob';

Now:
Alice → ₹600
Bob → ₹900

Money transferred twice
What I Observed
Database allows the same operation again
No automatic prevention
Duplicate transaction happens
Problem

Same request = executed multiple times
Balance becomes incorrect

How Real Systems Solve This
In real systems, I use an Idempotency Key.
Example:
txn_001
Before processing, I check:
“Is this transaction already done?”

Simple Logic
IF transaction_id exists
DO NOTHING
ELSE
PROCESS transaction
Final Result
First request → success
Duplicate request → ignored

No double deduction
Balance stays correct

Simple Idea
Same request should not change result again

Conclusion
Without idempotency:
Duplicate transactions happen
Money gets deducted multiple times

With idempotency:
Only one transaction is processed
System stays safe

So I remember it like this:
Even if request repeats, result should happen only once.

Top comments (0)