unfoldthat.com

Try Redis instead

Update: Okay, a big fat disclaimer: yes, Redis can’t fully replace a database, message queue and things like that. Especially it can’t replace reason and common sense. It has its limitations. What I’m trying to say is that uses of Redis are well beyond these of simple key-value stores like Memcached, it is really simple and lightweight to not be a problem and odds are that you can find it a good use-case or two in your project, even without rewriting and re-architecting things.

When I first heard of Redis, I thought - oh, it’s probably impressive, but I can’t find it a use and I have some work to be done. Still resisting to think about it later, I found an article named “How to take advantage of Redis just adding it to your stack” written by the very author of Redis. There is something impressive about this idea: benefit just from installing Redis. I like the approach, but things he proposes do not satisfy the goal - you still have to write some code. However, it inspired me to try Redis for real and I found out what it is awesome, it is extremely useful and, yes, you can just install it, configure a bit and make your web application better. Here is how.

Install Redis

There is a small inconvenience about Redis: it can’t be installed just by running apt-get1. It doesn’t make installation too hard: wget/tar/cd/make is all you need. You can find a source package on Redis website (don’t just copy-paste this: use the latest version).

Update: You know what? Just use packages from debian-backports, as Chris Lamb suggests.

sudo apt-get install build-essential
wget http://redis.googlecode.com/files/redis-2.2.13.tar.gz
tar -zxf redis-2.2.13.tar.gz
cd redis-2.2.13
make

Now you can run Redis just like that:

src/redis-server

But you probably want to add it to supervisor, runit or something like that instead. We should also install the python library:

pip install redis

Use it for sessions

Your Django website already uses sessions, and it results in making a query to your database on most pages. So if you make Redis handle sessions instead, you will make the website more healthy right away.

Install django-redis-sessions. They aren’t on PyPi if I’m not mistaken, let’s just use pip’s superpowers:

pip install git+https://github.com/martinrusev/django-redis-sessions

Configure Django now:

SESSION_ENGINE = 'redis_sessions.session'

SESSION_REDIS_HOST = 'localhost'
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 0

Use it for cache

You don’t need to kill your harddrive by using it for caching, and you don’t need any old-fashioned Memcached servers. Just use Redis!

pip install django-redis-cache

And add this to your settings:

CACHES = {
    'default': {
        'BACKEND': 'redis_cache.RedisCache',
        'LOCATION': 'localhost:6379',
        'OPTIONS': {
            'DB': 1,
        },
    },
}

As you have noticed, I’ve used different databases for cache and sessions. By default, Redis has 16 databases for you to use - no need to configure further. However, you can just put everything into a single database if you fell like it.

Use it instead of RabbitMQ

I like celery, and I used it with RabbitMQ for a long time. And I hated it - while a server and a worker processes, nginx and postgres were doing just fine on a micro AWS instance, RabbitMQ kept putting it into unusable state (I couldn’t even connect using SSH).2

Really, here is my piece of advice - want RabbitMQ? Use Redis instead! It’s not just a key-value story, it has a Pub/Sub message queue among other things. Here is how you configure Celery to use it:

REDIS_HOST = "localhost"
REDIS_PORT = 6379

CELERY_RESULT_BACKEND = "redis"
CELERY_REDIS_HOST = REDIS_HOST
CELERY_REDIS_PORT = REDIS_PORT
CELERY_REDIS_DB = 2

BROKER_TRANSPORT = "redis"
BROKER_HOST = REDIS_HOST  
BROKER_PORT = REDIS_PORT         
BROKER_VHOST = "3"

Now use it for everything

Suddenly from hipster thingy Redis become an essential part of your stack - without you writing a single line of code! There are no reasons to postpone trying it out any more. Play with its commands online and start replacing ugly SQL-queries with nice zincrbys. You will be astonished how great it is.

I’ve made staste with Redis in two days. No way I could do it with Postgres or MongoDB so quickly.


  1. There is a package named redis-server, but it seems really outdated. Beware!

  2. Okay, using micro instances for so much stuff is probably stupid. That doesn’t make RabbitMQ any more lightweight.

14th of September, 2011

blog comments powered by Disqus

Please send your comments, ideas, rants and job offers at v.golev@gmail.com.

Made with Nginx, Jekyll, Git, EC2 and Emacs.

Carried out by Valentin Golev