How You Can Do URL Shortening
Updated: Mar 1, 2019
URL's or 'Uniform Resource Locator' is an integral part of our day-to-day life on the internet. They are the mechanisms in which we jump from one page to the other as we search with Google or follow up on an interesting post on Facebook. As the internet increased in popularity and people started to create their own content, the previous practice of hand-crafting links became obsolete, now content management systems generate the links via an internal logic that creates enormous links like this,
And thus the art of URL shortening was born.
URL Shortening is, in essence, creating a short link that redirects to its longer counterpart. There are many ways to achieve this but the most common approach is to generate unique keys to each URL you want to shorten, and then pair that will a root URL or domain. An example would be
Additionally, any modern URL shortening service is expected to not only store millions of qualified links, but it should also find and serve those links in seconds.
The Art of Short Links
Following the algorithm presented above, the first order of business is generating a unique key for every link that's submitted for shortening. If you are using a relational database then the natural candidate for this is the primary key of the table that you are using to store the original links.
Using the primary key also solves the issue of speed talked about earlier, using it ensures that the record can be retrieved in the quickest possible manner.
As convenient as using the primary key is, it also has its own limitations (security issues aside). Imagine recording your trillionth link, here is what it would look like…
And while this link is short enough (kinda) it also highlights the fact that as your database grows the links it generates is going to get longer over time.
A popular though an imperfect solution is to "compact" the primary key, this can be done by converting it to a higher base or radix. For example, let’s convert our previous primary key in the decimal system (base 10) to the higher hexadecimal system (base 16)
1234567890 => 499602d2
If you do a count its 10 vs. 8 characters, that's a 20% savings on the length of the text. Using a higher radix/base system should provide a larger benefit.
With this foundational knowledge, you can now go ahead and create your own link shortening service. There are other topics we can discuss to enhance the service such as creating your own base system for better security, but it's best that we leave it to another article.
Irving George Menil
Java Developer 2
Here at Monstar Lab Philippines, we encourage our engineers, developers, and practically all team members to share technicals skills, development hacks, and industry knowledge to the entire family.
We conduct regular Tech Talk Tuesdays (or Thursdays) more popularly known as T3 Sessions. This helps us keep our minds sharp and always up to date with the latest in software development, mobile development, and emerging technology. You can count on the expertise of our team to design and deliver top-notch enterprise software solutions. Feel free to send us a message anytime and we’ll help you out.