Sub topic 2
Estimating traffic, storage, and code length
This chapter assumes you know basic HTTP, can read simple Python, and know that base62 uses 62 symbols per digit. You will turn the requirements from the previous chapter into numbers: requests per second, bytes on disk, memory for a cache, and the number of characters in a short code. These numbers are rough. Their job is to tell you which design choices matter and which don't.
Why estimate at all
Estimates settle questions that would otherwise be opinion:
- Whether one database server can hold the data or you must shard.
- Whether the read path needs a cache.
- Whether a 6-character code is enough or you need 7 or 8.
- Whether collisions will be rare or constant.
You are looking for order of magnitude. If the answer is 40 writes per second, it makes no difference whether the true figure is 35 or 45. If it were 40,000, the design would be different.
State your assumptions first
Write every assumption down before you calculate anything. A reviewer can then challenge an input without redoing your arithmetic. For this chapter:
| Assumption | Value | Reasoning |
|---|---|---|
| New short URLs per month | 100 million | A large public service. |
| Read to write ratio | 100 : 1 | Links are created once and clicked many times. |
| Retention | 5 years | Links rarely expire by default. |
| Average record size | 500 bytes | Code, long URL, timestamps, and index overhead. |
| Peak to average traffic | 3 : 1 | Traffic is bursty. |
| Cache coverage | 20% of daily reads | Popular links dominate traffic. |
Change any of these and the results scale in a predictable way. A smaller service at 1 million new URLs per month needs a single database and probably no sharding.
Traffic: requests per second
A month has about 30 × 24 × 3,600 = 2,592,000 seconds. Round it to 2.6 million.
- Writes: 100,000,000 / 2,592,000 ≈ 39 per second.
- Reads: 100 times the writes, so 10 billion per month ≈ 3,860 per second.
- Peak reads: 3 × 3,860 ≈ 11,600 per second.
Writes are small. Any relational database handles 39 inserts per second without effort. The read path at about 11,600 requests per second at peak is where the work is. This number is the reason the design includes a cache and read replicas.
Storage: how much data
Total records over the retention period:
- 100,000,000 per month × 12 months × 5 years = 6 billion records.
At 500 bytes each:
- 6,000,000,000 × 500 B = 3 × 10^12 B = 3 TB.
A single modern server can hold 3 TB on disk. So storage alone does not force sharding at this scale. Other limits might: write throughput, read throughput, backup time, and the working set that fits in memory. Plan for headroom, because indexes, replication, and backups multiply the raw figure. A replication factor of 3 turns 3 TB of data into 9 TB of disk across the cluster.
The 500-byte figure deserves a check. The short code is 7 bytes. The long URL averages around 100 to 200 bytes in practice. Timestamps are 8 bytes each. The primary key index and row overhead add the rest. If you stored click events in the same table, the figure would grow quickly, which is why analytics usually lives elsewhere (a later chapter).
Code length: how many characters
A base62 code of length L has 62^L possible values.
| Length | Combinations | Enough for 6 billion records? |
|---|---|---|
| 5 | 916,132,832 (about 0.9 billion) | No |
| 6 | 56,800,235,584 (about 57 billion) | Yes, with about 9x headroom |
| 7 | 3,521,614,606,208 (about 3.5 trillion) | Yes, with about 580x headroom |
| 8 | 218,340,105,584,896 (about 218 trillion) | Far more than needed |
A counter-based generator uses codes one after another, so 6 characters would last the full 5 years at this rate. Hash-based and random generators waste part of the space, because they pick codes without knowing which are free. Those strategies need a much larger space to keep collisions rare. That is why 7 characters is the common choice.
Collisions and the size of the space
With a random or hash-based generator, the chance that a new code is already taken equals the fraction of the space that is full. After 6 billion records:
- 6-character space: 6 × 10^9 / 5.68 × 10^10 ≈ 10.6% of inserts would collide at the end.
- 7-character space: 6 × 10^9 / 3.52 × 10^12 ≈ 0.17% of inserts would collide at the end.
A 10% collision rate is workable but wasteful, because each collision costs a retry and a database round trip. A 0.17% rate is negligible. Collision handling is the topic of a later chapter. For now, treat the fill fraction as the key figure when you choose a length.
Cache: how much memory
The read path is the hot path, so size the cache for it.
- Reads per day: 3,860 × 86,400 ≈ 333 million.
- Cache the 20% of requests that hit the hottest links: about 67 million entries.
- Each entry holds the short code and the long URL, about 200 bytes with overhead.
- Memory: 67,000,000 × 200 B ≈ 13 GB.
13 GB fits in the memory of one cache server, and a small cluster gives you redundancy. The 20% figure is a heuristic, not a law. Real traffic is often more skewed, which makes the cache more effective, so measure hit rate in production and adjust.
Bandwidth
A redirect response is small: status line, a Location header, and a few other headers. Assume 500 bytes.
- Outgoing: 11,600 requests per second × 500 B ≈ 5.8 MB/s, or about 46 Mbit/s at peak.
- Incoming: requests are also small, so total network is far below the limit of a single 1 Gbit/s link.
Bandwidth is not the constraint in this design. Request handling (connection setup, TLS, lookups) is. Checking bandwidth takes 30 seconds and rules out a whole class of worries.
Put it in a script
Keeping the estimate in code makes it easy to rerun with different assumptions. This script is complete and runs as shown.
SECONDS_PER_MONTH = 30 * 24 * 3600
SECONDS_PER_DAY = 24 * 3600
new_urls_per_month = 100_000_000
read_write_ratio = 100
years = 5
record_bytes = 500
peak_factor = 3
cache_fraction = 0.20
cache_entry_bytes = 200
headroom = 10 # want the space to be at most 10% full
def min_code_length(records: int, headroom: int = 1) -> int:
needed = records * headroom
length = 1
while 62 ** length < needed:
length += 1
return length
write_qps = new_urls_per_month / SECONDS_PER_MONTH
read_qps = write_qps * read_write_ratio
peak_read_qps = read_qps * peak_factor
total_records = new_urls_per_month * 12 * years
storage_tb = total_records * record_bytes / 1e12
reads_per_day = read_qps * SECONDS_PER_DAY
cache_entries = reads_per_day * cache_fraction
cache_gb = cache_entries * cache_entry_bytes / 1e9
print(f"writes/s: {write_qps:,.0f}")
print(f"reads/s: {read_qps:,.0f}")
print(f"peak reads/s: {peak_read_qps:,.0f}")
print(f"total records: {total_records:,}")
print(f"storage: {storage_tb:.1f} TB")
print(f"cache: {cache_gb:.1f} GB")
print(f"min length: {min_code_length(total_records)}")
print(f"length (10x): {min_code_length(total_records, headroom)}")Expected output:
writes/s: 39
reads/s: 3,858
peak reads/s: 11,574
total records: 6,000,000,000
storage: 3.0 TB
cache: 13.3 GB
min length: 6
length (10x): 7The last two lines show the decision: 6 characters hold the data, but with headroom for hash and random strategies you need 7.
Common errors
- Mixing units. Confusing bits and bytes, or TB and TiB, gives errors of 8x or 10%. Pick one unit and state it.
- Using the average as the target. Systems fail at peak, not at the mean. Always compute a peak figure.
- Forgetting overhead. Indexes, replicas, and backups multiply raw storage. Quote both raw and provisioned numbers.
- False precision. Reporting 3,858.02 reads per second implies accuracy you do not have. Round to 3,900 or "about 4,000".
- Ignoring growth. A service that doubles each year needs a plan beyond year 5. Say what growth you assumed.
- Sizing the code length for today's data only. Short codes are part of every public URL. Changing the length later is painful, so pick with margin.
What the numbers tell us
- Writes are about 40 per second, so a single primary database handles them.
- Reads peak near 11,600 per second, so a cache and read replicas are justified.
- Storage is about 3 TB raw, which fits on one machine but needs replication for safety.
- A cache of about 13 GB fits in memory.
- A 7-character base62 code gives ample room.
These results feed directly into the next chapters: the API, the code generation strategy, and the data model.
Exercise
Suppose the service creates 500 million new URLs per month and keeps them for 10 years, with the same 100:1 read to write ratio and 500 bytes per record. Compute the total number of records, the raw storage, and the shortest code length that leaves the space at most 10% full.
Show answer
Total records = 500,000,000 × 12 × 10 = 60 billion. Raw storage = 60 × 10^9 × 500 B = 30 × 10^12 B = 30 TB. For 10% fill the space must hold 600 billion codes. 62^6 is about 56.8 billion, which is too small. 62^7 is about 3.52 trillion, which is enough. So the code length is 7. Note that 30 TB with replication is a strong reason to consider sharding.
Exercise
Your estimate gives 4,000 average reads per second. A colleague says to provision for exactly 4,000. What is wrong with that, and what would you do?
Show answer
Traffic is bursty, so the peak is higher than the average, and a system sized for the mean fails during bursts. Choose a peak factor (for example 3), provision for about 12,000 reads per second, and check it against measured peaks once the service has real traffic.