Sub topic 11
Consistency and durability requirements
This chapter assumes you know the difference between functional and non-functional requirements. It also assumes you have met SLIs and SLOs, and that you know what a database replica is. Here you will learn to write two quality requirements precisely: how long data must survive, and what readers are allowed to see.
Why these two qualities need explicit requirements
Durability and consistency are easy to leave out because they seem like database settings. They are really promises to users. A shopper who sees "order placed" expects the order to exist tomorrow. That is durability. A user who changes their email address expects to see the new address on the next page load. That is consistency.
Both promises cost money and latency. If you leave them unstated, the team either over-builds (synchronous writes to three regions for a like counter) or under-builds (an acknowledged payment that vanishes after a crash).
Durability requirements
Durability means that once the system has acknowledged a write, the data survives the failures you named. The key word is acknowledged. A write that fails and tells the client so is not a durability violation. A write that is acknowledged and later lost is.
A durability requirement needs three parts:
- The trigger: which event counts as the commitment point, such as "after the API returns 201".
- The failure classes: process crash, disk loss, host loss, zone loss, region loss, operator error.
- The tolerated loss, usually stated as a recovery point objective (RPO). The RPO is the maximum amount of acknowledged data you may lose, measured in time or in writes.
Compare two versions of the same requirement:
- Weak: "Data must be durable."
- Testable: "A short URL whose creation has been acknowledged survives the loss of any single availability zone. RPO is zero for zone failure and at most 5 minutes for regional failure."
The second version tells you what to build. Zero loss across zones means the write must reach storage in a second zone before the acknowledgment. A 5-minute regional RPO allows asynchronous cross-region replication.
Pair the RPO with a recovery time objective (RTO), the time allowed to restore service after a failure. RPO is about data and RTO is about time, so they are separate requirements.
Mechanisms durability pushes you toward
- Write-ahead log with fsync. The database writes the change to a log and forces it to disk before acknowledging. Without the flush, the operating system may still hold the data in memory when the power fails.
- Synchronous replication. The leader waits for at least one replica to confirm. This survives host loss but adds a network round trip to every write.
- Backups and point-in-time recovery. Replication copies mistakes too, so a bad
DELETEreaches every replica. Backups protect against operator error and corruption. - Checksums and scrubbing. These detect silent corruption before the last good copy is gone.
Consistency requirements
Consistency, in this chapter, means what a reader may observe after a write when the data is stored in more than one place. It is not the same as the "C" in ACID, which is about a database keeping its own rules (constraints) intact.
The common levels, from strictest to weakest:
| Level | Guarantee | Typical cost |
|---|---|---|
| Strong (linearizable) | Every read sees the latest acknowledged write | Extra round trips, lower availability during partitions |
| Read-your-writes | A client always sees its own writes | Routing or session tracking |
| Monotonic reads | A client never sees data go backward in time | Sticky routing to one replica |
| Eventual | Replicas converge if writes stop, with no bound on when | Cheapest, but readers can see stale data |
Do not apply one level to the whole system. Choose per operation. In a URL shortener, creating a link and then immediately opening it should work for the creator (read-your-writes). A link created three seconds ago may take a few seconds to work for everyone else.
Writing a testable consistency requirement
State the operation, the reader, and the staleness bound:
- Weak: "The system should be consistent."
- Testable: "After a user changes their display name, that user sees the new name on every subsequent request. Other users see it within 5 seconds at the 99th percentile."
The second version has two requirements with different costs. The first can be met by sending a user's reads to the leader for a short window after a write. The second allows asynchronous replicas.
Seeing replica lag
The simulation below shows why eventual consistency produces stale reads. Writes go to the leader and are queued for the follower. A read from the follower before replication returns nothing.
leader = {}
follower = {}
replication_queue = []
def write(key, value):
leader[key] = value
replication_queue.append((key, value))
def replicate():
while replication_queue:
key, value = replication_queue.pop(0)
follower[key] = value
write("user:1:name", "Ada")
print(follower.get("user:1:name")) # None: a stale read
replicate()
print(follower.get("user:1:name")) # Ada: the replicas have convergedThe diagram shows the same sequence with a client that reads from a replica.
How the two qualities interact
Durability and consistency both depend on when you acknowledge a write. The later you acknowledge, the more copies exist, and the safer and more consistent the system is. The cost is write latency.
| Acknowledge after | Durability | Consistency of replica reads | Write latency |
|---|---|---|---|
| Leader memory only | Lost on crash | Stale | Lowest |
| Leader disk flush | Survives process crash | Stale | Low |
| One synchronous replica | Survives host loss | Mostly fresh | Medium |
| Majority of replicas | Survives zone loss | Strong with quorum reads | Highest |
This ties directly to your latency SLO. If the requirement says 99% of writes finish in under 50 ms, a cross-region synchronous write with a 120 ms round trip cannot meet it. One of the two requirements has to change.
Common errors
- Unbounded "eventual". Eventual consistency with no staleness bound cannot be tested. Add a number and a percentile.
- Treating acknowledgment as vague. If the requirement does not say when the system acknowledges, the team will pick the fastest option.
- Demanding strong consistency everywhere. Search indexes, counters, and recommendation lists rarely need it. Reserve it for money, inventory, and uniqueness constraints.
- Ignoring failure classes. "Survives failures" means nothing until you list which ones.
- Confusing the two meanings of consistency. Say "replica consistency" or "ACID consistency" explicitly.
Exercise
A ticketing service has two requirements. (a) A seat sold to one customer must never be sold to another. (b) The "tickets remaining" badge on the event page may lag. Which operation needs strong consistency, and what should the requirement for the badge say?
Show answer
Seat reservation needs strong consistency, because two buyers could otherwise both succeed. It must read and write the latest state, for example through a single leader or a transaction with a uniqueness constraint. The badge can use eventual consistency, but the requirement must bound the lag, for example "the count shown is at most 10 seconds old for 99% of page views."
Exercise
Rewrite "Orders must not be lost" as a testable durability requirement. Include the commitment point, one failure class, and an RPO.
Show answer
Example: "An order acknowledged with HTTP 201 survives the loss of any single availability zone, with an RPO of zero. Orders are written to storage in at least two zones before the 201 is returned." Any answer with a defined acknowledgment point, a named failure class, and a numeric RPO is correct.