First ask clarifying questions, mainly how many URLs are being shortened per month. Then do calculations for how much data we’ll have to store.
Redirection
301 and 302 are HTTP status codes for redirection. The former stands for permanently moved and the latter for temporarily moved
When a brower gets 301, it catches the short url to long url mapping. Subsequent calls will just use this cache. This option makes sense when we want to reduce server load. However, 302 makes sense when we want to track the click rate.
Convert long URL to short URL
We’ll explore two methods: hashing and base 62 conversion
Hashing and collision resolution
We can use a hash function, then take first n
characters of it to make short URL. The n
depends on how many URLs we want to support.
For collisions, add some predetermined string to the back of the long URL and hash again. Keep doing this till there’s no entry in the db for the short URL.
Since DB query can be expensive, we can use a Bloom Filter
Base 62 conversion
get a unique ID for the long URL and convert it to bade 62.