Back to articles
Idempotency Situation

Idempotency Situation

via Dev.to BeginnersARUL SELVI ML

Today I tried to understand what happens when the same transaction is executed multiple times in a database. This can happen in real applications like PhonePe or GPay when there are network issues and the same request is sent again. I used the same accounts table where Alice has 1000 and Bob has 500. First, I performed a normal transfer of 200 from Alice to Bob. BEGIN; UPDATE accounts SET balance = balance - 200 WHERE name = 'Alice'; UPDATE accounts SET balance = balance + 200 WHERE name = 'Bob'; COMMIT; After this, Alice had 800 and Bob had 700. Everything was correct. Then I repeated the same transaction again, as if the same request was sent twice. BEGIN; UPDATE accounts SET balance = balance - 200 WHERE name = 'Alice'; UPDATE accounts SET balance = balance + 200 WHERE name = 'Bob'; COMMIT; Now the balances became Alice 600 and Bob 900. This means the same transaction was applied again. The database did not stop it. From this, I understood that PostgreSQL does not automatically prev

Continue reading on Dev.to Beginners

Opens in a new tab

Read Full Article
6 views

Related Articles