Skip to content
Home » What is CAP theorem in the context of databases, and how does it apply to NoSQL databases?

What is CAP theorem in the context of databases, and how does it apply to NoSQL databases?

The CAP theorem, also known as Brewer’s theorem, is a concept in distributed database systems that states it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees:

  1. Consistency: Every read receives the most recent write or an error.
  2. Availability: Every request receives a response, without the guarantee that it contains the most recent write.
  3. Partition Tolerance: The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.

In other words, a distributed system can be consistent and available under the absence of network partitions, but under partition, it has to choose between consistency and availability.

The CAP theorem is fundamental to understanding the trade-offs that need to be made when designing and working with distributed systems. Each point of the CAP theorem directly corresponds to a desirable property in a database system:

  • Consistency ensures that a read will always return the most recent write, maintaining data integrity.
  • Availability ensures that the database is always ready to receive a read or write request, maximizing uptime.
  • Partition tolerance ensures that the system continues to function in the face of network partitions, enhancing reliability.

Now, when it comes to NoSQL databases, they are often distributed systems, so the CAP theorem applies directly to them. The variety of NoSQL databases (like key-value stores, document databases, wide-column stores, and graph databases) make different trade-offs with respect to the CAP theorem. Here are a few examples:

  • Cassandra: It is typically classified as an AP system (Availability and Partition-tolerance) because it prioritizes availability over consistency in the event of a network partition.
  • MongoDB: In its default configuration, MongoDB is a CP system (Consistency and Partition-tolerance) because it will halt operations until a majority of members are available again, thereby prioritizing consistency over availability.
  • Riak: It is an AP system (Availability and Partition-tolerance). Riak prioritizes high availability and partition tolerance over strict consistency.
  • CouchDB: CouchDB is generally considered an AP system (Availability and Partition-tolerance). It aims to provide a reliable system that can survive network partitions and continue to provide service, albeit with potentially outdated data. It uses a replication model that, once the partition is resolved, helps reconcile changes made to the data.
  • HBase: HBase is typically classified as a CP system (Consistency and Partition-tolerance). It blocks all writes to a region during a network partition, sacrificing availability to ensure consistency. HBase is a good choice when strong consistency is required for your data.
  • DynamoDB (Amazon’s NoSQL Database): DynamoDB provides tunable consistency models. By default, it supports eventual consistency but it can be configured to support strong consistency as well. Thus, depending on the specific configuration and nature of a potential network partition, it can act as either AP or CP.
  • Redis: Redis is a bit unique. It’s primarily used as an in-memory data structure store and supports various kinds of abstract data structures. The standalone Redis server is essentially CA (Consistency and Availability). However, in distributed mode using Redis Cluster, it can behave like a CP system, preferring consistency over availability.

Keep in mind that these designations are not absolute. The design of many NoSQL systems allows for a certain degree of flexibility, and they can often be tuned to behave more like an AP or CP system based on specific application requirements. For example, some database systems allow developers to fine-tune the balance between consistency and availability on a per-operation basis.