Sub topic 16
How requirements drive architecture decisions
This chapter assumes you can write measurable non-functional requirements (SLOs and percentiles), do the back-of-envelope load arithmetic from the earlier chapters, and read short Python snippets. Here you will turn those requirements into architecture decisions and learn to defend each decision with a requirement.
Requirements are the input, architecture is the answer
An architecture decision is a choice that is expensive to reverse. Examples are the datastore type, whether a cache sits in front of it, whether writes are replicated synchronously, and whether the service is one deployable unit or several. Each choice should answer a specific requirement. If you can't name the requirement behind a component, you may not need that component.
Functional and non-functional requirements shape the design in different ways:
- Functional requirements decide which components and interfaces exist. "A user can delete a short URL" means you need a delete operation, and a store that supports removal.
- Non-functional requirements decide how those components are built and deployed. "99% of redirects under 100 ms" decides whether the redirect path can hit a disk-backed database on every request.
Two systems with identical functional requirements can have very different architectures because their quality targets differ.
The derivation loop
The process has four steps and you will repeat it.
- Pick one requirement with a number attached.
- Ask what would make the system miss that number. This is the threat to the requirement.
- Choose the smallest mechanism that removes the threat.
- Record the decision, the requirement it serves, and what it costs.
Step 4 matters most. A decision with no recorded cost can't be compared against competing requirements later.
From requirement to mechanism
Take the URL shortener requirements from earlier chapters and derive decisions one at a time.
Latency: NFR-1
The target is 99% of redirects under 100 ms. A redirect is a key lookup, which is cheap, but the p99 is dominated by slow outliers: cold reads from disk, network hops, and garbage collection pauses. The threat is the slow tail, not the average.
Mechanisms that address it:
- A cache in front of the database, so most lookups avoid disk.
- Serving redirects from regions near users, which cuts network time.
- A short, bounded path on the redirect route, with no synchronous calls to other services.
The cost is stale data after a deletion (FR-3) and extra memory. A deleted URL may keep redirecting until the cache entry expires or is invalidated.
Availability: NFR-2
At 99.9% over 30 days, the downtime budget is about 43.2 minutes. A single server that restarts for each deploy and sometimes crashes can use that up quickly. The threat is any single point of failure on the redirect path.
Mechanisms: at least two instances in separate failure domains (for example, separate availability zones), a load balancer with health checks, and rolling deploys so no deploy takes the service down.
The cost is more infrastructure and the need to keep instances stateless or their state replicated.
Durability: NFR-3
"Never lost once creation has been acknowledged" is a statement about the moment of acknowledgment. The threat is acknowledging a write that exists on only one machine. The mechanism is to replicate the write to a second node, or write it to a durable log, before returning success.
The cost is higher write latency. This is acceptable here because creation is rare compared with redirects, and NFR-1 applies only to redirects.
Functional requirements still matter
FR-2 (redirect) and FR-1 (create) have very different traffic. Creation is rare and redirects are frequent, so a read-heavy workload justifies separating the read path from the write path. The functional split by operation becomes an architectural split by load profile.
Sizing with the capacity numbers
Capacity estimates convert a requirement into a count of machines. Suppose the earlier calculation gave a peak of about 6,944 requests per second. You assume one instance handles 1,000 requests per second, and you want to run at no more than 60% utilization at peak so there is headroom for spikes. You also want to survive losing one instance (N+1).
import math
peak_rps = 6944
per_instance_rps = 1000
target_utilization = 0.6
usable_rps = per_instance_rps * target_utilization
needed = math.ceil(peak_rps / usable_rps)
with_spare = needed + 1
print(needed) # 12
print(with_spare) # 13The 1,000 requests per second figure is an assumption. Write it down as one, and replace it with a load-test result when you have one.
Recording decisions with their reasons
A lightweight decision record links each decision to the requirement behind it. This lets a later reader check whether the decision still holds when a requirement changes.
decisions:
- id: AD-1
decision: Cache short-URL lookups in memory in front of the database.
serves: [NFR-1]
cost: Deleted URLs may keep redirecting until the entry expires.
revisit_if: Deletion must take effect immediately.
- id: AD-2
decision: Run at least 2 stateless instances in separate availability zones.
serves: [NFR-2]
cost: Higher infrastructure spend and a load balancer to operate.
revisit_if: The availability target is relaxed below 99.5%.
- id: AD-3
decision: Acknowledge creation only after the write is stored on two nodes.
serves: [NFR-3]
cost: Higher p99 latency on create.
revisit_if: Create latency becomes a stated requirement.The revisit_if field names the change in requirements that would invalidate the decision.
When requirements collide
AD-1 and FR-3 are in tension. The cache makes redirects fast but delays the effect of a delete. You have three options:
| Option | What it does | What it costs |
|---|---|---|
| Short cache expiry | Deleted URLs stop redirecting within seconds | More database reads, higher chance of missing NFR-1 |
| Invalidate on delete | Removes the cache entry as part of the delete | Extra mechanism, and a failure if invalidation is lost |
| Accept staleness | Do nothing | A delete is not immediate, which may break a stakeholder expectation |
Go back to the stakeholders to choose. If the business only needs a deleted link to stop working "soon", a one-minute expiry is enough. If a deleted link may contain something legally harmful, you need invalidation. The requirement for the delete delay was missing, and finding it is a result of this process.
Failure modes in this step
- Over-building: adding a message queue or a second datastore because it is common practice, with no requirement behind it. Every component adds operating work and failure modes.
- Under-building: ignoring the tail. Designing for average latency and then missing the p99.
- Solving for a number you invented: sizing for a peak that nobody confirmed. State assumptions and keep them visible.
- Hiding the cost: recording only benefits. The conflict between AD-1 and FR-3 shows why costs must be written down.
Exercise
The URL shortener adds a new requirement: "A created short URL must be readable from any region within 1 second of creation." Name one threat this poses to the existing design, one mechanism that addresses it, and one cost of that mechanism.
Show answer
The threat is replication lag. A URL created in one region may not yet exist in the store that serves another region, so a redirect there returns "not found". One mechanism is to replicate each new URL to all regions before acknowledging creation, or to have a redirect that misses locally fall back to a read from the originating region. The cost is higher create latency with synchronous cross-region replication, or higher latency and extra load on the originating region for fallback reads. Either way, AD-3 should be revised and its cost field updated.
Exercise
Using the sizing code, how many instances are needed if the peak is 6,944 requests per second, each instance handles 1,000 requests per second, and you want to run at no more than 50% utilization plus one spare?
Show answer
Usable capacity per instance is 500 requests per second. 6,944 / 500 = 13.9, which rounds up to 14. Adding one spare gives 15 instances.