Consistency (ACID)
Database constraints are enforced
“Consistency ensures that a transaction can only bring the database from one valid state to another, maintaining database invariants.”
When to use
Relational databases.
Why it matters
- Valid Data: Ensures rules like “Foreign Keys must exist” or “Age cannot be negative” are never broken.
Signs of Violation
- A row referring to a
user_idthat doesn’t exist. - A
balancecolumn containing a string.
Explanation
Problem
Garbage in, garbage out. If the DB allows invalid data, the application code has to double-check everything constantly.
Solution
Schema Constraints (Foreign Keys, Unique Keys, Check Constraints, Triggers).
Real world analogy
Law of Physics. You cannot walk through a wall. The universe (Database) enforces this rule (Consistency). You don’t need a policeman to tell you; it just physically fails to happen.
Pros and Cons
| Pros | Cons |
|---|---|
Comparison
- CAP Consistency: CAP Consistency is about specialized nodes seeing same data (Synchronization). ACID Consistency is about data rules (Validity).
Code example
Typescript
Bad (Violation)
Good (Adherence)
PHP
Good (Adherence)