DEV Community

Anjana R.K.
Anjana R.K.

Posted on

Idempotency Situation

Hi!
To Simulate a situation where the same transfer operation is executed more than once, as might happen in a real system due to network retries or duplicate requests.

CREATE TABLE accounts (id SERIAL PRIMARY KEY,name TEXT NOT NULL,balance INT NOT NULL CHECK (balance >= 0),last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
Enter fullscreen mode Exit fullscreen mode

inserting values to the table accounts,

INSERT INTO accounts (name, balance) VALUES ('Alice', 1000),('Bob', 500);
Enter fullscreen mode Exit fullscreen mode

to observe whether the accpunt balance is affected the deduction and credit operations performed multiple times

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE name = 'Alice';
UPDATE accounts SET balance = balance + 100 WHERE name = 'Bob';
COMMIT;
Enter fullscreen mode Exit fullscreen mode
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE name = 'Alice';
UPDATE accounts SET balance = balance + 100 WHERE name = 'Bob';
COMMIT;
Enter fullscreen mode Exit fullscreen mode

to analyze whether the system prevents duplicate processing or allows the same transaction to be applied repeatedly
*it does not automatically prevent duplicate processing of the same transaction.

Top comments (0)