Why Everyone Should Know the CAP Theorem?
Tags: devops, kubernetes, database, cloud
Published: 2023-11-20 18:20:48

---
title: Why Everyone Should Know the CAP Theorem?
published: true
tags: DevOps,kubernetes,database,cloud
canonical_url: https://firstfinger.substack.com/p/why-everyone-should-know-the-cap
---

Suppose we are building an e-commerce system that needs to remain operational even when network issues occur between data centres. However, it is critical that customer data like cart items and order history remain consistent across regions.

Even if you don't work directly with databases, understanding CAP can provide valuable insight into system design trade-offs.

***

**CAP stands for:** 

- **C**onsistency

- **A**vailability 

- **P**artition tolerance

These are three critical properties of distributed databases.
***

## What is the CAP Theorem?

The CAP theorem states that in any distributed database system, you can only guarantee two of these three properties. Sacrificing one property is inevitable [when designing a distributed system](https://firstfinger.in/system-design-vs-software-architecture/).

## Consistency

Consistency means that every read from the database returns the latest write. All nodes in the distributed system return the same data for any given query, even in the event of failures.

Strong consistency is important for use cases like financial transactions, where consistent reads and writes are critical.

## Availability

Availability means every request is guaranteed to receive a response, even if one or more nodes in the system go down. The system stays operational even during network partitions, hardware failures or other outages.

Availability is important for high-performance systems where uptime is critical.

## Partition Tolerance

A partition refers to a communications break between nodes in a distributed system.

Partition tolerance means the system continues to function and uphold its consistency guarantees despite any number of network failures or partitions.

Most distributed systems are designed with partition tolerance, as network issues are a fact of life.

* * *

## The Trade-Off

According to the CAP theorem, among these three properties, a distributed system can only guarantee two at any given time.

Often there is a trade-off between consistency and availability during a partition, known as the CA guarantee.

**Some common database types and their CAP guarantees:** 

1. **CP (Consistency and Partition Tolerance)**: MongoDB, Redis

2. **AP (Availability and Partition Tolerance)**: Cassandra, Riak 

3. **CA (Consistency and Availability)**: Traditional RDBMS like MySQL

When designing a distributed system, engineers must make the tough choice between consistency and availability based on the application's requirements.

There are various strategies to minimize the CAP trade-off, but ultimately the theorem proves it is impossible to guarantee all three properties.

* * *

> If you enjoyed this article, please consider [subscribing to our newsletter](https://short.firstfinger.in/newsletter) 📬 and joining our [WhatsApp Channel](https://short.firstfinger.in/whatsapp-invite) 👥, where we share similar content. ✏️