Collision Probability and Namespace Size
You'll learn: How to calculate the number of unique short URLs available and assess the likelihood of collisions as your URL shortener scales.
The Math Behind Short URL Capacity
When building a URL shortener, you need to understand your namespace size—the total number of unique short codes possible given your encoding scheme.
If you use alphanumeric characters (a-z, A-Z, 0-9), that's 62 possible characters. For a short code of length n, your namespace size is 62^n:
- Length 6: 62^6 = ~56.8 billion unique URLs
- Length 7: 62^7 = ~3.5 trillion unique URLs
- Length 8: 62^8 = ~218 trillion unique URLs
Collision Probability
As you generate more short URLs, the chance of randomly creating a duplicate increases—similar to the birthday paradox. The probability isn't linear; it accelerates as you fill the namespace.
A rough guideline: when you've used √N of your namespace (where N is total capacity), collision probability becomes significant (~50%). For a 6-character system with 56.8 billion slots, expect noticeable collisions around 238,000 URLs.
Practical Implications
With earlier estimates showing millions of daily URL creations:
- 6 characters might exhaust quickly at scale
- 7 characters provides comfortable headroom for years
- 8 characters handles massive scale but looks longer (user perception tradeoff)
You can either pre-generate collision-free IDs or implement collision-detection-and-retry logic—but knowing your namespace capacity informs which strategy makes sense.
Key Takeaway: Your short URL length determines namespace size (62^n), and collision probability grows with the square root of utilization—choose length based on expected scale, not just aesthetics.