Sub topic 18
Worked example: requirements for a URL shortener
This chapter applies the whole requirements workflow to one system. It assumes you can tell functional from non-functional requirements and know what an SLI and an SLO are. You should also be able to read short Python snippets and have a basic picture of caches and databases. We go from a vague request to a numbered requirements document, then to a load estimate, then to the first architecture decisions.
The starting request
A product manager says: "We want a service like bit.ly. Users paste a long link and get a short one. It must be fast and it can't go down."
Two phrases here are unusable as written: "fast" and "can't go down". Neither can be tested, and neither says who cares or how much. The functional part is also thin. It doesn't say who can delete links or what happens to a deleted link.
Step 1: Ask questions and record the answers
Elicitation with the product manager and an engineering lead produces these answers:
- Anyone can create a link. Only the creator can delete it.
- Redirects are the main workload. Creation is rare by comparison.
- Users notice slow redirects, because the redirect delays the page they actually want.
- A lost link is worse than a briefly unavailable one, because printed or shared links stay in circulation.
- Analytics, custom domains and link expiry are wanted eventually, but not in the first release.
The last answer matters for scope. Recording what is out of scope keeps later chapters of the project from silently growing.
Step 2: Write the requirements document
Each requirement gets an ID, so tests, designs and reviews can refer to it. Non-functional requirements name a quality, a number, a measurement point and a time window.
service: url-shortener
functional:
- id: FR-1
text: A user can submit a long URL and receive a short URL.
- id: FR-2
text: Requesting a short URL redirects to the original long URL.
- id: FR-3
text: A user can delete a short URL they created.
non_functional:
- id: NFR-1
quality: latency
text: 99% of redirects complete in under 100 ms, measured at the service edge over 30 days.
- id: NFR-2
quality: availability
text: Redirects succeed for 99.9% of requests over a 30-day window.
- id: NFR-3
quality: durability
text: A created short URL is never lost once creation has been acknowledged.
out_of_scope:
- Click analytics
- Custom domainsThe three non-functional entries correspond to the three complaints from Step 1:
- "Fast" became NFR-1, a percentile target with a measurement point.
- "Can't go down" became NFR-2, a ratio over a window.
- "A lost link is worse" became NFR-3, a durability guarantee tied to the moment of acknowledgment.
The unstated requirement "a deleted link should stop working" is still missing. Add it as a consistency requirement, which the next sections justify:
- NFR-4 (consistency): after a deletion is acknowledged, the short URL stops redirecting within 60 seconds.
Step 3: Turn targets into budgets
NFR-2 sounds strict, but a number makes it concrete. The downtime budget is the fraction of the window you are allowed to fail.
def downtime_budget_minutes(availability, days=30):
"""Minutes of allowed downtime for an availability target over a window."""
return (1 - availability) * days * 24 * 60
print(round(downtime_budget_minutes(0.999), 1)) # 43.2
print(round(downtime_budget_minutes(0.9999), 1)) # 4.3At 99.9%, the team can absorb about 43 minutes of failure per month. At 99.99%, the budget is about 4 minutes, which is shorter than a typical deploy-and-rollback cycle. That is a strong reason to ask whether the extra nine is worth its cost. Here the stakeholders accept 99.9%.
Latency needs the same care. A mean can hide the slow requests that NFR-1 is written to catch:
import math
def percentile(samples, p):
"""Nearest-rank percentile: the smallest value with at least p% of samples at or below it."""
ordered = sorted(samples)
rank = math.ceil(p / 100 * len(ordered))
return ordered[rank - 1]
latencies_ms = [12, 15, 14, 18, 20, 22, 25, 30, 40, 250]
print(percentile(latencies_ms, 50)) # 20
print(percentile(latencies_ms, 99)) # 250The median is 20 ms, so the service looks healthy. The 99th percentile is 250 ms, so NFR-1 would be violated. With only ten samples, p99 is just the maximum. A real check needs many samples over the whole 30-day window.
Step 4: Estimate load and storage
Capacity numbers need assumptions, and each assumption belongs in the requirements document. Suppose the product team expects 10 million new links per month, 100 redirects per created link, a peak of three times the average, 500 bytes per stored record and five years of retention.
new_urls_per_month = 10_000_000
read_write_ratio = 100
peak_factor = 3
retention_years = 5
bytes_per_record = 500
seconds_per_month = 30 * 24 * 3600
write_rps = new_urls_per_month / seconds_per_month
redirects_per_month = new_urls_per_month * read_write_ratio
average_read_rps = redirects_per_month / seconds_per_month
peak_read_rps = average_read_rps * peak_factor
total_records = new_urls_per_month * 12 * retention_years
storage_gb = total_records * bytes_per_record / 1e9
print(round(write_rps)) # 4
print(round(average_read_rps)) # 386
print(round(peak_read_rps)) # 1157
print(total_records) # 600000000
print(round(storage_gb)) # 300
print(62 ** 7) # 3521614606208Reading the results:
- Writes are about 4 per second. Writes are not a scaling problem.
- Peak reads are about 1,157 per second. One well-provisioned database could handle that, but NFR-1 and NFR-2 still push toward a cache.
- Storage is about 300 GB over five years. This fits on a single modern disk, so storage size alone does not force sharding.
- Seven characters from a 62-symbol alphabet give about 3.5 trillion codes, far more than the 600 million needed.
Compare this with the generic example in the earlier capacity chapter, where 10 million daily users produced about 6,900 peak requests per second. The estimate here is much smaller. Estimates are not automatically large, and a small number is a useful result.
Step 5: From requirements to decisions
Each requirement either forces a decision or rules out an option. The diagram shows how the main requirements lead to design choices.
Two of these decisions interact. Caching helps NFR-1, but a cached redirect keeps working after the link is deleted. NFR-4 limits that: a time-to-live of 60 seconds or less on cache entries caps the staleness. The alternative is to invalidate the cache entry on delete, which is more complex and can fail. The requirement says 60 seconds, so the simpler design is allowed.
| Requirement | Decision | Cost accepted |
|---|---|---|
| NFR-1 | Cache redirects | Stale entries after delete |
| NFR-2 | Multiple redirect servers behind a load balancer | More infrastructure to run |
| NFR-3 | Synchronous replication on create | Slower creation |
| NFR-4 | Cache TTL of 60 s or less | More cache misses than a long TTL |
Step 6: Check the document
Review each requirement against three questions:
- Is it testable? NFR-1 can be checked from edge latency logs, NFR-2 from request success counts, and NFR-4 by deleting a link and polling until it stops redirecting.
- Is it consistent? NFR-1 and NFR-4 together limit the cache design, but they do not contradict each other.
- Is it complete? Creation latency is the gap found above, and the document should be updated.
Exercise
The team raises NFR-2 from 99.9% to 99.95% over 30 days. How many minutes of downtime does the new target allow, and is that more or less than half of the old budget?
Show answer
(1 - 0.9995) 30 24 * 60 = 21.6 minutes. The old budget was 43.2 minutes, so the new one is exactly half. Doubling the failure-free fraction from 99.9% to 99.95% halves the budget, which makes it harder to absorb a single slow deploy or a regional outage.
Exercise
A stakeholder asks to add "users can see how many times their link was clicked". Which section of the requirements document should you check first, and what must you do if you accept the request?
Show answer
Check outofscope, where "Click analytics" is listed. If you accept the request, remove it from outofscope, add a numbered functional requirement, and re-run the load estimate. Recording a click on each redirect adds a write to the hot read path, which could affect NFR-1 and the 4 writes per second estimate.