HorizontalScalability

What is CAP theorem?

Choosing between availability and consistency when designing a distributed system.
Written by
Raphael Cleto
Raphael Cleto

The CAP theorem states that, in a distributed system, you can only have two out of the three possible guarantees: Consistency (C), Availability (A), and Partition Tolerance (P).

In real life, networks can go down, software needs to be restarted and hardware failures happen. So Partition Tolerance (P) needs to be supported by default. The real tradeoff/choice is then made between consistency and availability.

If the network communication goes down, it means that the updates won’t be synced between multiple nodes. So you have 2 options:

CONSISTENCY AND PARTITION TOLERANCE (CP)

Reads always retrieve the most recent written content on success. The response can also be a timeout or an error due to the lack of guaranteed availability. Consistency is a good choice when your business require atomic reads and writes.

Use-case example

AVAILABILITY AND PARTITION TOLERANCE (AP)

Reads will be answered with the available data in the requested node. It has no guarantee that it will be the most up-to-date version of it, since writes take some time to replicate the data. Availability is a good choice for systems that allow flexibility around the data while the system synchronizes.

Use-case example

CONSISTENCY PATTERNS

We have some options when it comes to level of consistency required:

TECHNOLOGY EXAMPLE: MONGODB

By default, MongoDB is strongly consistent: If you do a write and then a read, considering that the write is a success, the read will return the most up-to-date data.

If you enable the feature of reading from secondaries, then MongoDB becomes eventually consistent, which means it is possible to read out-of-date results.