architect-handbook

Software Architect Handbook

View on GitHub

URL Shortener

Step 1: Understand the problem and establish design scope

Q&A

Candidate: Can you givec an example of how a URL shortener work? Interviewer: Assume URL https://www.systeminterview.com/q=chatsystem&c=loggedin&v=v3&l=long is the original URL. Your service creates an alias with shorter length: https://tinyurl.com/y7keocwj. If you click the alias, it redirects you to the original URL.

Candidate: What is the traffic volume? Interviewer: 100 million URLs are generated per day.

Candidate: How long is the shortened URL? Interviewer: As short as possible.

Candidate: What characters are allowed in the shortened URL? Interviewer: Shortend URL can be a combination of numbers (0-9) and characters (a-z, A-Z).

Candidate: Can shortened URLs be deleted or updated? Interviewer: For simplicty, let us assume shortened URLs cannot be deleted or updated.

Use cases

Back of the envelope estimation

Step 2: Propose high-level design and get buy-in

We shall discuss the API endpoints, URL redirecting, and URL shortening flows.

API endpoints

We will design the APIs REST-style. A URL shortener primary needs two API endpoints:

  1. URL Shortening. To create a new short URL, a client sends a POST request, which contains one parameter: the original long URL. The API looks like this: POST api/v1/data/shorten.
    • Reqeust parameter: { longUrl: longURLString }
    • return shortURL.
  2. URL Redirecting. To redirect a short URL to the corresponding long URL, a client sends a GET request: GET api/v1/shortUrl.
    • Return longURL for HTTP redirection.

URL redirecting

Once the server receives a tinyurl request, it changes the short URL to the long URL with 301 redirect.

One thing worh discussing is 301 redirect vs 302 redirect:

URL Shortening

The most intuitive way to implement URL shortening is to use hash tables. Assuming the hash table stores <shortURL, longURL> pairs. Getting a long URL can be done by calling hashTable.get(shortURL).

Let us assume the short URL looks like this: www.tinyurl.com/{hashValue}. To support the URL shortening case, we must find a hash function $f_x$ that maps a long URL to the hashValue.

The hash function must satisfy the following requirements:

Step 3: Design deep dive

We dive deep into the following: data model, hash function, URL shortening and URL redirecting.

Data model

Storing everything in a hash table is not feasible for real-world systems as memory resources are limited and expensive. A better option is to store the mapping in a relational database.

Hash function

Hash value length

The hashValue consists of characters from 0,9, a-z, A-Z, containing 10 + 26 + 26 = 62 possible characters. To figure out the length of hashValue, find the smallest n such that 62^n >= 365 billion. The system must support up to 365 billion URLs based on the back of the envelope estimation.

When n = 7, 62^n = ~3.5 trillion, which is more than enough to hold 365 billion URLs, so the length of hashValue is 7.

We will explore two types of hash functions for a URL shortener. The first one is “hash + collision resolution”, and the second one is “base 62 conversion”.

Function 1: Hash + collision resolution

This hash function hashes a long URL to a 7-character string. A straightforward solution is to use well-known hash functions like CRC32, MD5, or SHA-1. The following table compares the hash results after applying different hash functions on this URL: https://en.wikipedia.org/wiki/Systems_design.

Even the shortest hash value (from CRC32) is more than 7 characters. How can we make it shorter?

The first approach is to collect the first 7 characters of a hash value; however, this method can lead to hash collisions. To resolve hash collisions, we can recursively append a new predefined string until no more collisions is discovered.

This method can eliminate collision; however, it is expensive to query the database to check if a shortURL exists for every request. A technique called bloom filters can improve performance. A bloom filter is a space-efficient probabilistic technique to test if an element is a member of a set.

Function 2: Base 62 conversion

Base conversion is another approach commonly used for URL shorteners. Base conversion helps to convert the same number between its different number repsentation systems. Base 62 conversion is used as there are 62 possible characters for hashValue.

Let us use an example to explain how the conversion works: convert $11157_10$ to base 62 representation:

Thus, the short URL is https://tinyurl.com/2TX

URL shortening deep dive

As one of the core pieces of the system, we want the URL shortening flow to be logically simple and functional. Base 62 conversion is used in our design.

If the long URL is new, a new unique ID (primary key) is generated by the unique ID generator. Convert the ID to shortURL with base 62 conversion.

URL redirecting deep dive

As there are more reads than writes, <shortURL, longURL> mapping is stored in a cache to improve performance.

  1. A user clicks a short URL link.
  2. The load balancer forwards the request to web servers.
  3. If a shortURL is already in the cache, return the longURL directly.
  4. If a shortURL is not in the cache, fetch the longURL from the database. If it is not in th database, it is likely a user entered an invalid shortURL.
  5. The longURL is returned to the user.

Step 4: Wrap up

If there is extra time, here are a few additional talking points: