Sub topic 13
Worked example: estimating a URL shortener
This chapter chains the earlier techniques into one estimate: traffic, storage, key length, bandwidth, cache, servers, and availability. It assumes you know the 86,400-second day, powers of two and ten, and the peak-factor idea from earlier chapters. It also assumes you know what a cache, a database shard, and a load balancer do.
A URL shortener turns a long URL into a short code such as sho.rt/aZ3k9Qx. When someone opens the short link, the service looks up the code and redirects the browser to the long URL.
Step 1: Fix the requirements and assumptions
An estimate is only as good as its stated assumptions. Write them down first, so the interviewer or your team can correct any of them.
| Assumption | Value | Reason |
|---|---|---|
| New short URLs per day | 100 million | Large public service |
| Read-to-write ratio | 10 : 1 | Links are created once and clicked many times |
| Record size | 500 bytes | Code, long URL, timestamps, owner, index overhead |
| Retention | 5 years | Product requirement |
| Peak factor | 2 | Peak QPS is twice the average |
| Replication | 3 copies | Durability and read scaling |
| App server capacity | 5,000 QPS | A simple lookup and redirect, mostly served from cache |
The 100 million figure and the 500-byte record are rounded on purpose. Round numbers keep the arithmetic checkable by hand. If a number turns out to be wrong by 2x, the design rarely changes.
Step 2: Traffic
Writes come first, because reads are derived from them.
- Average write QPS: 100,000,000 / 86,400 ≈ 1,157 per second.
- Average read QPS: 10 × 1,157 ≈ 11,574 per second.
- Peak write QPS: 2 × 1,157 ≈ 2,315 per second.
- Peak read QPS: 2 × 11,574 ≈ 23,148 per second.
Total peak load is about 25,500 requests per second. That is modest. A single well-tuned server can handle several thousand simple lookups per second, so the design is not driven by raw QPS. Storage and availability will matter more.
Step 3: Storage and key length
Storage is writes per day times record size times retention days.
- Total records: 100,000,000 × 365 × 5 = 182.5 billion.
- Raw storage: 182.5 billion × 500 bytes = 91.25 TB.
- With 3 replicas: about 274 TB.
The record count also sets the length of the short code. If the code uses base62 (a-z, A-Z, 0-9), each character carries 62 options.
- 62^6 ≈ 56.8 billion. This is less than 182.5 billion, so six characters are not enough.
- 62^7 ≈ 3.5 trillion. This covers the 5-year total about 19 times over.
So the code needs 7 characters. Because 3.5 trillion is far above 182.5 billion, random collisions stay rare, though you still need a uniqueness check on write.
Step 4: Bandwidth
Ingress is the data sent into the service when users create links. Egress is the data sent out when users follow links. A redirect response carries only HTTP headers and a Location URL, so assume about 500 bytes.
- Average ingress: 1,157 × 500 B ≈ 0.58 MB/s.
- Average egress: 11,574 × 500 B ≈ 5.8 MB/s, or about 46 Mbit/s.
- Peak egress: about 93 Mbit/s.
Network cards are rated in bits per second, so convert bytes to bits (multiply by 8) before comparing. Even at peak, one 1 Gbit/s link has plenty of room. Bandwidth is not a constraint here.
Step 5: Cache
Traffic follows a skewed pattern. A small share of links gets most of the clicks. Use the 80/20 rule: cache the hottest 20% of a day's read volume.
- Daily read volume: 1 billion reads × 500 B = 500 GB.
- Hot 20%: about 100 GB.
This is an upper bound. It treats every read as a different URL, while real traffic repeats the same popular links. A 100 GB working set fits on two 64 GB cache nodes. Add a third node if you want spare capacity or replication.
Memory lookups take around 100 nanoseconds, while a database round trip inside a data center takes closer to 0.5 to 1 millisecond. A cache therefore cuts redirect latency by orders of magnitude. It also cuts database load, which Step 6 uses.
Step 6: Servers
Application servers:
- Peak total QPS is about 25,463 (reads plus writes).
- One server handles 5,000 QPS at full load, so the minimum is 25,463 / 5,000 ≈ 5.1, which is 6 servers.
- Running servers at full load leaves no room for spikes or a failed machine. Plan for 50% utilization at peak: 25,463 / 2,500 ≈ 10.2, which is 11 servers.
Spread the 11 servers across at least three availability zones. Losing one zone then leaves roughly two thirds of capacity, which still covers the average load comfortably.
Database:
- With a 90% cache hit rate, only 10% of reads reach the database: about 2,300 reads per second at peak.
- Add 2,315 writes per second, and the database sees about 4,600 operations per second at peak.
- A sharded key-value store handles that load easily. The sizing question is capacity: 274 TB at roughly 10 TB per node is about 28 nodes.
The database tier is sized by storage, not by QPS. This is a common outcome and worth stating out loud.
Step 7: Availability
A redirect that fails is a broken link for the user. Suppose the target is 99.99% for redirects.
- Allowed downtime per year: 0.0001 × 525,600 minutes = 52.56 minutes.
- Per month: about 4.4 minutes.
At this level, a manual recovery process cannot meet the target. You need redundant servers across zones, health checks on the load balancer, automatic failover, and a cache that can serve reads while the database is degraded. Link creation can have a looser target, such as 99.9% (525.6 minutes per year), because a failed write is retried by the user.
Step 8: The whole chain in code
Keeping the estimate in a script makes each assumption a named variable. You can change one value and see every downstream number update.
import math
# Assumptions
new_urls_per_day = 100_000_000
read_write_ratio = 10
bytes_per_record = 500
retention_years = 5
replication = 3
peak_factor = 2
seconds_per_day = 86_400
server_qps = 5_000
target_utilization = 0.5
cache_node_gb = 64
db_node_tb = 10
# Traffic
write_qps = new_urls_per_day / seconds_per_day
read_qps = write_qps * read_write_ratio
peak_write = write_qps * peak_factor
peak_read = read_qps * peak_factor
peak_total = peak_write + peak_read
print(f"avg write QPS: {write_qps:,.0f}") # 1,157
print(f"avg read QPS: {read_qps:,.0f}") # 11,574
print(f"peak write QPS: {peak_write:,.0f}") # 2,315
print(f"peak read QPS: {peak_read:,.0f}") # 23,148
# Storage and key length
total_urls = new_urls_per_day * 365 * retention_years
raw_tb = total_urls * bytes_per_record / 1e12
replicated_tb = raw_tb * replication
key_length = math.ceil(math.log(total_urls, 62))
print(f"total URLs (billions): {total_urls / 1e9:.1f}") # 182.5
print(f"raw storage TB: {raw_tb:.2f}") # 91.25
print(f"replicated TB: {replicated_tb:.2f}") # 273.75
print(f"key length: {key_length}") # 7
# Bandwidth
ingress_mb_s = write_qps * bytes_per_record / 1e6
egress_mb_s = read_qps * bytes_per_record / 1e6
peak_egress_mbit = peak_read * bytes_per_record * 8 / 1e6
print(f"avg ingress MB/s: {ingress_mb_s:.2f}") # 0.58
print(f"avg egress MB/s: {egress_mb_s:.2f}") # 5.79
print(f"peak egress Mbit/s: {peak_egress_mbit:.1f}") # 92.6
# Cache (hottest 20% of one day of reads)
daily_read_gb = read_qps * seconds_per_day * bytes_per_record / 1e9
cache_gb = daily_read_gb * 0.2
cache_nodes = math.ceil(cache_gb / cache_node_gb)
print(f"cache GB: {cache_gb:.0f}") # 100
print(f"cache nodes: {cache_nodes}") # 2
# Servers
app_servers = math.ceil(peak_total / (server_qps * target_utilization))
db_nodes = math.ceil(replicated_tb / db_node_tb)
print(f"app servers: {app_servers}") # 11
print(f"db nodes: {db_nodes}") # 28Summary of results
| Resource | Estimate |
|---|---|
| Average / peak read QPS | about 11.6K / 23.1K |
| Average / peak write QPS | about 1.2K / 2.3K |
| Storage, raw / replicated | 91 TB / 274 TB |
| Short code length | 7 base62 characters |
| Peak egress | about 93 Mbit/s |
| Cache | about 100 GB, 2 to 3 nodes |
| App servers | 11 |
| Database nodes | about 28 |
| Downtime at 99.99% | about 52.6 minutes per year |
Checking the result
Run the usual sanity checks before trusting these numbers.
- Compare against known systems. About 25K requests per second at peak is large for one machine but small for a fleet. That matches a service that is large by user count but simple per request.
- Check units. The 91.25 TB figure uses decimal units (1 TB = 10^12 bytes). Binary units would give about 83 TiB. The difference is under 10%, which is below the noise in the assumptions.
- Identify the dominant term. Storage drives the database tier, and peak QPS with headroom drives the app tier. Bandwidth drives nothing.
- Vary one assumption. If the read ratio were 100 : 1, peak reads would be near 230K QPS, and the cache hit rate would become the key design factor.
Exercise
The product team raises the read-to-write ratio from 10:1 to 50:1 and keeps 100 million new URLs per day. What are the new average and peak read QPS, and does the 11-server estimate still hold if each server handles 5,000 QPS at 50% target utilization?
Show answer
Average write QPS stays at about 1,157, so average read QPS is 50 × 1,157 ≈ 57,870. Peak read QPS is about 115,741. Peak total is about 115,741 + 2,315 = 118,056. Divided by 2,500 effective QPS per server, that is about 47.2, so 48 servers. The 11-server estimate no longer holds.
Exercise
The service must store links for 10 years instead of 5. By how much does the total record count grow, and is a 7-character base62 code still enough?
Show answer
Total records double to 365 billion (100 million × 365 × 10). A 7-character base62 code gives about 3.5 trillion combinations, which is nearly 10 times the 365 billion needed. Seven characters are still enough.