2

I have been reading about URL shorteners and there is a common question that I keep running into which I don't understand how to solve for.

Given a long URL, one of the ways to convert it into a short URL:

  • Given an id: id
  • compute base62(id): shortForm
  • store {id => shortForm, longUrl} in a database with key being id
  • return domain/shortForm

Id is either an auto incrementing number or a number chosen from a store of available keys (given scalable needs). However, if Id is chosen in either of those ways, and if I specify https://google.com twice, the algorithm would pick two different id and yield a different shortForm

In most of the services, I have seen that is not the case. I am wondering how is the same shortForm achieved for the same longUrl ?

brainydexter
  • 19,826
  • 28
  • 77
  • 115
  • 3
    By storing it in a database and looking up the corresponding URL. – Sani Huttunen Jan 23 '19 at 06:01
  • I guess I always just assumed it was a pretty simple “find or create” scenario. When generating a short url for a long one, query your table to see if you’ve already done this before. If so, return your shortForm. If not, generate some random value for your shortForm, insert a row in your table for this new combo and carry on. Toss a unique index on the long url field, and another on the shortForm field to prevent troubles, and retry if your constraints barf. – Nate Jan 23 '19 at 06:03
  • @SaniSinghHuttunen updated my question with storing in database. It is stored in database, indexed by the shortForm found or can even store it by id since it is easy to convert shortForm to Id – brainydexter Jan 23 '19 at 06:05
  • @brainydexter: You also store the URL along with the id and "short form". Or you don't store the "short form" at all and only look upp the URL and then get the id and compute the "short form" again. – Sani Huttunen Jan 23 '19 at 06:06
  • @SaniSinghHuttunen are you suggesting that I do a double write in database - 1 with shortForm: longUrl and second with longUrl, shortForm ? – brainydexter Jan 23 '19 at 06:08
  • Either that or all in one write to the same table or just id and URL. Since the "short form" can be computed with only the id you can look up the id with the provided URL. If the URL doesn't exist then create a new entry with a new id and the provided URL. – Sani Huttunen Jan 23 '19 at 06:09
  • @SaniSinghHuttunen what are you suggesting the key to be ? if provided URL is the key, then yes i can lookup id and return short url. However, in that case, how will it resolve short URL to long form url (since now the key is long url ) ? – brainydexter Jan 23 '19 at 06:28
  • Just store all three components and you'll be able to convert them in any direction you want. If you don't want to store the "short form" you still can get the id by converting the base(62) "short form" back to id since it's not a hash. – Sani Huttunen Jan 23 '19 at 06:34

0 Answers0