redis is the new memcached
One of the first things you learn about optimizing web applications is how to cache processed data in memory. Just install memcached - the easy to use key-value store - and you're set. Until you reach its limits, that is...
Memcached does not allow keys larger than 250 bytes or values bigger than 1MB. What happens when you really need bigger keys/values? You move on to redis.
If you are using a proper framework (and you should) there's probably already a redis plugin for you (here's the Django one). Now all you have to do is install redis and configure it to suit your caching needs. As a rule of thumb, speed and low resource usage are more important than data persistence. Here's my configuration (only the relevant differences from the default redis.conf):
- daemonize yes
- bind 127.0.0.1
- loglevel notice
- logfile /var/log/redis/redis.log
- dir /var/lib/redis/
- maxmemory 67108864
With these settings redis runs as a daemon, listens only on localhost, keeps the logging to a minimum and uses sensible locations for the logs and database. I also set the maximum amount of memory it can use depending on the
available RAM on the server. Adjust as needed.
The 2.4.1 version I'm using is quite fast, probably due to the jemalloc memory allocator, but if you need even more performance you can switch from TCP sockets to unix sockets. Pay attention to the socket file's default permission (755) and either run the daemon under the same user as the client(s), or change the init script to do a chmod after redis is started. Sometimes in the near future you'll be able to set the permission from redis.conf with the unixsocketperm variable and the unix socket will be the preferred way to setup local caching instead of an optimization.
Enjoy your new cache and remember that if you ever need a powerful NoSQL database, redis is there waiting for you.
Category: caching



Leave a Comment :
Leave a Comment