How do URL shortener calculate the URL key? How do they work?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
URL shorteners are services that transform long URLs into condensed versions, reducing the number of characters required to represent the URL. They play a critical role in managing links for social media, SMS, print media, and other platforms with limited character space. The key component of a URL shortener is the URL key, which is appended to the base URL of the shortening service. This article explores how URL shorteners calculate this URL key and how these services function, including technical examples.
How URL Shorteners Work
To understand URL shorteners, it's crucial to discuss their core functionalities:
- Mapping Long URLs to Short URLs:
- URL shorteners map long-form URLs to short URLs using a unique identifier, or key.
- This key is generated algorithmically and appended to the base URL of the shortening service.
- Redirecting to the Original URL:
- When the short URL is accessed, the server retrieves the stored long URL and performs a redirection, typically an HTTP 301 Redirect, to guide the user to the original content.
Calculating the URL Key
The URL key is typically an alphanumeric string that uniquely identifies a long URL in the database. Here's how the calculation and assignment generally work:
- Unique Identifier Generation:
- The key must be unique to prevent collisions, meaning that no two long URLs should map to the same key.
- After storing a URL, a unique key is generated, often using sequential IDs, hash functions, or random generators.
- Cryptographic Hash Functions:
- Some URL shorteners utilize cryptographic hash functions, like MD5 or SHA-1, to generate a fixed-size string from a URL.
- However, since hashes can result in collisions (two URLs generating the same hash), an extra data-check might be necessary.
- Encoding Sequential IDs:
- SQL databases often use auto-incrementing primary keys, which can then be encoded using Base62 (or any character set) to produce alphanumeric strings.
- For example, a sequential ID "1500" could be encoded to "5G" in Base62. Base62 includes the characters [0-9, a-z, A-Z].
- Random Key Generation:
- Random generators create a key by randomly selecting characters from the defined character set.
- Adequate randomness ensures low probability of collision.
Example: Base62 Encoding
Sequential IDs are often encoded using Base62 encoding for URL keys. Here's an example of how Base62 encoding works:
With this function, a sequential database ID can be encoded into a Base62 string, providing a compact URL key.
URL Key Characteristics
- Length: Typically between 6 to 8 characters, balancing uniqueness and brevity.
- Character Set: Often Base62 for robustness and low collision probability.
- Collision Handling: Strategies often include re-generation or appending additional characters if a collision is detected.
Advantages of URL Shorteners
- Link Management: Enables ease of sharing and tracking.
- Aesthetic Appeal: Removes unwieldy strings, improving visual appeal.
- Analytics: Many services provide click analysis and geographic data.
A Brief Comparison of URL Shortening Methods
| Method | Collision Probability | Characteristics |
| Sequential ID | Low | Predictable, requires storage for mapping |
| Hashing | Moderate | Quick generation, potential collisions |
| Random Generation | Variable | Unpredictable, risk of collisions without verification |
Conclusion
URL shorteners provide an essential service by making links more manageable and presentable across various platforms. The calculation of the URL key is a crucial process that involves unique identifier generation considering collision possibilities and efficient redirection mechanisms. Understanding the inner workings not only enhances user experience but also provides insights into efficient data management and network optimization.

