LearnastraTHE DESIGN COURSE

Understand the building blocks / CHAPTER 17

CAP: consistency and availability during partitions

Use a disconnected write-and-read timeline to explain the CAP limit and choose safe behavior for each operation.

Advanced1h of deliberate study10 questions

Builds on Characteristics of distributed systems, Redundancy and replication.

By the end, you can
  • Define CAP consistency and availability precisely.
  • Trace an operation across a network partition.
  • Choose a safe product behavior without using a pick-any-two slogan.

01Ask what happens when replicas cannot talk

East and West keep copies of a shared number, called x. Both begin at zero. Their network link breaks, but clients can still reach either region.

  1. One client asks East to set x to 1.
  2. East saves the change and reports success.
  3. Only after receiving that success, another client asks West to read x.
  4. West still has zero and cannot communicate with East. What should it return?

West cannot learn whether East accepted the write until the required communication recovers. This example exposes the CAP limit. CAP names consistency, availability, and partition tolerance, with specific meanings:

  • A network partition prevents required messages from getting between parts of the system. It does not require either region’s process to stop.
  • Consistency in the usual CAP formulation means linearizability: operations on the same modeled data appear to occur one at a time in an order that respects real completion times. A read starting after a successful write must reflect that write or a later valid change.
  • CAP availability requires every request received by a non-failing node eventually to complete the requested operation with an appropriate result. A quick refusal is a failure response, not completion of that write or read.

During a partition, the same replicated read/write data cannot guarantee both of those last two promises at every non-failing node. Eventual agreement between copies, valid database rows, monthly success percentages, and practical response-time targets remain useful ideas, but they are different definitions.

02Work the two-region trace

Apply each possible action to West’s later read. Keep East’s successful write earlier than that read; otherwise this example would not test the same promise.

West’s action What happens Which promise is missing?
Return local zero The read completes, but misses the completed write of 1. Linearizability of this shared value.
Wait for the missing communication The read has no result while the partition persists. CAP availability for this request.
Refuse because it cannot establish the required state The caller gets a defined failure. Successful completion of the requested operation at this node.

More replicas can allow useful service on part of the system:

  1. Suppose a protocol needs more than half the replicas to establish authority.
  2. The side containing that majority may continue under the protocol’s full rules.
  3. A smaller isolated side cannot meet the same requirement and waits or refuses.
  4. Requests on that smaller side still fail to complete, even when its processes are running. This is therefore not universal CAP availability.

Avoid “choose any two” as a design procedure:

  • A deployed network can lose communication regardless of the database label. The design must say what happens then.
  • When required communication works, a supported protocol can often provide both useful availability and strong consistency. CAP does not ban that normal behavior.
  • CAP states an impossible combination of guarantees. An implementation can still do worse by losing availability without providing the intended consistency.

Draw the failed communication and one operation timeline before applying a label.

03Choose behavior per operation and recover

A marketplace need not use the same failure policy for every operation. Follow the consequence for each one:

  • A seat confirmation must establish that no other purchase owns the seat. If an isolated region cannot establish exclusive ownership, it must wait or fail rather than confirm twice.
  • A public event description can come from an older local copy if the product permits that age.
  • A view counter may accept local increments if the service safely saves them and can combine them without losing or counting an increment twice.

Make the counter’s duplicate handling concrete:

  1. Assign each intended increment a stable identifier; a retry retains that same identifier.
  2. Save accepted identifiers in the region before reporting success under the chosen durability policy.
  3. After communication returns, combine the retained identifier sets from the regions.
  4. Count each distinct identifier once. Do not add the same retried increment again.

This simple exercise explains the required behavior; a production implementation may use a more compact representation. A timestamp alone does not add every increment correctly.

For conflicting edits, name what is lost. A last-write-wins rule keeps the value with the winning timestamp. Two edits of the same profile field can converge to one value while discarding the other edit; clock disagreements and ties need rules too.

For every local-completion policy, answer these recovery questions:

  1. Which updates were confirmed, and where are they retained?
  2. How are duplicates recognized and conflicting versions compared?
  3. Who resolves incompatible changes, and what reversal or compensation can the user see?
  4. Which permission checks remain required throughout the outage?

Useful reduced service is a product choice with explicit guarantees. A counter merge does not solve exclusive seat ownership.

A completed write precedes an isolated read

