Redis and Relational Data

Redis and Relational Data

To test the the performance I created a dataset that includes 10,000 ‘blog posts’, with each post having 10 comments (100,000 comments in total). All tests were run on a 2011 Macbook Pro (2.3 GHz i7, 8GB RAM). To test PostgreSQL, I sequentially fetched each post and used a join to retrieve its comments (10,000 separate queries). The test was repeated six times to produce an average time and was done for both PostgreSQL and Redis.

Average Time (Seconds) Query (Milliseconds)
psql 138.34 13.8
psql (Native Bindings – NB) 125.95 12.6
psql (NB + Index) 2.72 0.27
Redis (Hires) 0.76 0.067

‘a-post-about-databases’ : { title : ‘A post about databases’, body : ‘…’, createdAt : 1338751532301}

 

redis.zrange(‘posts’, 0, 10, function(error, posts){ //return the keys (urls) associated with the first 11 posts }) var startDate = (new Date(2012, 5, 1)).getTime() ; // June 1st var endDate = (new Date(2012, 5, 30)).getTime(); // June 30th redis.zrangebyscore(‘posts’, startDate, endDate, function(err, posts){ //returns the keys (urls) associated with all the posts from June 2012 })

 

var postURL = ‘a-post-about-databases’ var multi = redis.multi(); // queue up the queries multi.hgetall(postURL); multi.zrange(‘comments-‘ + postURL, 0, 1); // execute the queries atomically multi.exec( function(error, results){ /* results[0] will contain the post results[1] will contain an array with all the comments */ });

The single biggest caveat to using Redis, is that it is entirely in memory. If your relational dataset is 2.5GB (not that large), you’ll need a $160/month Linode (4GB RAM) to keep it in Redis. In contrast, a $20/month Linode (512MB RAM) has 20GB of disk space and could easily hold that same dataset using PostgreSQL. This tradeoff becomes even more of an issue as your dataset become larger than 4GB.

 

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s