DEV Community

Santhosh V
Santhosh V

Posted on

CA 37 - Durability

I built a simple wallet system like GPay / PhonePe.
Users can:
Store money
Send money
View transactions

Here I focused on Durability

Because once money is sent, it should never be lost.

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
INSERT INTO accounts (name, balance)
VALUES
('Alice', 1000),
('Bob', 500);
Enter fullscreen mode Exit fullscreen mode

Transfer Money

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

After Restart

I restarted the DB and checked again:

SELECT * FROM accounts;
Enter fullscreen mode Exit fullscreen mode

After COMMIT, data is saved permanently Even if system crashes, data is not lost

If crash happens:

Before commit the changes gone After commit the changes safe

Top comments (0)