Published
- 11 min read
Database Deep-dive: CAP, ACID, BASE, and Beyond

Introduction
Databases are the foundation of modern applications, but what makes them truly reliable? The answer lies in a set of core principles that govern how they handle data. Instead of just memorizing terms, the best way to understand these principles is to ask a simple question: “What would break if this didn’t exist?”
This article follows a detailed conversation that does just that. We’ll start with the four pillars of transaction reliability—ACID—journey into the scalable, flexible world of BASE, explore how ACID systems adapt to a distributed world, and finally, unite everything under the fundamental law of distributed systems: the CAP Theorem.
Part 1: Deconstructing ACID - The Pillars of Reliability
The conversation begins with ACID (Atomicity, Consistency, Isolation, Durability), the set of properties that guarantee database transactions are processed reliably.
Q: What problem would we face if ‘Isolation’ didn’t exist?
A: Imagine an e-commerce site with only one popular item left in stock. Two customers, Alice and Bob, click “Buy” at almost the exact same time. Without Isolation, both of their transactions would read the stock count as “1” and proceed with the sale. They would both believe they bought the last item.
Isolation acts like a traffic cop. When Alice’s transaction starts, it places a “lock” on the stock data. Bob’s transaction, arriving milliseconds later, is forced to wait. Once Alice’s transaction successfully completes and updates the stock to “0,” the lock is released. Now, Bob’s transaction can proceed, but when it reads the stock, it sees “0” and the sale fails, correctly informing Bob that the item is sold out.
Isolation ensures that concurrent transactions don’t corrupt each other, making them appear as if they are running one after the other, alone in a room.
Q: It seems like Isolation helps maintain ‘Consistency’. How does that work?
A: That’s a great connection! Isolation is a key mechanism that helps enforce Consistency.
Consistency is like the set of laws for your database, defined in your schema. It ensures that any transaction will only take the database from one valid state to another. For example, a bank database might have a rule: “An account balance cannot be negative.”
If a customer with $300 tries to withdraw $500, the Consistency principle blocks this transaction because it would violate the “no negative balance” law. The database was in a valid state before the transaction, and it remains in a valid state after. Without Isolation, our e-commerce stock could have dropped to -1, which would be an inconsistent state.
Q: What kind of disaster would occur without ‘Atomicity’?
A: “Atom” comes from the Greek word for “indivisible,” which perfectly describes this principle: “all or nothing.” A transaction is a package of multiple steps. Atomicity guarantees that either all steps complete successfully, or if even one fails, all previous steps are rolled back.
Consider a bank transfer from Frank to George. There are two steps:
- Debit $100 from Frank’s account.
- Credit $100 to George’s account.
If Atomicity didn’t exist and the system crashed right after step 1, the money would be gone from Frank’s account but would never arrive in George’s. It would have vanished into thin air, corrupting the database and breaking its consistency. Atomicity prevents these “half-finished” states.
Q: And the final principle, ‘Durability’? What happens if we don’t have it?
A: Durability is simple: once a transaction is successfully completed (committed), it is permanent. The results are written to a non-volatile medium like a disk and will survive system crashes or power outages.
During a transaction, changes are often held in temporary memory (RAM). If the transaction commits successfully, these changes are written to disk. If the power goes out before the transaction commits, the data in RAM is lost. When the system restarts, it’s as if the transaction never happened because nothing was permanently changed on the disk.
Without Durability, you could make an online purchase, see the “Payment Successful” screen, but a server crash a minute later could erase any record of your order. Durability ensures that “what is written, stays written.”
Part 2: The Trade-off - ACID vs. BASE
Q: Are all databases ACID-compliant? If not, what are they for?
A: No, not all databases are ACID. Traditional relational (SQL) databases like PostgreSQL and MySQL are built on an ACID philosophy. However, the strict guarantees of ACID can become a performance bottleneck at a massive scale (think Google or Facebook).
This need gave rise to NoSQL databases, which often follow a different philosophy called BASE:
- Basically Available: The system is always available to respond.
- Soft State: The state of the system may change over time, even without new input.
- Eventual Consistency: The system will eventually become consistent once it stops receiving input.
While ACID prioritizes consistency (like a bank), BASE prioritizes availability and scale (like a social media feed). It’s acceptable for a “like” to appear a few seconds late, but it’s not acceptable for the feed to be down.
Q: I’ve used MongoDB, which is NoSQL, but it supports ACID transactions. Can’t I just build a massive e-commerce site using only MongoDB?
A: Absolutely, and many do! MongoDB is a great example of the lines blurring between worlds. It was designed for speed and flexibility (BASE) but has since added ACID transaction capabilities.
The key is to understand the trade-offs. MongoDB’s schema flexibility puts the responsibility of maintaining data consistency on you, the developer, within your application code. In a traditional SQL database, these rules are often enforced by the database itself.
When building a large site with MongoDB, you would think like an architect:
- Use ACID transactions for critical operations like payments, orders, and inventory.
- Use its classic, flexible, high-speed nature for less critical features like product catalogs, user comments, or session management.
The choice isn’t just about the technology (MongoDB vs. PostgreSQL), but about planning which features of that technology to use for different parts of your project.
Part 3: A Deep Dive into BASE (AP) Systems
Q: Let’s break down BASE. How does ‘Basically Available’ work? Does data get lost?
A: The “Tweet feed” problem is a perfect analogy. Making every tweet instantly visible to every follower worldwide is technically possible but prohibitively expensive and unnecessary.
”Basically Available” is achieved through replication. In BASE systems, sharding and replication are often default behaviors, not afterthoughts. Data is automatically distributed across all nodes in the cluster. When you request to read data, the system can serve you the answer from the nearest replica instantly, even if that replica hasn’t yet received the absolute latest write. You get a response immediately, but it might be slightly stale data.
Q: This “stale data” sounds like ‘Soft State’. How is it different from an in-flight transaction in an ACID system?
A: This is an incredibly sharp question.
- ACID (Hard State): When you read from an ACID database during a transaction, you are seeing the last guaranteed consistent version of the data. The system promises you: “What you are seeing is 100% correct and committed.” The state is “hard.”
- BASE (Soft State): When you read from a BASE system, there is no single guaranteed consistent version across all replicas. The system is in constant flux as data replicates. It promises you: “Here is some data. It might not be the latest version, but the system is working on updating itself.” The state is “soft” or “in-flux.”
Essentially, ACID shows you a perfect photograph of the past, while BASE shows you a blurry preview of the future.
Q: So the system is always in flux. This must lead to ‘Eventual Consistency’ and conflicts. How are they resolved?
A: Exactly. The end goal is that, eventually, all replicas will have the same data. This leads to the need for conflict resolution.
- Leader Election: If a primary node fails, a new leader is elected. “In-flight” requests to the old leader can be lost—a core trade-off for availability.
- Last Write Wins (LWW): The most common strategy. When a conflict occurs, the system accepts the update with the newest timestamp.
- Vector Clocks: A more advanced mechanism. Instead of just when an event happened, it tracks its “causal history.” Its job is to detect conflicts with certainty. Once a conflict is detected, an arbitrary but deterministic rule (like LWW or “server with higher ID wins”) is used to resolve it. This can mean losing an update, but it’s a predictable loss that all nodes agree on.
- CRDTs (Conflict-free Replicated Data Types): An even more advanced approach where data structures are mathematically designed to merge without ever creating a conflict.
Part 4: Scaling the Unscalable - Distributed ACID (CP) Systems
Q: So ACID is great for consistency, but how does it scale when a single server isn’t enough?
A: When a single relational database hits its limits, we need to distribute it. This presents two main problems:
- Read Scalability: Too many users are reading data. The solution is Read Replicas. A primary “master” database handles all writes, and its data is copied to multiple “replica” databases that handle the read requests, spreading the load.
- Write Scalability & Data Size: The volume of writes is too high, or the data is simply too large to fit on one machine. The solution is Sharding. The database is horizontally partitioned, with different sets of rows stored on different servers (shards). For example, users A-M are on Server 1, and users N-Z are on Server 2.
Q: How do you maintain ACID guarantees, like Atomicity, across these different servers?
A: This is the hardest part. If a bank transfer involves an account on Server A and another on Server B, you can’t just lose data if one server fails. The classic solution is the Two-Phase Commit (2PC) protocol.
- Phase 1 (Prepare): A central “coordinator” asks all participating servers (shards) if they are ready to commit the transaction. Each server prepares the change and, if successful, votes “YES.”
- Phase 2 (Commit): If the coordinator receives “YES” from all participants, it sends a final “COMMIT” command. If even one participant votes “NO” or fails to respond, it sends an “ABORT” command, and everyone rolls back.
This ensures the transaction is atomic (all or nothing), but 2PC can be slow and can get stuck if the coordinator fails.
Q: 2PC sounds fragile. Is there a more reliable way to resolve conflicts and agree on transactions?
A: Yes. Modern distributed SQL databases (like Google Spanner or CockroachDB) use Consensus Algorithms like Paxos or Raft. The core idea is democratic: for a write to be considered committed, a majority of servers (a “quorum”) must agree on it and log it. If a leader fails, the remaining servers can elect a new one and continue operating. This is far more fault-tolerant than 2PC and prevents “split-brain” scenarios where different parts of the network accept conflicting writes.
Part 5: The Grand Unifier - The CAP Theorem
Q: We’ve talked about ACID and BASE. Is there a fundamental law that governs them all?
A: Yes, there is: the CAP Theorem. It’s the constitution for all distributed systems. It states that any distributed data store can only provide two of the following three guarantees simultaneously:
- C - Consistency (Strong): Every read receives the most recent write or an error. All nodes see the same data at the same time.
- A - Availability: Every request receives a non-error response, without the guarantee that it contains the most recent write.
- P - Partition Tolerance: The system continues to operate despite network failures between nodes.
In the real world, network partitions (P) are a fact of life, so they cannot be sacrificed. Therefore, the real choice every distributed system must make is between Consistency and Availability.
- CP (Consistency + Partition Tolerance): When a network partition occurs, the system chooses to preserve consistency. This may mean refusing requests or shutting down the minority part of the system to prevent serving stale data. ACID-focused distributed systems (NewSQL) like CockroachDB fall here.
- AP (Availability + Partition Tolerance): When a partition occurs, the system chooses to remain available. This means it will continue to serve requests, even if that means serving stale data from a non-updated partition. BASE systems like Cassandra and DynamoDB fall here.
Conclusion
This journey reveals there is no single “best” database. The choice is a series of trade-offs guided by fundamental principles. Do you need the bulletproof guarantees of an ACID (CP) system for your financial transactions, even if it means sacrificing some availability during a failure? Or do you need the massive scale and constant uptime of a BASE (AP) system for your social media feed, accepting that data will only be eventually consistent?
By understanding not just what these principles are, but why they exist and what problems they solve, you can move from simply using a database to architecting a truly robust and reliable data solution.