Skip to content

Availability vs Consistency (CAP Theorem)

The three guarantees

In a distributed computer system you can only support two of the following three guarantees at once:

Guarantee Meaning
Consistency Every read receives the most recent write, or an error.
Availability Every request receives a response, without guaranteeing it contains the most recent data.
Partition Tolerance The system keeps operating despite arbitrary network partitions/failures.

Why it's really a two-way choice

Networks aren't reliable, so you must support partition tolerance.

Because partitions (network failures between nodes) will happen, you can't sacrifice partition tolerance in practice. That leaves a software tradeoff between consistency and availability.

So the real question is: CP or AP?

CP — Consistency + Partition Tolerance

  • When a partition occurs, the system waits for a response from the partitioned node, which may result in a timeout error.

  • Choose CP when your business needs require atomic reads and writes (e.g., banking, inventory).

AP — Availability + Partition Tolerance

  • Responses return the most readily available version of the data on any node, which might not be the latest.

  • Writes may take time to propagate once the partition is resolved.

  • Choose AP when the business can tolerate eventual consistency, or when the system must keep working despite external errors (e.g., social feeds, DNS, shopping carts).

Key takeaways

  • CAP is often taught as "pick two of three," but in real distributed systems partition tolerance is non-negotiable, so the practical choice is between C and A.

  • This maps directly to the consistency patterns in 04-consistency-patterns.md.

Distributed Lock Service Design

A distributed lock service provides mutual exclusion across multiple nodes. The core challenges are handling network partitions, node failures, and preventing two clients from holding the same lock simultaneously (split-brain).

Core Building Blocks

  • Strongly consistent store: Use a consensus-based coordination service such as ZooKeeper, etcd, or Chubby. These systems provide linearizable writes and sequential consistency, which are required for safe lock acquisition. Avoid using plain Redis without careful fencing because a single Redis instance is a single point of failure, and Redlock-style multi-node algorithms can still lose safety under GC pauses or network delays.
  • Lease-based locks: Each lock is granted with a lease (TTL). The client must renew the lease before it expires. If the client crashes or is partitioned, the lock is automatically released after the TTL, allowing other clients to make progress.
  • Fencing tokens: When a lock is acquired, the lock service returns a monotonic token (e.g., a version number). The client passes this token with every operation. Storage or other services reject operations with stale tokens, preventing a delayed client that still believes it holds the lock from causing corruption.
  • Lock metadata: Store lock holder ID, lease expiration, and fencing token in a persistent, replicated path (e.g., a ZooKeeper znode or etcd key). Clients use atomic compare-and-swap or create-with-uniqueness to acquire locks.

How This Answers the Question

When designing a distributed lock service, explicitly choose a consensus-based backend for leadership and linearizability. Describe the acquisition / renewal / release protocol, the role of leases to tolerate crashes, and fencing tokens to guard against split-brain. Mention trade-offs: a lock service built on ZooKeeper/etcd favors safety over raw throughput; a Redis-based approach is faster but must include fencing and careful failure analysis. This explains how the lock service coordinates mutually exclusive access across distributed clients safely and with high availability.


flowchart LR
    C[Client] --> LS[Lock Service - etcd / ZooKeeper]
    LS -->|grant lease + fencing token| C
    C -->|renew lease| LS
    C -->|write with fencing token| S[Storage]
    S -->|reject stale token| C

Common Interview Questions

Design a globally distributed database

Why it's asked here: multi-region data forces the CAP decision front and center.

Key points to discuss:

  • You can't avoid partitions across regions → pick CP or AP per use case.
  • CP: block/error on the minority side (e.g., Spanner-style with consensus).
  • AP: keep serving stale data and reconcile later (e.g., Dynamo with vector clocks).
  • Quantify the cost of each: stale reads (AP) vs unavailability (CP).
flowchart LR
    U[User] --> DC1[DC A]
    U --> DC2[DC B]
    DC1 <-->|quorum replication| DC2
    DC1 -->|CP| E[Error on minority]
    DC2 -->|AP| S[Serve stale]

Further reading