Thursday, June 26, 2014

Dropping MongoDB for Redis

Up until now,I have been using the NoSQL database MongoDB for Project Venice.  It is very neat and easy to use.  It is almost perfect for my needs.  However, the more I thought about it, the more I was bothered by one feature MongoDB lacks:  transactions.  This is kind of a deal breaker for the game prototype I am developing.

Let's look at the scenario of a player buying an item off of a marketplace.  There are 2 main things that need to happen:

1)  Check and deduct item inventory
2)  Check and deduct player funds

This would involve two MongoDB documents.  While each of the two can be done atomically, they can't both be done together.  So stuff could happen in between.  This would be a bad thing and lead towards all sorts of problems.  MongoDB just does not support atomic operations across documents.

I started to research alternatives.  A traditional relational database ( mysql, postgresql ) would do the trick but I didn't really want to go that route.  This prototype is about learning new things, so I wanted to stick with NoSQL.  Redis and FoundationDB ended up being the two I seriously looked at.

FoundationDB has full ACID/transaction support but looked pretty daunting.  Redis, on the other hand, has transaction support ( but no rollback ) and looked very simple to use.  Keeping with the KISS philosophy, I went with Redis.  Implementing it was very simple and did not take long.  So now Project Venice is full on Redis-fied.  

Redis also has the advantage of being memory resident, so it is very quick.  The drawback is that it needs a lot of RAM.  I'm not anywhere close to needing to worry about that though.