Question

Writing a URL shortener seems fairly straightforward for a traditional database, with a couple notable exceptions:

def save_new_url(request):
    url = StoredUrl(url=request.POST['url'])
    url.save()

    url.short_link = base62(url.id)
    url.save()

    return direct_to_template('mytemplate.html', { 'link': url.short_link })

The only problem with the above example is the lack of support for known exceptions, URLs which should be reserved for system/site usage, like account or admin.

How then, can I make a URL shortening service for a NoSQL database like MongoDB, which uses hexadecimal values as keys for its 'rows'? It seems that most URL shorteners work on shortening numbers into [a-zA-Z0-9] sets of characters. Since I don't have a number to work with, how can I shorten a URL stored in a MongoDB table?

Was it helpful?

Solution

First, there are many ways you can use the _id field in MongoDB. See my answer to this SO question for some ideas.

Then, if I understand correctly, your question has to do with the fact that SQL DBs have auto-increment counters which are convenient primary keys while NoSQL DBs, like MongoDB, don't, which opens the question about what should you use as the basis for generating a new short URL.

If you want to generate auto-incrementing IDs in MongoDB there are two main approaches: optimistic loop & maintaining a separate counter collection/doc. Having tried both at scale exceeding 1,000 requests/second, I would recommend the approach based on findAndModify.

A good URL shortener design would also include randomization, which in this case would mean leaving random gaps between different auto-incrementing IDs. You can use this by generating a random number on the client and incrementing by that number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top