Sub topic 11
Consistency and durability requirements
This chapter assumes you know what a replica is, how a database write reaches disk, and how to read short Python snippets. It also assumes you have seen the earlier chapters on non-functional requirements and measurable targets.
Two qualities, two questions
Consistency asks: after a write, what can a reader see? Durability asks: once the system says a write succeeded, can it ever be lost?
Both qualities are requirements, not implementation details. Stating them early decides whether you need a single primary database, synchronous replication, or a cheaper design that tolerates stale reads.
Consistency requirements
Strong consistency means every read returns the result of the latest acknowledged write. Eventual consistency means replicas converge if writes stop, but a read may return old data for a short time.
Write the requirement in terms of what a user can observe. "The data must be consistent" is not testable. These are:
- After a user deletes a short URL, no redirect for it succeeds once the delete call returns.
- A newly created short URL may take up to 5 seconds to resolve in other regions.
- A user always sees their own latest edit (read-your-writes).
Different data in one system can have different needs. A bank balance needs strong consistency. A view counter does not.
Durability requirements
Durability is the promise that an acknowledged write survives crashes and hardware failure. State the point at which the promise starts and the failures it covers.
- Start: "once creation has been acknowledged" (as in NFR-3 of the URL shortener).
- Failures covered: a process crash, a single disk failure, loss of one data center.
Durability is often expressed as a probability of loss over a period. Object stores advertise figures such as 99.999999999% (eleven nines) per year. Treat such numbers as design targets backed by replication, not as guarantees against every disaster such as a bad deployment that deletes data. Backups cover that case.
How the requirement shapes the design
The acknowledgment point decides the cost. The sequence below shows a write that is acknowledged only after a replica has stored it.
Acknowledging after the replica confirms protects against losing the primary, but it adds the replica round trip to write latency. Acknowledging before replication is faster, but a primary crash can lose writes the client believed were saved.
| Choice | Durability | Write latency | Consistency of reads |
|---|---|---|---|
| Acknowledge after local disk only | Lost if the machine is lost | Low | Strong on the primary |
| Acknowledge after one replica stores it | Survives one machine loss | Higher | Strong if reads use the primary |
| Asynchronous replicas | Recent writes can be lost | Low | Replica reads may be stale |
Writing them down
Add these to the requirements document with measurable wording:
non_functional:
- id: NFR-3
quality: durability
text: A created short URL is never lost once creation has been acknowledged, including after the loss of one availability zone.
- id: NFR-4
quality: consistency
text: A deleted short URL stops redirecting everywhere within 5 seconds of the delete being acknowledged.Exercise
A service acknowledges a write as soon as the primary has it in memory, and copies it to a replica a moment later. The primary crashes in between. Which quality is violated, and what is one change that fixes it?
Show answer
Durability is violated, because an acknowledged write is lost. Acknowledge only after the write is on disk and stored by at least one replica, accepting higher write latency.
Exercise
Rewrite "search results should be consistent" as a testable requirement for a product search page where a seller edits a price.
Show answer
For example: "A price edit appears in search results within 10 seconds of the edit being acknowledged, and the seller sees the new price immediately on their own listing page." This names who reads, how soon, and what staleness is allowed.