When I build a wallet system like PhonePe or GPay, I make sure the data is always correct. This is called Consistency in ACID.
What Consistency Means
Consistency means:
Data should always follow rules
No invalid data should be stored
My Table
CREATE TABLE accounts (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
balance INT NOT NULL CHECK (balance >= 0),
last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Here, I added:
CHECK (balance >= 0) → balance should never be negative
Dummy Data:
INSERT INTO accounts (name, balance)
VALUES
('Alice', 1000),
('Bob', 500);
Testing the Rules
Try to make balance negative
UPDATE accounts
SET balance = -100
WHERE name = 'Alice';
This will fail
Reason: database rule (CHECK constraint)
Try to deduct more money
UPDATE accounts
SET balance = balance - 2000
WHERE name = 'Alice';
This will also fail
Because balance becomes negative
What I Understand
Database itself blocks wrong data
It keeps balance valid
This is Consistency
Simple Idea
Database ensures rules
Data always stays correct
Conclusion
Consistency makes sure:
No negative balance
No wrong updates
So I remember it like this:
Data should always be valid before and after any operation.
Top comments (0)