1

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?

Community
  • 1
  • 1
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • I don't understand the problem. What issue are you having? There are results like this: http://stackoverflow.com/questions/9951163/generating-short-urls-in-a-distributed-data-store – WiredPrairie Apr 07 '13 at 23:58

1 Answers1

2

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.

Community
  • 1
  • 1
Sim
  • 13,147
  • 9
  • 66
  • 95
  • "Having tried both at scale exceeding 1,000 requests/second, I would recommend the approach based on `findandModify`." - Thank you, that just about sums it up. Was wanting to know how it'd scale. – Naftuli Kay Apr 08 '13 at 04:13