Design a URL Shortener
Learn how URL shorteners work, from storing long URLs and generating unique short codes using Base62 encoding to handling redirects and optimizing for existing
In depth
A URL shortener transforms long, complex web addresses into concise, shareable links. This process is crucial for improving readability, simplifying sharing across platforms with character limits, and often for tracking link performance.
How URL Shorteners Work
At its core, a URL shortener acts like a dictionary, mapping a short key to a much longer URL. When a user requests to shorten a URL, the system stores the original long URL and assigns it a unique identifier.
Storing and Identifying Links
The first step involves saving the provided long URL into a database. Each stored URL is given a unique numerical ID. This ID serves as the primary reference for the original URL within the system.
Generating Short Codes with Base62
To create a compact, human-readable short code from the numerical ID, Base62 encoding is commonly used. Base62 uses 62 characters (0-9, a-z, A-Z) to represent numbers, allowing for much shorter strings compared to standard decimal or hexadecimal representations for the same numerical ID. For example, a database ID of `500` could be encoded into a short key like `88` using Base62.
The Shortening Process
When a user submits a long URL to the shortener (e.g., via a `POST` request), the server receives it, stores it in the database, and assigns a unique numerical ID. This ID is then converted into a short key using Base62 encoding. The server responds with the newly generated short URL, such as `short.ly/88`.
Handling Redirection
When a user accesses a short URL (e.g., `GET /88`), the shortener server looks up the corresponding long URL using the short key. It then issues an HTTP `301 Redirect` response, directing the user's browser to the original long URL. This process is seamless and typically unnoticeable to the end-user.
Optimizing for Existing Links
Before creating a new entry, an efficient URL shortener will check if the submitted long URL has already been shortened. If an existing mapping is found, the system can return the previously generated short URL, avoiding duplicate entries and ensuring consistency.
Core Steps of a URL Shortener
1. User submits LONG_URL to the shortener service.
2. Service checks if LONG_URL already exists in the database.
3. If it exists, return the associated SHORT_URL.
4. If not, store LONG_URL in the database, assigning a unique NUMERIC_ID.
5. Encode NUMERIC_ID into a short KEY using Base62.
6. Construct SHORT_URL using the generated KEY.
7. Return SHORT_URL to the user.
8. When a user accesses SHORT_URL:
a. Extract KEY from SHORT_URL.
b. Look up LONG_URL in the database using KEY.
c. Issue a 301 Redirect to LONG_URL.Key Takeaways
- URL shorteners map long URLs to short, unique keys.
- Databases store the original URLs and their corresponding IDs.
- Base62 encoding efficiently converts numerical IDs into compact short keys.
- HTTP 301 Redirects are used to forward users from the short URL to the original destination.
- Checking for existing links prevents duplication and optimizes storage.
Got a different question? SeaThru generates a fresh video for any topic where systems talk or data structures move.
Ask your own question →