Redis setup
mouse 397 · person cloud · link
Last update
2019-02-21
2019
02-21
«caching with http://redis.io/»

NOTE: Redis has changed its license... see its GoodFORM fork on github.


Configure Linux:

1
2
3
4
5
6
7
8
9
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
sysctl vm.overcommit_memory=1

echo "net.core.somaxconn = 512" >> /etc/sysctl.conf
sysctl net.core.somaxconn=512

# run and add this to /etc/rc.local (system startup) or
# reboot with the kernel bootparam transparent_hugepage=never
echo never > /sys/kernel/mm/transparent_hugepage/enabled

Compile Redis:

1
2
3
4
5
6
7
8
9
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
sudo apt-get install libc6-dev-i386 # per la versione a 32bit
make 32bit # usa less ram but max memory will be 3GB

# optional
sudo apt-get install tcl8.5
make test

Configure Redis (redis.conf):

1
2
3
4
5
6
7
8
9
10
11
bind 127.0.0.1
daemonize yes
pidfile /path/to/redis.pid
logfile "/path/to/redis.log"
unixsocket /path/to/redis.sock
unixsocketperm 700
save "" # disable auto snapshots
dir /path/to/db_dump_dir
# NOTE: snapshots can take up to 2x maxmemory
maxmemory xxxMB
maxmemory-policy allkeys-lru # eviction policy

Configure Rails:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# in Gemfile:
gem 'hiredis'  # speed up redis client
gem 'redis', require: %w{ redis redis/connection/hiredis } # redis client
gem 'readthis' # use redis as a faster cache store

# in environment config:
config.cache_store = :readthis_store, {
  expires_in: 1.hour.to_i,
  namespace: 'foobar',
  redis: { # redis-rb params
    # host: 'localhost', port: 6379, db: 0,
    url:  'redis://localhost:6379/0',
    # path: 'tmp/sockets/redis.sock', # this is not fault tolerant
  },
  driver: :hiredis
}

# in config/initializers/redis.rb:
Readthis.fault_tolerant = true

# everywhere in the code always use .to_i for expiration time:
expires_in: 1.week.to_i

Source: Redis.io quickstart, full docs, LRU, Admin; DigitaOcean/redis; SitePoint/rails; SitePoint/models; Stackoverflow/models