• Monstar Lab PH

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,

https://www.facebook.com/builtable/?__tn__=kC-R0.g&eid=ARBPC_wY3Mw7S52Bu09GfhTXAZRams5iYYQVw1cWaIAo-tdfYtO2msYy8vlcsmiuki4N6crQvCQY8d1-&hc_ref=ARQFmYOmV9umSlG4TuQhewWE8WrgImown-8k1X_jVWfKHx02ahotDaqIdZfCgnWXDBY&fref=nf&__xts__%5B0%5D=68.ARCILgzGv5xh-sRzfxNimL2CNPet7Cebf9miUXBi_9ShNKXkElIoXGQkRm2eepGlTqqwSaog8orfeg1thVu0Txzr8EK5SQP1-bf7ITVsi2thVj4XM5-0otC4CCMzjbYI0Aow1dvPOJpH9_VC0MLM4b4pRcBt-4twk1Me_we9ZknLzV94h43RPtxsrzmKVjsfAtxV94nhIaXI9tW-odIn500_gqnyPNbAoRLDWkAV8Z8D9gY5Qs6a4ce24_b2058jgzI_ZdV9qbrJyDoA9-f501MXhYGvEuu9LjIL04t7SAq5bCyFZYyaqEG2P2tJ0r0fAFM


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

https://my.domain.com/1bNsLrK


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…

https://my.domain.com/1234567890


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.



Contributor:

Irving George Menil

Senior Developer


Original Content:

Nathaniel Martinez

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.