Understanding the CAP Theorem; Through a Food Delivery App

Enaikeleomoh
Tech Notions
Published in
4 min readMay 30, 2024

Developing a food delivery app was challenging and enlightening, especially when managing orders efficiently. As the number of orders grew, handling them alone became impossible, so I enlisted the help of a partner.

This collaboration, however, introduced a common problem in distributed systems: maintaining consistency when each partner doesn’t know their orders.

Here, I’ll explain the CAP theorem with this real-world scenario.

The CAP Theorem in Distributed Systems

The CAP theorem states that in any distributed system, you can only achieve two out of the following three guarantees:

  1. Consistency (C): Every read receives the most recent write.
  2. Availability (A): Every request receives a response, without guarantee that it contains the most recent write.
  3. Partition Tolerance (P): The system continues to operate despite network partitions.

In our food delivery app scenario:

  • Consistency: All nodes (partners) return the latest order status.
  • Availability: All nodes respond to requests, even if they don’t have the latest order status.
  • Partition Tolerance: The system continues to function even if there is a communication breakdown between nodes.

To build a robust system, you need to understand your use cases and requirements:

  • Eventual Consistency: The system will become consistent over time, which might be acceptable for non-critical applications.
  • Tweaking Partition Tolerance: Adjust how much inconsistency you can tolerate during network failures.

Consistency: Keeping Everyone on the Same Page

Consistency in a distributed system means every read operation returns the most recent write. In the context of our food delivery app, it means that both my partner and I need to know exactly which orders have been received and their status.

For example, if I take an order and my partner is unaware, we risk creating a poor customer experience. To maintain consistency, I needed to ensure that my partner was immediately informed of any new orders.

However, imagine a situation where my partner isn’t available, perhaps due to a network issue or other problem. This leads us to another important aspect: availability.

Availability: Ensuring the System is Always Up

Availability refers to the system’s ability to respond to requests. A highly available system can handle many requests and continue to function even if parts of it fail.

For our app, this means ensuring that orders can be taken and processed even if one of us isn’t available.

By adding more partners or servers, we can increase the system’s availability. However, each server must come to a consensus on the orders to maintain consistency.

But what happens if the communication between servers breaks down? This brings us to the third aspect of the CAP theorem: partition tolerance.

Partition Tolerance: Dealing with Network Failures

Partition tolerance means the system continues to operate despite network partitions that prevent some servers from communicating with others.

Imagine the communication channel between my partner and me breaks down. Our system might start losing consistency since we can’t synchronize the order data. In this case, we must choose between consistency and availability.

If we tolerate partitioning, we might sacrifice consistency to keep the system available. This means orders can still be taken, but there might be inconsistencies between what different partners see.

For a good customer experience, it’s often better to shut down one server to maintain consistency while the other handles the requests.

Understanding the Trade-offs

Consistency and Availability (CA) without Partition Tolerance:

If the system prioritizes consistency and availability but cannot tolerate partitions, it assumes that there is always a reliable network and that nodes do not fail.

In real-world scenarios, network failures and partitions are inevitable, making this combination impractical for distributed systems.

Consistency and Partition Tolerance (CP) without Availability:

If the system prioritizes consistency and partition tolerance, it may sacrifice availability. During a partition, the system might not respond to some requests to maintain consistency, effectively becoming unavailable to ensure that the data remains consistent across nodes.

Availability and Partition Tolerance (AP) without Consistency:

If the system prioritizes availability and partition tolerance, it may sacrifice consistency. The system will always respond to requests even during a partition, which might lead to different nodes having different versions of the data (eventual consistency).

In conclusion, the CAP theorem helps us understand the trade-offs in designing distributed systems. You can create a system that meets your needs by balancing consistency, availability, and partition tolerance.

In our food delivery app, we had to decide what was more important: having a consistent view of orders or ensuring the system was always available.

Understanding these principles can help you build more resilient and efficient distributed systems.

--

--