Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

CAP Theorem & Collaborative Apps

Recommended resources

Hello! In our last lesson, we explored the spectrum from strong to eventual consistency and established that for your offline-first collaborative application, prioritizing availability and low latency is paramount. We concluded that when a user goes offline, creating a network partition, the application must remain usable.

Today, we will formalize this intuition with one of the most foundational principles in distributed systems: the CAP theorem. This lesson will describe the theorem and, more importantly, have you analyze the specific trade-offs that real-time collaborative applications like your visual workflow builder must make. Understanding CAP provides the "why" behind the architectural patterns we'll be building upon, including the eventual need for CRDTs.

The Three Guarantees and the Fundamental Trade-off

The CAP theorem, also known as Brewer's theorem, is a simple yet powerful statement about the constraints of distributed data stores. It asserts that it is impossible for any distributed system to simultaneously guarantee more than two of the following three properties:

  1. Consistency (C): All nodes in the system see the same data at the same time. Every read operation returns the value of the most recent successful write. This is the "strong consistency" we discussed in the previous lesson.
  2. Availability (A): Every request receives a (non-error) response, without the guarantee that it contains the most recent write. The system remains operational for both reads and writes.
  3. Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes. In our context, a user's device going offline is a classic example of a network partition.

A great way to build an initial intuition for this is to see it framed from a perspective you're very familiar with—the frontend.

CAP theorem explained for Frontend Developers

The video "CAP theorem explained for Frontend Developers" by Sam Selikoff does an excellent job of demonstrating that CAP is not just a "database nerd" concept. It's a fundamental reality for any application with a client-server architecture.

Watch the first part of the video, from the introduction to the todo app example. Pay close attention to how the choice you make in the UI when the network goes down—either show an error or allow the user to continue working—directly maps to choosing between Consistency and Availability.

The video illustrates the core dilemma. When a partition occurs (the user is on the subway), you are forced to make a choice. This isn't a theoretical exercise; it's a practical product and engineering decision. The following diagram visualizes this trade-off.

This Venn diagram illustrates the core idea of the CAP theorem. A distributed system can only pick two of the three properties: Consistency (C), Availability (A), or Partition Tolerance (P). As we'll see, the choice is often simpler than it appears.

Why Partition Tolerance is Non-Negotiable

In any real-world application that communicates over a network—from a global-scale database to your React app fetching data from an API—the network is unreliable. Packets get dropped, latency spikes, and connections fail. As a result, partition tolerance (P) is not a choice; it's a requirement. Any system that isn't partition tolerant will fail completely the moment there's a network issue.

This reality simplifies the CAP theorem significantly. Since we must tolerate partitions, the real trade-off for any distributed system is between Consistency and Availability.

When a network partition happens, do you:

  • Choose C over A? To guarantee consistency, the system might have to stop accepting writes or even reads that it cannot confirm with other nodes, thus sacrificing availability.
  • Choose A over C? To guarantee availability, the system continues to accept reads and writes on both sides of the partition, knowing that this may lead to temporary data inconsistency.

The following article gives a crisp, concrete example of this "impossible choice."

CAP Theorem: The Fundamental Trade-off in Distributed Systems-The Compiler Journal

This article provides clear, formal definitions and a great thought experiment to solidify the C vs. A trade-off.

Please read from the beginning of the article down to the end of the section The Impossible Choice. The example of the two ATMs during a network partition perfectly illustrates why you cannot have both C and A when P occurs.


Real-World Systems: CP vs. AP

This fundamental trade-off leads to two primary categories of distributed systems.

  • CP (Consistency & Partition Tolerance): These systems choose correctness over availability. When a partition occurs, they will refuse to serve requests that could lead to inconsistent state. The banking and inventory management systems mentioned in the LINK article are classic examples. It's better to show an error than to sell an item that's out of stock or allow an account to be overdrawn.

  • AP (Availability & Partition Tolerance): These systems choose responsiveness over perfect consistency. They continue to operate during a partition, accepting that data might become temporarily inconsistent. This is the world of eventual consistency we discussed previously.

For your goal of building a real-time collaborative tool, this choice has profound implications for the user experience.


The Trade-off for Collaborative and Offline-First Apps

As a frontend architect, your focus is on delivering a performant and resilient user experience. The trade-offs dictated by the CAP theorem are not just backend concerns; they directly shape the UI and UX. A system that becomes unavailable the moment Wi-Fi drops is, for many use cases, a poor experience.

CAP theorem explained for Frontend Developers

Let's return to Sam Selikoff's video, which extends the CAP theorem to databases and, most importantly, brings the implications back to frontend application design.

Please watch the remainder of the video, from this point. He explains why P is a given, how different database technologies make different C vs. A trade-offs (like Amazon's DynamoDB favoring availability for its shopping cart), and why this matters for the apps you build.

As the video highlights, for many modern applications, especially collaborative ones, availability is key. Think about Google Docs, Figma, or Slack. These applications don't simply freeze when your connection is unstable. They allow you to continue typing or moving objects. They choose A over C. This is the essence of building an "optimistic UI" and is the foundation for offline-first functionality.

The author of the "Brave New Geek" blog makes this point even more forcefully, reframing the entire challenge as a UX problem.

cap theorem – Brave New Geek

This article argues that the trade-offs we make in distributed systems have direct consequences on the user experience, and that we should design for failure and recovery rather than striving for perfect consistency.

Please read the sections The Truth is Prohibitively Expensive and Failure Is an Option. The core message is powerful: for many applications, choosing availability and designing the UX to handle potential inconsistencies leads to a dramatically better product. The article's reference to how banking systems actually work—compensating for errors rather than preventing them altogether—is a great real-world analogy for an AP system.

For your visual workflow builder, the implications are clear:

  • A user must be able to create, move, and edit nodes even when offline (a network partition).
  • This means your application must prioritize Availability over strong Consistency.
  • By making this choice, you accept that when the user comes back online, their local state might conflict with changes made by other collaborators.
  • Therefore, your system needs a mechanism to merge these conflicting states predictably and without losing data.

This is precisely the problem that Conflict-free Replicated Data Types (CRDTs) are designed to solve. They are data structures for AP systems that guarantee Strong Eventual Consistency.


Conclusion

In this lesson, we've unpacked the CAP theorem and connected its theoretical constraints to the practical realities of building your collaborative application.

Here are the key takeaways:

  • The CAP Theorem: A distributed system can only provide two of three guarantees: Consistency (C), Availability (A), and Partition Tolerance (P).
  • P is a Given: Since network failures are inevitable in any real-world system, Partition Tolerance is non-negotiable.
  • The Real Choice is C vs. A: During a network partition, a system must choose between remaining Consistent (and potentially becoming unavailable) or remaining Available (and potentially becoming inconsistent).
  • Collaborative Apps Choose A: To provide a fluid, offline-first user experience, applications like your planned canvas builder must choose Availability over Consistency (becoming AP systems). This allows users to continue working without interruption.

We've now established why we must allow the application to function during a network partition. In our next lesson, we will begin to explore how this is accomplished by looking at optimistic replication, the underlying mechanism that allows changes to be made locally and synchronized later.