SYS ARCHITECTLearning Platform
Settings
Theory

Consistency vs. Integrity

๐Ÿ“‹ Overview

In the context of databases and distributed systems, "Consistency" and "Integrity" are often used interchangeably, but they represent two distinct facets of data correctness. Integrity refers to the rules (constraints) that data must follow to be considered valid. Consistency is the guarantee provided by the system (often via ACID transactions) that these rules will never be violated, ensuring the system transitions from one valid state to another.


๐Ÿ—๏ธ Core Principles & Characteristics

  • Integrity (The Rules):
    • Entity Integrity: Primary keys must be unique and non-null.
    • Referential Integrity: Foreign keys must point to existing records.
    • Domain Integrity: Values must fall within valid ranges (e.g., age > 0).
    • Business Integrity: Custom logic (e.g., total_amount = sum(line_items)).
  • Consistency (The Enforcement):
    • ACID "C": A transaction will only commit if all integrity constraints are met.
    • CAP Theorem "C": Every read receives the most recent write or an error (Linearizability).
    • Eventual Consistency: A weaker guarantee where data will converge over time but may be temporarily "inconsistent."

โš–๏ธ Trade-offs: Pros & Cons

  • High Integrity/Strong Consistency:
    • Pros: Simplifies application logic; data is always "correct" and trustworthy.
    • Cons: Significant performance penalty; reduces availability (CAP) and increases latency due to locking/coordinated commits.
  • Low Integrity/Weak Consistency:
    • Pros: Extremely high throughput and availability; better user experience for global-scale apps.
    • Cons: Application must handle "dirty data," duplicates, and temporary contradictions.

๐ŸŒ Real-World Implementation

  • Banking Systems: High Integrity/Strong Consistency. You cannot have a negative balance if the rule says so; transactions must be atomic.
  • Social Media Likes: Eventual Consistency. It doesn't matter if two users see 100 vs. 101 likes for a few seconds. Integrity (count is a number) is maintained, but consistency (same value everywhere) is delayed.
  • E-commerce Checkout: Strong consistency for inventory (Integrity: stock >= 0), but eventual consistency for "Recently Viewed" items.

๐Ÿ’ก Interview "Gotchas" & Tips

  • The "C" in ACID vs CAP: In ACID, Consistency means "Integrity enforcement." In CAP, Consistency means "All nodes see the same data at the same time." These are different!
  • Integrity at App Level vs DB Level: Integrity is best enforced at the DB level (Constraints) for safety, but app-level checks are needed for better UX (preventing errors before submission).
  • Write-Ahead Logs (WAL): These are the primary mechanism for ensuring consistency after a crashโ€”replaying valid transactions to restore integrity.

๐Ÿ“ Suggested Architecture Primitives

  • Database Constraints: NOT NULL, UNIQUE, FOREIGN KEY, CHECK.
  • ACID Transactions: BEGIN...COMMIT/ROLLBACK.
  • Validation Layer: Middleware/Validators in the API layer.
  • Audit Logs: To verify integrity after the fact in eventually consistent systems.
Canvas