URL shorteners are everywhere, from links you share on twitter to popular services like bit.ly or tiny url But have you ever wondered how you could create a quick URL shortener for yourself ?
On a high level we can think of URL Shortener as passing the long the url through some encoder and in return we will get the short url. URL Shortener design revolves around 2 operations:
- Creating a short url using a long url.
- Decoding short to long url.
For Creating a short url we can consider alphabets a-z , A-Z and numeric 0–9, - , _ . So total we have 64 characters which we can use. For our case let’s consider creating 7 character urls. So with 64 characters and 7 char long url we will have 64^7 combinations available.
So we have 64^7 ~ 281 trillion combinations.
Considering 10000 requests per second.
One Year Request = 10000 * 60 * 60 * 24 * 365 = 315360000000
Years with 281 trillion combination = 281 / 0.31536 ~ 891 years.
So with 64 characters our system can create short a url for 891 years with average 10000 requests per second.
We can use multiple encoders/algorithms for creating urls. Few are below:
- Base62
- MD5
- ZooKeeper
Here I have used radix64 Algorithm to encode/ decode short URLS
With radix64 we can create short urls (7 characters) easily with small algo.
The first challenge is How can we make sure it always generate 7 chars code only ?
This code always guarenteed to generate always 7 chars random digits
parseInt(Math.random() * 1000000000000)
But how?
log of 1000000000000 of base 64 always generate 7. Same logic when we check how many bits are there in any decimal number.
Note: There is a possibility of collision with radix64 also but chances are less since we have taken large sample space.
-
Install latest version of Node.
Note: Please install node 14+ some of the latest JS features may break in older version of node. -
Install postgres as your RDBMS.
For mac users please install it via homebrew (assuming you have already installed homebrew.)
brew install postgresql
-
create a database as mentioned in the db.js file
url_shortner_db
and create a role fordbadmin
. To change the databse-name / role / password please change db.js file accordingly. -
To start the application type
node server.js
on your terminal. -
To create a new link enter
/api/links
add body as json :
{ link:"https://youtube.com", code:"rTYgH5_" // optional:: user defined short code }
-
To get the actual URL enter short code e.g
/rTYgH5_
it will redirect to youtube's site.