Lecture: Lecture 21 - Beyond Relational Databases
Related: NoSQL Data Models MapReduce Rereduce and Views · Distributed Transaction Classification and Two-Phase Commit · Transactions and ACID Properties

Replication (Slides 30–34)

Replication stores the same data chunks (same content and schema) across multiple local or remote servers, clusters, or data centers to increase availability (redundancy survives node crashes) and performance (Slides 30–31).

Master-Slave vs. Master-Master (Slide 32)

  • Master-Slave: One Master handles all writes, updates, and inserts; one or more Slaves handle only reads (they cannot write). Gives read scalability only, and the Master is a single point of failure.
  • Master-Master: Supported by CouchDB—multiple nodes can accept writes independently, requiring conflict detection and resolution when concurrent writes collide.

Synchronous vs. Asynchronous replication (Slides 33–34)

ModeMechanismTradeoff / failure consequence
Synchronous (Slide 33)Before committing, Master waits for (all) Slaves to commit (conceptually like 2-Phase Commit)Performance killer, especially in the cloud. Common compromise: wait only for a subset / majority of Slaves.
Asynchronous (Slide 34)Master commits locally immediately; each Slave fetches updates independently laterFaster, but unreliable: if Master fails before any Slave fetches → committed data is lost; if only some Slaves fetched → replicas must reconcile.

The CAP Theorem and “Beyond CAP” (Slides 35–44)

When autonomous machines cooperate over the same dataset (Slide 35), three guarantees are at stake (Slide 36):

  1. Consistency (C): All distributed database nodes provide the same data to applications (single-copy consistency).
  2. Availability (A): Node failures (e.g., master failure) do not prevent surviving nodes from continuing to operate and respond.
  3. Partition tolerance (P): The system continues to operate despite arbitrary message loss when connectivity failures split the network into partitions.

CAP Theorem (Slides 37–39)

Formulated by Eric Brewer (1999–2000) and formally proved by Seth Gilbert and Nancy Lynch (2002), the CAP Theorem states that it is impossible for a distributed system to simultaneously guarantee all three properties (C, A, and P).

Consider two nodes on opposite sides of a network partition (Slide 38):

  • Pick AP (forfeit C — “best effort”, Slide 42): Both sides continue accepting reads and writes (A), so the two sides diverge and lose global consistency at all times. Examples: CouchDB, Cassandra, Dynamo, Riak, SimpleDB, Voldemort (Slide 39).
  • Pick CP (forfeit A — “transaction locking”, Slide 41): To preserve global consistency during a partition, out-of-sync replica sets block requests (turn off A) until the partition heals and consistency is verified. Examples: Paxos, MongoDB, HBase, BigTable, Redis, MemcacheDB (Slide 39).
  • Pick CA (forfeit P — “local consistency”, Slide 40): Both C and A hold only when no partition exists (or within each isolated partition independently). Examples: traditional single-site RDBMSs (MySQL, Postgres), Aster Data, Greenplum, Vertica (Slide 39).

Practical design rule (Slides 38, 43): For wide-area distributed systems, network partitions will happen, so designers cannot forfeit P and must choose between C and A. As Amazon CTO Werner Vogels summarized (Slide 43), any algorithm forcing cross-node agreement under high load and failures eventually becomes a scalability bottleneck—nodes should decide based on local state wherever possible.

”Beyond CAP” — Brewer’s 2012 follow-up (Slide 44)

Reducing CAP to a rigid “pick 2 of 3” rule is misleading for three reasons:

  1. Partitions are rare: There is little reason to sacrifice C or A when the network is healthy and unpartitioned.
  2. Fine-grained choices: The C vs. A choice can differ across subsystems, operations, or even specific users/data items inside the same system.
  3. Continuous, not binary: Availability ranges from 0% to 100%, consistency has many intermediate levels, and even partitions have nuances (such as partial disagreement over whether a partition exists).

ACID vs. BASE (Slides 45–47)

ACID and BASE represent opposite ends of the consistency–availability spectrum (Slide 45):

  • ACID focuses on consistency (Slide 46):
    • Atomicity (A): All-or-nothing execution.
    • Consistency (C): Transaction preserves database integrity rules (unique keys, constraints). Crucial distinction: C in ACID means integrity rules, whereas C in CAP means single-copy consistency across nodes.
    • Isolation (I): Core to CAP—under ACID isolation, a partitioned system can operate on at most one side.
    • Durability (D): Committed results survive failures.
  • BASE focuses on high availability in the presence of partitions (Slide 47):
    • Basically Available: Provides availability in CAP terms.
    • Soft state: System state may change over time even without new input, because delayed updates from the eventual consistency model are still propagating.
      • Added analogy (outside the PDF): Like a liquid settling across connected containers after pouring stops—levels keep adjusting on their own until equilibrium is reached. (Limit: unlike passive water leveling, concurrent conflicting writes require explicit conflict resolution.)
    • Eventual consistency: Once new inputs stop, the system converges to a consistent state over time (e.g., DNS, though DNS is not multi-master).

