Sub topic 2
Estimating Traffic, Storage, and Code Length
This chapter assumes you know basic HTTP, can read simple Python, and have met the idea of a base-N number system. We turn the requirements into numbers. Those numbers decide how long a short code must be, how much data we store, and whether one server could handle the load.
Why estimate first
A back-of-the-envelope estimate is a rough calculation from stated assumptions. It is not a measurement. Its purpose is to find the order of magnitude: is this 40 requests per second or 40,000? That answer changes the design. Always write your assumptions down so a reader can change them and redo the math.
Assumptions
We use these round numbers:
- 100 million new short URLs per month.
- 100 redirects for every URL created (a read-to-write ratio of 100:1).
- Records are kept for 5 years.
- Each record takes about 500 bytes, covering the code, the long URL, timestamps, and index overhead.
- Peak traffic is 2 times the average.
Traffic
A month has about 2.6 million seconds (30 × 24 × 3600 = 2,592,000).
- Writes: 100,000,000 / 2,592,000 is about 39 per second.
- Reads: 100 times that, about 3,900 per second.
- Peak: about 80 writes and 7,700 reads per second.
Roughly 8,000 reads per second is easy for a cached service on a few machines. The service is read-heavy, so the read path is the part to optimize. Writes are a small trickle.
Storage
Over 5 years we create 100 million × 12 × 5 = 6 billion URLs. At 500 bytes each, that is 3 TB. This fits on one large disk, but it is large enough that we will need replication and possibly sharding later.
Code length
The code length depends on how many URLs we must ever distinguish. With base62 (digits plus upper and lower case letters), the number of codes of length n is 62^n:
| Length | Possible codes |
|---|---|
| 5 | about 916 million |
| 6 | about 56.8 billion |
| 7 | about 3.5 trillion |
Six billion URLs fit in 6 characters, but only just. If codes are random or hash-based, the table would be about 10% full, and roughly one new code in ten would collide with an existing one. At 7 characters the table is about 0.17% full, so collisions are rare. We choose 7.
Cache size
Traffic follows a skewed pattern: a small share of links gets most of the clicks. Assume 20% of the URLs read in a day produce most of the reads. A day has about 333 million reads, and 20% of that is about 67 million URLs. At 500 bytes each, that is about 33 GB. This fits in the memory of one cache server.
The calculation in code
SECONDS_PER_MONTH = 30 * 24 * 3600
new_urls_per_month = 100_000_000
read_write_ratio = 100
years = 5
bytes_per_record = 500
hot_fraction = 0.20
def min_code_length(total_urls: int, max_fill: float = 0.01) -> int:
"""Smallest base62 length that keeps the code space under max_fill full."""
n = 1
while total_urls / 62**n > max_fill:
n += 1
return n
writes_per_sec = new_urls_per_month / SECONDS_PER_MONTH
reads_per_sec = writes_per_sec * read_write_ratio
total_urls = new_urls_per_month * 12 * years
storage_tb = total_urls * bytes_per_record / 1e12
reads_per_day = new_urls_per_month * read_write_ratio / 30
cache_gb = reads_per_day * hot_fraction * bytes_per_record / 1e9
print(f"writes/s: {writes_per_sec:.1f}")
print(f"reads/s: {reads_per_sec:.1f}")
print(f"total URLs: {total_urls:,}")
print(f"storage TB: {storage_tb:.1f}")
print(f"code length: {min_code_length(total_urls)}")
print(f"cache GB: {cache_gb:.1f}")Running it prints:
writes/s: 38.6
reads/s: 3858.0
total URLs: 6,000,000,000
storage TB: 3.0
code length: 7
cache GB: 33.3How the numbers connect
Common errors
- Mixing units, such as dividing by days instead of seconds.
- Forgetting peak traffic and sizing for the average.
- Choosing the code length from the URL count alone, without leaving headroom for collisions.
- Treating the result as exact. These are estimates, so round them and state the assumptions.
Exercise
Suppose traffic doubles to 200 million new URLs per month, with the same ratio and retention. How many total URLs will you store after 5 years, and is a 7-character base62 code still acceptable if you want to stay under 1% full?
Show answer
Total URLs = 200,000,000 × 12 × 5 = 12 billion. The 7-character space holds about 3.52 trillion codes, so the fill is 12e9 / 3.52e12, about 0.34%. That is under 1%, so 7 characters is still acceptable. Storage doubles to about 6 TB at 500 bytes per record.