MongoDB and Redis for an URL shortener
March 1, 2010
I’ve wanted to prototype an URL shortener some time ago to have a measure on how long it would take to do using a NoSQL DB to store stats and do analytics. The main reason would do it using cyclone and MongoDB, and later build over it to create a tracker, web analytics tool and maybe a NGINX module.
As there are many topics discussing to which use each new NoSQL db belongs too, I thought that it may be natural to use more than one DB to specific tasks.
I setup to do it this weekend. From MongoDB I got the ease to create, change, manage documents and collections. It’s really refreshing start using and changing documents without worrying about migrations, schemas, tables and so on. From the initial idea to the final document, all I had to do was fire-up a python prompt (lovely BPython) and mess around with my documents. I soon found out that while MongoDB would be killer for stats storage, I missed Redis pragmatism regarding atomic operations. You can setup a counter with less code and worries that would need in a MongoDB only environment.
So I set up to implement a Redis unique global counter (using INCR) and fast encoded url to url cache (for redirects). Naturally I got a encoded url to mongo ObjectId index which is very fast. Also, I could cook up a nginx module which queries Redis to translate the URL, using its logs and a simple job script to gather stats and persist on MongoDB. But thats another project.
To real use there are some steps to be taken, but I consider it an excellent proof of concept for a weekend hack !
Enjoy it. The repository is at http://github.com/gleicon/uurl