Lesson 04

CAP and PACELC, Precisely

Building block

CAP is quoted in the round as a label far more often than as what it says. It says one thing: while the network splits the replicas, a request that reaches the cut-off side either gets an answer that may be out of date, or no answer at all.

That is a choice made during a failure, per operation, and not a label printed on a database. The trade that shapes every design is the one PACELC adds: with no failure at all, each consistent answer waits for other machines.

What CAP Says

The precise statement is this: in a network that can lose messages, no system can answer every request and also keep every answer linearizable. Each letter means less than the slogan suggests.

C is linearizability, the one-copy model of the last lesson, and not the C of a database transaction. A means that every request a working node receives gets an answer; it is not a count of nines. P is a fact of networks rather than an option. So the choice is between C and A, and it exists only while a partition lasts.

What CAP Does Not Say

Pick two of three is the misquote. Eric Brewer, who first put CAP forward, wrote in 2012 that the two-of-three formulation was always misleading. A system without partition tolerance is a system on one machine.

Keeping C does not mean being down. A system that keeps C refuses requests only on the cut-off side and only during the cut, so it can still be highly available. Brewer called Google's Spanner technically CP and said its users can assume both. And many stores let each request choose: Cassandra's consistency levels run from one replica to all of them.

Choosing per Operation

The move the interviewer listens for is to take each write and say what the cut-off side does with it. Refuse when two answers would cost money or trust, as with the last unit of stock or a payment. Accept and reconcile later when a lost or duplicated update is cheap to merge, as with a cart, a like or a status. The merge is the next lesson.

You might say: "During a split, the stock count refuses writes on the minority side; the cart keeps accepting and merges afterwards."

One write on the cut-off side of a split
Refusekeeps C
The user sees: an error, and tries again later or on the other side.
Afterwards: nothing to merge; the majority side's state is the truth.
Answerkeeps A
The user sees: success, possibly built on stale data.
Afterwards: both sides' writes are merged when the split heals.

Else, Latency

Daniel Abadi's PACELC, from 2012, adds the half of the trade that runs every day. If there is a partition, choose availability or consistency. Else, choose latency or consistency. The name spells the rule out: P for a partition, then A or C; E for else, then L or C.

The else half is priced in round trips. A write that waits for a copy in another zone pays 1 to 2 milliseconds. A leader in US east that waits for a copy in western Europe pays about 85. Waiting for a copy in Southeast Asia costs about 225.

Now place three copies, one in each of those regions, and wait for a majority. Two of three is the leader plus one more, so the leader waits only for the nearer copy. A consistent write costs about 85 milliseconds, not 225.

The rule from this chapter's first lesson holds here too: wait inside the region while a zone is the failure to survive, and pay for a remote copy once a region's loss must lose nothing. How regions are laid out is Chapter 6's lesson on regions.

What a consistent write from US east waits for
Another zonesame region
1-2 ms. Survives a zone, not a region.
Western Europeone remote copy
About 85 ms on every write.
Majority of threewestern Europe and Southeast Asia
About 85 ms: the nearer remote copy completes the majority.
All threewestern Europe and Southeast Asia
About 225 ms: the farthest copy sets the pace.

Placing a Design

Say both halves for each kind of data, not for the store. Abadi places the default settings of Dynamo, Cassandra and Riak on the available, low-latency side of both halves: they give up consistency during a partition, and they give it up for latency in normal running too.

A worked line for a shop: the stock count chooses consistency in both halves, refusing on the cut-off side of a split and waiting for its replicas on a normal day. The cart chooses availability during a split and the nearest copy the rest of the time. The product page does the same as the cart, behind a cache.

A shop's data, placed by requirement
Two buyers must never get the last unit→Stock count: PC/EC
A shopper can always add to the cart→Cart: PA/EL
A product page may be a little stale→Product page: PA/EL, cached
CAP vs PACELC

CAP describes the choice during a partition: answer with possibly stale data, or refuse. Use it to say what each operation does on the cut-off side of a split.

PACELC adds the choice on every normal request: wait for other replicas, or answer from the nearest copy. Use it to price consistency on an ordinary day, which is the trade that shows up in latency numbers.

How Interviewers Probe This

Start from the operation, then say what it does during a split, and price it on a normal day last.

The first probe is "Is your system CP or AP?" Decline the single label, split the operations, and say for each what happens on the minority side of a split.

The second is "There is no partition today. What is consistency costing you?" Give the round trip each consistent write waits for, from where its replicas sit, and name the operations that skip the wait.

The third is "Your replicas span three regions. How slow is a consistent write?" Compute the majority's wait as the nearer remote copy, not the farthest, which is about 85 milliseconds from US east. Then say what moving one copy would change. Move the Southeast Asia copy into another zone of US east, and the majority's wait drops to 1 to 2 milliseconds, but losing US east now takes two of the three copies.

Misconceptions
  • "This database is AP, so it is never consistent." The label describes default behaviour during a split. A request can often ask for more replicas, and pays for them in latency.
  • "We choose CA." A replicated system on a network cannot opt out of partitions; CA describes one machine.
  • "CP means often down." It refuses requests only on the cut-off side and only for the length of the partition. The majority side keeps serving.
  • "The C in CAP is the C in ACID." CAP's C is linearizability of a single item; ACID's C is the database keeping its declared rules.
  • "CAP is the trade that matters most." Partitions are rare, and the latency of waiting for replicas is paid on every request, which is why PACELC's second half drives most choices.
Why It Matters
  • Answer "CP or AP?" per operation, saying what the cut-off side does for each.
  • Price consistency in milliseconds from where the replicas sit: 1 to 2 across zones, about 85 from US east to western Europe.
  • Wait for a majority rather than for every copy, and say which copy the majority waits for.
  • Keep the operations that must refuse during a split few and named, and let the rest answer from the nearest copy.
RelatedConsistency Models That Come Up what C promisesQuorums the R plus W dial per requestConcurrent Writes and Conflicts merging after a splitRegions and the Arithmetic of Nines where the copies sit

Knowledge Check

In CAP, what do C and A mean?

  • C is the database keeping its declared rules; A is uptime counted in nines
  • C is linearizability; A is an answer to every request a working node gets
  • C is identical bytes on every replica; A is a majority of the nodes still up
  • C is serializable transactions; A is an answer within a set latency

A candidate says their replicated store is CA. What is wrong with that?

  • CA is allowed only when every replica sits inside a single zone
  • CA works only when every write is replicated synchronously
  • Replicas on a network cannot opt out of partitions; CA is one machine
  • Nothing, as long as it refuses writes on the minority side of a split

A CP store of five nodes is split into three and two. What happens on the side with three?

  • It keeps serving reads and writes, because it still holds a majority
  • It refuses every request until the split heals, to stay consistent
  • It serves reads only, because writes need every replica to confirm
  • It answers from local data and merges with the other side afterwards

A leader in US east waits for a majority of three copies; the other two are in western Europe and Southeast Asia. How long does a consistent write wait?

  • About 85 ms, the round trip to the nearer of the two remote copies
  • About 225 ms, the round trip to the farther of the two remote copies
  • About 310 ms, the two round trips to both remote copies in sequence
  • About 1 to 2 ms, because only the leader's own zone has to confirm

During a split, which pairing of shop data and behaviour is right?

  • The stock count accepts and merges later; the cart refuses on the minority side
  • The stock count refuses on the minority side; the cart accepts and merges later
  • Both refuse on the minority side, since any stale answer costs the shop money
  • Both accept and merge later, since a split is too rare to be worth planning for

You got correct