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_id that doesn’t exist.
  • A balance column 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
  • Guaranteed data quality
  • Performance cost on writes (checking constraints)
  • 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)