CAP theorem

Its a theorem by Eric Brewer which states that for any distributed system of the following three choose any two:

  1. Consistency (Not the C in ACID)
  2. Availability
  3. Partition Tolerance

Terminology

Lets got through each of these and try to understand the actual meaning of this theorem.

Consistency – Every request to the service must return the most recent value

We can understand this by thinking of a banking system where we strongly desire consistency. If you do a transaction from A to B and the they live on polar opposite sides of Earth. Then a consistent banking system after a successful transaction will show the new balance to both A and B.

Availability – Every request to the service must get a response.

Notice how we didn’t say the correct response above. As long as your service is “available” i.e., responding to requests your system is considered available.

Going back to our banking system, an available banking would always give a response irrespective of the state of system. So if there was an outage where B lives then an available banking system would still show the balance but it would be the old data. A consistent system in this case would not even serve the request.

Partition Tolerance – The system can function despite any network failure (like split brain where we get two sets of servers not able to communicate with each other)

Partition is a truth of nature. If there is a distributed system which by its very name means is distributed across regions – therefore connected by network. The network is faulty, it can drop messages because some router is faulty or overworked or power failure or any other disaster.

Reduction

Lets round back up to CAP theorem now. On first glance it seems like we can have three types of systems

  1. CA (Consistent & Available)
  2. CP (Consistent & Partition tolerant)
  3. AP (Available & Partition tolerant)

But here is the catch, Partition tolerance can never be truly ignored since we know that network is faulty.

So we can say the only choice we actually have is between Availability and Consistency. Therefore technically there are only two types of systems we can build in real world because saying that network is always available and connections are fault tolerant would be unwise.

Final

The CAP theorem reduced is then : In a distributed system since partition is inevitable we can choose between building these two types of systems

  1. Consistent and Partition tolerant
  2. Available and Partition tolerant

A system which is CP will not be available because when there is partition we stop returning responses from our services to honor the consistency gaurantee. Example of such system include MySQL, Postgres etc RDBMS which support ACID transactions.

A system which is AP will not be consistent because when there is partition we can continue serving requests but they are not guaranteed to be latest since some part of the system may have gotten an update which the responding system doesn’t yet know about. Example of such system would be the NoSQL databases such as redis, mongodb, hbase etc

Leave a Reply

Your email address will not be published. Required fields are marked *