ACID, CAP, and BASE: Unveiling Database Consistency

Omar Mahmoud
5 min readSep 6, 2023

--

ACID, CAP, and BASE: Unveiling Database Consistency

Welcome to my first article on Medium!

If you’re probably interested in learning about the ins and outs of ACID, BASE, CAP, and other important concepts in databases, you are in the right place.

Among the myriad discussions and publications on these subjects, ‘Designing Data-Intensive Applications’ encapsulates the essence concisely and, in my opinion, with remarkable impact.

A colleague of mine pointed out the other day that there never was an ACID Vs CAP battle. Nor an ACID Vs BASE or CAP Vs BASE. We cannot find any better way to state it. We will look into these concepts, in a concise manner, to assess which one is applicable to ourselves.

You might be wondering, “Why is this knowledge important?” Allow me to explain — these concepts form the foundational framework of database operations. Grasping ACID, BASE, and CAP equips you with profound insights into how databases manage transactions, navigate failures, and uphold data integrity. Believe me, this expertise will prove invaluable as you construct or engage with databases down the line!

Here the point of this article is: what is the most convenient for my project not what is the best database type?

The Abbreviations

ACID stands for Atomicity, Consistency, Isolation, and Durability. BASE stands for Basically Available, Soft-state, and Eventually consistent. Last but not least, the CAP theorem tells us about the balance between Consistency, Availability, and Partition Tolerance.

ACID

ACID from Transactional Database view

Transferring $100 from account Omar to Mohamed Account, at a high level, involves two steps -

  1. Debit $100 from account Omar
  2. Credit $100 to account Mohamed

However, a multitude of complications can arise and errors during these actions. For instance:

  • The application server might crash right after step 1.
  • The database could encounter a failure while executing step 2.
  • Two individuals, A and B, might independently initiate the transfer simultaneously, leading to potential conflicts and errors, etc.

So, Transactions must follow four basic properties to achieve it — Atomicity, Consistency, Isolation, and Durability.

Atomicity:

When a set of operations is treated as an atomic transaction, success depends on all or nothing. Consider the $100 transfer from account Omar to Mohamed — if treated as a transaction, both steps 1 and 2 either succeed or fail together. No partial success is allowed.

Consistency:

In our case, after the transaction, we should see that our values make the target the main balance for example if Omar credit 30 to Mohamed after that operation we should see that the total money of Omar and Mohamed equals 100. The Consistency property ensures the perpetual validity of these facts. Relational databases maintain this through methods like unique keys, foreign key constraints, and triggers.

Isolation:

Isolation dictates that an ongoing transaction is invisible to and unaffected by other concurrent transactions. This isolation is crucial to restoring a transaction’s state to its original point. Relational databases offer various isolation levels to adapt to specific requirements — like read uncommitted, repeatable read, and more.

Durability:

Once committed, a transaction’s updates should endure. Database systems employ techniques like write-ahead logs, hard disks, and backups to uphold this principle.

All’s well until we introduce multiple machines into the equation, forming a system. Adhering strictly to ACID principles becomes intricate. This is where we need additional sets of rules and theories for distributed systems — introducing CAP and BASE.

CAP

CAP Theorem from The official website of MongoDB

Within a distributed environment, it’s expected that network partitions may occur among the nodes. The CAP theorem establishes that when facing a network partition, the system must choose between being Available or Consistent.

Now that we have a basic understanding of the CAP theorem, let’s break down the acronym and discuss the meanings of consistency, availability, and partition tolerance.

Consistency:

In a consistent system, all nodes see the same data simultaneously. If we perform a read operation on a consistent system, it should return the value of the most recent write operation. The read should cause all nodes to return the same data. All users see the same data at the same time, regardless of the node they connect to. When data is written to a single node, it is then replicated across the other nodes in the system.

Availability:

All the active nodes at any moment must be able to respond to different operations. it means that the system remains operational all of the time. Every request will get a response regardless of the individual state of the nodes.

Partition tolerance:

The system must be able to tolerate network partition among its participant nodes. It means that there’s a break in communication between nodes. If a system is partition-tolerant, the system does not fail, regardless of whether messages are dropped or delayed between nodes within the system. To have partition tolerance, the system must replicate records across combinations of nodes and networks.

Depending on your project what will you use?

BASE

Basically-Available, Soft-state, Eventually-consistent

Basically-Available:

A Database must offer availability by providing a response, whether it’s an acknowledgment or even a failure message, to every incoming request, The database may experience brief periods of unavailability, but it should be designed to minimize downtime and provide quick recovery from failures.

Soft-state:

The database system may keep changing states as and when it receives new information, This can happen due to the effects of background processes, updates to data, and other factors. The database should be designed to handle this change gracefully and ensure that it does not lead to data corruption or loss.

Eventually-consistent:

The elements within the system might not instantaneously display an identical value or state for a record at any given instance. However, they will gradually reconcile this disparity over time. This concept pertains to the eventual consistency of data within the database, even in the presence of evolving changes. Essentially, the database is anticipated to ultimately reach a harmonized and consistent state, even if the propagation and reflection of all updates demand a certain duration. This stands in opposition to the instantaneous consistency demanded by conventional ACID-compliant databases.

Conclusion

In summary, ACID emphasizes strong consistency and reliability, CAP underscores the need to balance consistency, availability, and partition tolerance in distributed systems, while BASE offers a more flexible approach prioritizing availability and eventual consistency. The choice among these approaches depends on the application’s requirements and technical context, guiding the design of effective and resilient database systems.

If you reached here Thanks for Reading :)

--

--