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:

  1. wget http://redis.googlecode.com/files/redis-2.4.5.tar.gz
  2. tar xvzf redis-2.4.5.tar.gz
  3. cd redis-2.4.5
  4. make
  5. mkdir /etc/redis /var/lib/redis
  6. cp src/redis-server src/redis-cli /usr/local/bin
  7. cp redis.conf /etc/redis
  8. 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
  9. wget https://raw.github.com/gist/257849/9f1e627e0b7dbe68882fa2b7bdb1b2b263522... (an init script for Redis en Centos someone kindly wrote.)
  10. vi redis-server:
    • redis="/usr/local/bin/redis-server"
  11. mv redis-server /etc/init.d
  12. chmod 755 /etc/init.d/redis-server
  13. chkconfig --add redis-server
  14. chkconfig --level 345 redis-server on
  15. vi /etc/sysctl.conf (see below):
    • vm.overcommit_memory = 1
  16. sysctl vm.overcommit_memory=1 (see below)
  17. 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.

Comments

Thanks work well also on Centos 5

The above steps worked well even on Red Hat Enterprise Linux Server 6 (64-Bit) . Thank you!

Thanks for the walkthrough.  One problem is that it is not running it as a service in the background.  With your command, as soon as I ctrl+c to get my prompt back the redis server stops.  What options do I have to continue to run even after I logout?

Hi Anthony,

I believe the important part is to edit /etc/redis/redis.conf and set "daemonize" to "yes".

Hope that works for you,

John

It works!

Hi.Thanx a lot for explanation and very well documented setup guide.JM on RHEL62 64-bit

Thanks, it's working great

What are the equivilant commands using unbuntu 12x for the commands:

  1. chkconfig --add redis-server
  2. chkconfig --level 345 redis-server on

Thanks for a great article.You could also set Logrotate to keep your logs from getting too big:

echo '/var/log/yum.log {
missingok
notifempty
size 50M
create 0600 root root
rotate 1
}' > /etc/logrotate.d/redis

Thanks telm

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.