URL Encoding Techniques to create Shortened URL

To convert a long URL into a unique short URL we can use some hashing techniques like Base62 or MD5. We will discuss both approaches. 

1. Base62 Encoding

  • Base62 encoder allows us to use the combination of characters and numbers which contains A-Z, a-z, 0–9 total( 26 + 26 + 10 = 62).
  • So for 7 characters short URL, we can serve 62^7 ~= 3500 billion URLs which is quite enough in comparison to base10 (base10 only contains numbers 0-9 so you will get only 10M combinations).
  • We can generate a random number for the given long URL and convert it to base62 and use the hash as a short URL id. 

If we use base62 making the assumption that the service is generating 1000 tiny URLs/sec then it will take 110 years to exhaust this 3500 billion combination.

Python3




def to_base_62(deci):
    s = '012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    hash_str = ''
    while deci > 0:
        hash_str = s[deci % 62] + hash_str
        deci /= 62
    return hash_str
  
  
print to_base_62(999)


Javascript




function to_base_62(deci) {
  var hash_str, s;
  s = "012345689abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  hash_str = "";
  
  while (deci > 0) {
      var b = parseInt(deci % 62);
      var a = s[b] ? s[b]: "";
    hash_str = hash_str+a;
      deci = parseInt(deci/62);
      console.log("b",b,"a",a, "deci", deci);
  }
  
  return hash_str;
}
  
to_base_62(64);


2. MD5 Encoding

MD5 also gives base62 output but the MD5 hash gives a lengthy output which is more than 7 characters.

  • MD5 hash generates 128-bit long output so out of 128 bits we will take 43 bits to generate a tiny URL of 7 characters.
  • MD5 can create a lot of collisions. For two or many different long URL inputs we may get the same unique id for a short URL and that could cause data corruption.
  • So we need to perform some checks to ensure that this unique id doesn’t exist in the database already. 

System Design | URL Shortner (bit.ly, TinyURL, etc)

The need for efficient and concise URL management has become a big matter in this technical age. URL shortening services, such as bit.ly, and TinyURL, play a massive role in transforming lengthy web addresses into shorter, shareable links. As the demand for such services grows, it has become vital to undertand the System Design of URL Shortner and mastering the art of designing a scalable and reliable URL-shortening system, to gain a crucial skill for software engineers.

This article gets into the System Design of URL Shortner (URL Shortening Service), which will help in architecting a robust system that can seamlessly generate and redirect short URLs while ensuring scalability, durability, and high availability.

Important Topics for System Design of URL Shortner (URL Shortening Service)

  • How to Design a URL Shortener Service Like TinyURL?
  • Requirements for System Design of URL Shortner
  • Capacity estimation for System Design of URL Shortner
  • Low Level Design for System Design of URL Shortner
    • URL Shortening Logic (Encoding)
    • Techniques to Generate and Store TinyURL 
  • High-level Design for System Design of URL Shortner
  • Database
  • Conclusion

Similar Reads

How Would You Design a URL Shortener Service Like TinyURL?

URL shortening services like bit.ly or TinyURL are very popular to generate shorter aliases for long URLs. You need to design this kind of web service where if a user gives a long URL then the service returns a short URL and if the user gives a short URL then it returns the original long URL....

1. Requirements for System Design of URL Shortner Service

1.1 Functional requirements of URL Shortening service...

2. Capacity estimation for System Design of URL Shortner

Let’s assume our service has 30M new URL shortenings per month. Let’s assume we store every URL shortening request (and associated shortened link) for 5 years. For this period the service will generate about 1.8 B records....

3. Low Level Design for System Design of URL Shortner

...

3.1 URL Encoding Techniques to create Shortened URL

To convert a long URL into a unique short URL we can use some hashing techniques like Base62 or MD5. We will discuss both approaches....

3.2 Efficient Database Storage & Retrieval of TinyURL

...

4. High-level Design of a URL-Shortening Service

...

5. Database

Let’s discuss the mapping of a long URL into a short URL in our database:...

Conclusion

...

Contact Us