Press enter or space to select a node.You can then use the arrow keys to move the node around. Press delete to remove it and escape to cancel.
Press enter or space to select an edge. You can then press delete to remove it or escape to cancel.
Overview
URL shortening services make it easier to share links on social media platforms, where there may be a limit on the number of characters that can be used in a post. The long, original URL is converted into a shorter, more manageable link that redirects to the original website. A service like this can also be useful for tracking clicks on a link. Many URL shortening services provide tracking and analytics tools.A shortened link (shortlink) can look something like "https://shorturl.com/s2vZfGh". While referencing the term "shortlink" throughout this design, the focus on will be on the last part of that URL, "s2vZfGh".
API Design
The client primarily sends two different requests. These requests can be modeled using REST.
GET /:shortlink, params = ()
- Returns the original URL corresponding to the provided shortlink, and redirects to the original URL.
POST /shorten, params = (original_url, api_key, custom_shortlink, expiration_date)
- The client provides the original URL, an api key, a custom shortlink, and an expiration date as input. The original URL is then mapped to the custom shortlink and stored in the URL database.
Data Model
We can use a NoSQL database to store the URL data. Since the shortlink is unique (there can't be multiple original URLs mapped to a shortlink), it can act as the primary key for the URL table stored in the database.
URL Object
short_url: string - PK
original_url: string
created_at: datetime
expiration_date: datetime
Generating and Mapping Shortlinks
How long should a shortlink be? If we assume we can use digits 0-9, and the lowercase and uppercase English alphabet, then
6 characters ≈ 56.8 billion possible shortlinks
7 characters ≈ 3.5 trillion possible shortlinks
8 characters ≈ 218 trillion possible shortlinks
6-8 characters should be long enough for a shortlink.How do we create shortlinks? The core design challenge is figuring out how to create and map shortlinks to original URLs. This is directly related to the
POST /shorten
request, and the logic resides in the Application Servers.Upon receiving the
POST /shorten
request, our Application Servers first check whether the custom shortlink already exists in the URL database. With many users and a large number of URLs, checking for the existence of a shortlink can become slow and expensive. This can be optimized by using Bloom Filters. If the shortlink is available, then we can store a new URL object in the database. Otherwise, an error message is returned to the client.