DatabaseIF
Core Concept

CAP Theorem

The fundamental law of distributed systems. You can't cheat physics.

Formulated by Eric Brewer, the CAP theorem states that any distributed data store can only provide two of the following three guarantees:

C

Consistency

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 an arbitrary number of messages being dropped by the network.

The "Choose Two" Myth

A common misunderstanding is that you get to choose any two. In a distributed system over a network, Network Partitions (P) are inevitable. Routers crash, cables get cut, GC pauses simulate dropped packets. Therefore, Partition Tolerance is not a choice; it's a requirement.

When a network partition occurs, you must choose between Consistency (cancel the operation/return error) and Availability (return stale data).

Database Classifications

  • CP (Consistent & Partition Tolerant): MongoDB, Redis, HBase. If they can't verify the state with the majority of nodes, they stop accepting writes.
  • AP (Available & Partition Tolerant): Cassandra, CouchDB. They will accept writes on any available node and resolve conflicts later (Eventual Consistency).
  • CA (Consistent & Available): Only possible on a single node (RDBMS like single-node Postgres), because network partitions can't happen if there is no network.