Install Redis 2.4.5 as Service on Centos 6
On my initial attempt of installing Redis on a development Centos machine I kind of balanced "getting something going" with "doing it right". Now I had to install it into production so I actually paid attention to exactly what I was doing (I mainly followed in the footsteps of this guy who has written a nice albeit harder to interpret guide). Here are the steps:
- wget http://redis.googlecode.com/files/redis-2.4.5.tar.gz
- tar xvzf redis-2.4.5.tar.gz
- cd redis-2.4.5
- mkdir /etc/redis /var/lib/redis
- cp src/redis-server src/redis-cli /usr/local/bin
- cp redis.conf /etc/redis
- vi /etc/redis/redis.conf:
- daemonize yes
- bind 127.0.0.1 (don't do this if you need to access redis from another computer)
- loglevel notice
- logfile /var/log/redis.log
- dir /var/lib/redis
- wget https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522... (an init script for Redis en Centos someone kindly wrote.)
- vi redis-server:
- mv redis-server /etc/init.d
- chmod 755 /etc/init.d/redis-server
- chkconfig --add redis-server
- chkconfig --level 345 redis-server on
- vi /etc/sysctl.conf (see below):
- vm.overcommit_memory = 1
- sysctl vm.overcommit_memory=1 (see below)
- service redis-server start
Finished! Run redis-cli to connect, set a key, save, then check a dump.rdb file appears in /var/lib/redis.
But what are those scary kernel-configuring commands you're asking me to make with "vm.overcommit_memory"?
You don't have to do it, but it's recommended and explained in the Redis FAQ under "Background saving is failing with a fork() error under Linux even if I've a lot of free RAM!". I found out about it because if you don't do it, after startup Redis writes a warning about it into the log file. Here's my summary of the situation: If you're confident that there will always be as much memory as Redis is currently using free in your system, you don't need it. If not, you should enable it because writes to disk could fail. Unless you don't mind some of the disk writes failing, I suppose.
Basically Redis forks in order to write to disk, since forking means duplicating the process, logically you need at least as much free memory available as Redis is currently using, in order to be taken by the forked process. The trick comes with the fact that with optimisations in modern OSs, this statement is no longer true and the forked process will actually just share the same memory as the original process as long as neither changes it. So it could be that you need no additional memory at all! Setting overcommit_memory = 1 allows us to take advantage of this and tells the kernel to approve the memory allocation even if there is not sufficient memory technically available. Redhat says:
Use these previous two parameters with caution. Enabling overcommit_memory can create significant performance gains at little cost but only if your applications are suited to its use. If your applications use all of the memory they allocate, memory overcommit can lead to short performance gains followed by long latencies as your applications are swapped out to disk frequently when they must compete for oversubscribed RAM.
Since Redis is evidently suited to its use, I reckon it should be OK. But have a think about what else is running on the system.
Lastly you can alternatively set it to 2 which causes it to work in combination with an "overcommit_ratio" setting, allowing overcommits up to a percentage above physical memory. Note that in one point the above linked Redhat article mixes up the settings 1 and 2 I believe - can be confirmed here.