A completed write precedes an isolated readStart with zero at East and West, then break their communication. East saves 1 and returns success to its client. Only afterward, another client starts the West read. Compare an old-value result with waiting or refusal using the stated CAP definitions. Numbered arrows correspond to the connection key below. All connections are directional.East: x=1Write has completedNetwork partitionNo East↔West messagesWest: x=0Cannot learncompleted writeClient A1. Write and receivesuccessWest’s decisionStale result ORwait/rejectClient B3. Start read after Areceives success1234A completed write precedes an isolated readStart with zero at East and West, then break their communication. East saves 1 and returns success to its client. Only afterward, another client starts the West read. Compare an old-value result with waiting or refusal using the stated CAP definitions. Numbered arrows correspond to the connection key below. All connections are directional.East: x=1Write has completedNetwork partitionNo East↔West messagesWest: x=0Cannot learncompleted writeClient A1. Write and receivesuccessWest’s decisionStale result ORwait/rejectClient B3. Start read after Areceives success1234A completed write precedes an isolated readStart with zero at East and West, then break their communication. East saves 1 and returns success to its client. Only afterward, another client starts the West read. Compare an old-value result with waiting or refusal using the stated CAP definitions. Numbered arrows correspond to the connection key below. All connections are directional.East: x=1Write has completedNetwork partitionNo East↔West messagesWest: x=0Cannot learncompleted writeClient A1. Write and receivesuccessWest’s decisionStale result ORwait/rejectClient B3. Start read after Areceives success1234

Follow the numbered arrows

  1. Client A → East: x=11. write x=1
  2. East: x=1 → Client A2. return successful completion
  3. Client B → West: x=03. read begins after that success
  4. West: x=0 → West’s decisionstale result OR wait/reject
East confirms x=1 before the West read begins. The link is broken, so West still has zero and cannot learn about the write. Returning zero and waiting or refusing violate different guarantees.
Trace the diagram in words
  1. Start with zero at East and West, then break their communication.
  2. East saves 1 and returns success to its client.
  3. Only afterward, another client starts the West read.
  4. Compare an old-value result with waiting or refusal using the stated CAP definitions.

DRAW. CALCULATE. EXPLAIN.

Choose three marketplace partition policies

20 minutes · guided practice

A region cannot reach the seat-ownership service, but retains public event descriptions and can safely save local view-counter updates.

  1. Choose behavior for seat confirmations.
  2. Choose behavior for description reads.
  3. Explain how local increments and their retries are saved and later combined.
Your self-check
Compare the worked solution
  1. Do not confirm a seat without establishing exclusive ownership. Wait or return the documented failure when the isolated region cannot perform that check.
  2. Serve a public description only within its permitted staleness policy. That permission does not extend to private data whose access may have been revoked.
  3. Give every intended view increment a stable identifier and keep that identifier on retries. Safely retain the accepted identifiers locally under the chosen policy.
  4. After reconnection, combine the retained sets and count each distinct identifier once. Check for missing accepted work; do not treat a repeated delivery as a new view.
  5. This provides useful reduced service. It does not claim every isolated operation is both linearizable and CAP-available.

The checkboxes record your own assessment. The worked solution gives you a way to check the reasoning.

RETRIEVE BEFORE YOU REVEAL

Keep the important ideas.

Say the answer aloud, then turn the card over.

CARD 01What limit does CAP place on operations during a partition?
  1. East confirms a write of 1.
  2. A later West read cannot contact East and still holds 0.
  3. Returning 0 breaks linearizability. Waiting or refusing breaks CAP availability for that request.

The limit concerns the same replicated data during missing communication.

CARD 02Why not “pick any two”?

A network partition is a condition the system must handle, not a feature a label removes.

  • Ask which requests complete during that condition.
  • Ask what values they may return.
  • Keep the normal healthy-network promises separate.
CARD 03What follows a partition policy?
  1. List updates confirmed while regions were disconnected.
  2. Retain them and recognize retries using stable identifiers.
  3. Define how to combine updates or resolve conflicts.
  4. Keep permission checks.

A counter must not double-count; a seat must not gain a second owner.

Practise this topic in the revision deck ↗

CHECK THE REASONING

Can you explain the choice?

Answer every question, then finish for an immediate score and an explanation for each answer. Aim for 80%, then revisit any gaps.

1 / 10East completes x=1 before an isolated West read begins. West returns 0. Which promise fails?
2 / 10West waits indefinitely for East during a persistent partition. What is not guaranteed?
3 / 10A majority continues and a healthy isolated minority rejects writes. Is full CAP availability achieved?
4 / 10Two isolated regions each see one available seat. Which choice preserves no double confirmation?
5 / 10What does consistency mean in the usual CAP formulation?
6 / 10What does a quick unavailable error establish for a refused write?
7 / 10Why is partition tolerance not simply omitted from a deployed replicated network?
8 / 10An availability-oriented counter accepts local increments. What remains necessary?
9 / 10Why can a single AP label for an entire application hide important details?
10 / 10After choosing local completion during a partition, what must be designed next?
0 of 10 answered

CLOSE THE LOOP

Explain it without the page.

Summarise the mechanism, one trade-off, and one failure mode. Then mark this chapter complete.

Go to the source