Conflict resolution and CouchDB’s HTTP REST API (Slides 48–52)

Conflict detection and deterministic winner selection (Slides 48–50)

  • Hotel booking example (Slides 48–49): Customer A books the last available hotel room on Node A; simultaneously, Customer B books that same room on Node B while the nodes are temporarily inconsistent. When they replicate, the hotel room document has two conflicting updates.
  • Who resolves it? Ideally application business logic resolves the semantic conflict (e.g., rebooking or notifying one guest), while the database detects the conflict and picks a temporary deterministic winner so all nodes stay aligned.
  • CouchDB’s deterministic winner algorithm (Slide 50): Every CouchDB instance seeing the same conflict picks the exact same winning and losing revisions by choosing:
    1. The revision with the longest revision history list;
    2. If tied, the revision with the highest _rev value in ASCII sort order.

(Familiarity — Slides 51–52): CouchDB is a “web database” with an HTTP RESTful API requiring no ad-hoc DB client: GET /db/doc_id and HEAD /db/doc_id (200 OK), and PUT /db/doc_id (passing the current revision to prevent silent overwrites: 201 Created, or 409 Conflict if a stale revision is supplied).

Added end-to-end functional walkthrough (outside the PDF)

(Added synthesis example from our study session connecting NoSQL models, MapReduce, CAP/BASE, and conflict resolution): Consider a distributed wallet app running CouchDB across Node A (Milan) and Node B (Rome).

  1. Schema-less writes & Banking Recipe via HTTP PUT:
    • PUT /wallet/tx001 → {"_id": "tx001", "from": "bank", "to": "alice", "amount": 1000} (201 Created)
    • PUT /wallet/tx002 → {"_id": "tx002", "from": "bank", "to": "bob", "amount": 500} (201 Created)
    • PUT /wallet/tx003 → {"_id": "tx003", "from": "alice", "to": "bob", "amount": 100, "note": "Christmas gift"} (201 Created; dynamic "note" field added with zero schema migration; retrying PUT /wallet/tx003 yields 409 Conflict).
  2. Parallel Map → Reduce → Rereduce: Suppose Node A holds {tx001, tx003} and Node B holds {tx002}:
    • Map emits (from, -amount) and (to, +amount) independently per document.
    • Local Reduce (sum(values)) yields Node A {alice: 900, bank: -1000, bob: 100} and Node B {bank: -500, bob: 500}.
    • Rereduce merges nodes cleanly (sum is distributive): alice: 900, bob: 600, bank: -1500 (net sum 0).
  3. Partition (AP + BASE + _rev resolution): The Milan–Rome link breaks. Both nodes stay available (AP). Starting from user_alice at _rev: "1-aaa", Node A writes city: "Torino" (_rev: "2-bbb") while Node B writes city: "Roma" (_rev: "2-ccc"). When the link heals, asynchronous replication propagates updates (Soft state → Eventual consistency). Both nodes tie on history length 2 and compare _rev in ASCII order ("2-ccc" > "2-bbb"), deterministically selecting city: "Roma" and flagging the conflict for application review.

Common confusions

  • Conflating C in ACID with C in CAP: ACID C = database integrity rules (keys/constraints); CAP C = every node returning the exact same copy of data.
  • Thinking CouchDB discards losing conflict revisions silently: CouchDB picks a deterministic winning revision so every replica agrees, while keeping the losing revision marked as a conflict so application logic can inspect and resolve it.
  • Treating CAP as “always broken into 2 of 3”: Under normal unpartitioned operation (no P event), systems can provide both C and A; the C vs. A tradeoff bites during a partition.

Retrieval questions with answers

1. Compare C in ACID with C in CAP, and explain what “Soft state” means in BASE.
C in ACID means preserving database integrity constraints (e.g., unique keys, valid balances), whereas C in CAP means single-copy consistency across all distributed nodes. Soft state means a node’s stored state can still change over time even with no new client input, while delayed updates from other nodes finish propagating toward eventual consistency.

2. During a network partition, why does a CP system become unavailable to at least one side, whereas an AP system stays available?
To guarantee single-copy consistency (C), a CP system blocks reads/writes on out-of-sync replica sets until the partition heals (A sacrificed). An AP system lets every reachable partition answer with its local state (A preserved) at the cost of temporary divergence (C sacrificed).

3. Two CouchDB replicas concurrently update the same document from _rev: "1-00" to "2-a9" (history length 2) and "2-b1" (history length 2). Which revision wins across all nodes, and what if "2-a9" later gets an extra edit "3-01"?
When both have history length 2, CouchDB compares _rev in ASCII sort order, so "2-b1" wins deterministically everywhere. If "2-a9" was updated once more to "3-01" (history length 3 vs. 2), "3-01" wins regardless of ASCII order because the longest revision history list always takes priority first.