Redis のインストールと設定

1 . Redisのインストール

$ sudo apt-get install php-redis redis-server
$ sudo service redis-server status

2 . Drupal設定

$ cd sites/all/libraries && sudo git clone https://github.com/nrk/predis.git && sudo chown -R www-data:www-data predis && cd -
$ sudo drush -y en redis
$ sudo vim sites/default/settings.php 
/**
 * Redis
 */
$conf['redis_client_interface'] = 'Predis';
$conf['lock_inc']               = 'sites/all/modules/redis/redis.lock.inc';
$conf['path_inc']               = 'sites/all/modules/redis/redis.path.inc';
$conf['cache_backends'][]       = 'sites/all/modules/redis/redis.autoload.inc';
$conf['cache_default_class']    = 'Redis_Cache';

3 . 情報確認

$ redis-cli info
# Server
redis_version:3.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:687a2a319020fa42
redis_mode:standalone
os:Linux 4.4.0-36-generic x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:5.3.1
process_id:2401
run_id:20fe76bbbd6194da9365c36a241adc18596974a1
tcp_port:6379
uptime_in_seconds:642386
uptime_in_days:7
hz:10
lru_clock:13775035
config_file:/etc/redis/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:1900856752
used_memory_human:1.77G
used_memory_rss:2874097664
used_memory_peak:2230228200
used_memory_peak_human:2.08G
used_memory_lua:1107968
mem_fragmentation_ratio:1.51
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:208
rdb_bgsave_in_progress:0
rdb_last_save_time:1473392756
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:14
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:442031
total_commands_processed:89807835
instantaneous_ops_per_sec:0
total_net_input_bytes:20106192790
total_net_output_bytes:345477689491
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:122756
evicted_keys:0
keyspace_hits:84360251
keyspace_misses:2739145
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:43020
migrate_cached_sockets:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:1662.58
used_cpu_user:7545.18
used_cpu_sys_children:3801.99
used_cpu_user_children:16593.39

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=333795,expires=332992,avg_ttl=31087190474

4 . Disable RDB snapshot

$ sudo vim /etc/redis/redis.conf
save 900 1
save 300 10
save 60 10000
save "" #追加
$ sudo systemctl restart redis-server.service

5 . ログの確認と対応 log

Warning 等を確認して、対応。

$ sudo less /var/log/redis/redis-server.log

maxclients のデフォルト値10000は多いので減らす

# You requested maxclients of 10000 requiring at least 10032 max file descriptors.
# Redis can't set maximum open files to 10032 because of OS error: Operation not permitted.
# Current maximum open files is 4096. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase 'ulimit -n'.
$ sudo vim /etc/redis/redis.conf
maxclients 1024

overcommit_memory を1に変更。

# WARNING overcommit_memory is set to 0! Background save may fail under low memory condition.
$ sudo vim /etc/sysctl.conf
vm.overcommit_memory = 1
$ sudo sysctl vm.overcommit_memory=1

THP を無効にする。

# WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
$ cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
$ sudo -
# echo never > /sys/kernel/mm/transparent_hugepage/enabled
$ sudo vim /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled
$ sudo systemctl restart redis

net.core.somaxconn を設定

# WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
$ sudo vim /etc/rc.local
sysctl -w net.core.somaxconn=65535
$ sudo sysctl -w net.core.somaxconn=65535

6 . Unix Socket を使用してスピードアップ(未検証)

https://guides.wp-bullet.com/how-to-configure-redis-to-use-unix-socket-s... https://www.techandme.se/performance-tips-for-redis-cache-server/

$ sudo usermod -g www-data redis
$ sudo chown -R redis:www-data /var/run/redis
$ sudo vim /etc/redis/redis.conf
port 0
unixsocket /var/run/redis/redis.sock
unixsocketperm 775
$ sudo systemctl restart redis
$ ls -lh /var/run/redis
total 0
srwxr-xr-x 1 redis www-data 0 Sep 10 17:00 redis.sock
$ redis-cli -s /var/run/redis.sock