Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate18 min

CAP Theorem and Consistency Models: Trading Guarantees

Understand the idea

Why network partitions force distributed databases to choose between correctness and uptime.

The idea

When a database runs across multiple servers, things can go wrong with the network cables, switches, or cloud connections between them. The describes a simple rule of nature that every distributed database must follow. The acronym stands for Consistency, Availability, and Partition Tolerance:

  • Consistency (C): Every read returns the newest write, or an error. Nobody sees old data.
  • Availability (A): Every working server responds to requests without failing, but it might return older data.
  • Partition Tolerance (P): The system continues running even when network connections between servers are interrupted or cut.

In any real-world network, connections will occasionally drop or lag. That means Partition Tolerance (P) is not optional; networks will partition. So when a network split happens, the database has to pick between two paths:

  1. CP Systems (Consistency + Partition Tolerance): The database refuses writes or pauses until servers can talk again, choosing accuracy over being available.
  2. AP Systems (Availability + Partition Tolerance): The database keeps accepting requests on whatever servers are still reachable, accepting that some servers might temporarily show old data ().

What changes

CP vs AP Systems in a Network Partition
ArchitectureWhen Network PartitionsData GuaranteeExample Systems
CP (Consistency)Rejects writes or returns errors if quorum is lostStrong consistency: no stale readsZooKeeper, etcd, MongoDB (strict write concern), Spanner
AP (Availability)Always returns a response, even from isolated nodesEventual consistency: stale reads possibleCassandra, DynamoDB (eventual mode), Couchbase

CAP triangle

Given up: Availability during a partition

Node 1

value = v1

↔

Node 2

value = v1

Choose two properties on the triangle. All nodes agree while the network is connected.

01 / 03CAP triangle

The command

Simulating CP vs AP partition response

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

The mistake

Check the result

Run the example to see a CP system reject a write when severed from quorum, while an AP system accepts the write and marks it for eventual sync.

Guided challenge

Classify CAP partition responses

Update the function so it returns 'reject_write' for CP systems and 'serve_stale' for AP systems.

Classify CAP partition responses

3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

EXPECTED RESULT

reject_write
serve_stale

Hints 0/1

    Use all hints or make 3 failed checks (0/3).

    Free exploration

    Change this example and run it again. Each run is a new process. Nothing is saved.

    Your variation

    3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does the CAP theorem prove regarding network partitions in distributed systems?
    02What is the core guarantee of eventual consistency in an AP database?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson