Redis インストール

Nid: 1153

1 . Redisのインストール

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

2 . 設定変更

Disable RDB snapshot

maxmemory を4GBに設定 1024x1024x1024x4=4294967296

$ sudo vim /etc/redis/redis.conf

# Accept connections on the specified port, default is 6379 (IANA #815344).
# If port 0 is specified Redis will not listen on a TCP socket.
#port 6379
port 0

# Unix socket.
unixsocket /var/run/redis/redis-server.sock
unixsocketperm 770

supervised systemd

#save 900 1
#save 300 10
#save 60 10000
save "" # Disable RDB snapshot

stop-writes-on-bgsave-error no
rdbcompression yes

maxclients 10000
maxmemory 4294967296
maxmemory-policy allkeys-lru

unix socket 使用のための権限設定

$ sudo chown -R redis:www-data /var/run/redis
$ sudo vi /etc/systemd/system/redis.service
User=redis
#Group=redis
Group=www-data
$ sudo systemctl daemon-reload

overcommit_memory を1に変更。

net.core.somaxconn を設定

$ sudo vim /etc/sysctl.conf
vm.overcommit_memory = 1
net.core.somaxconn = 1024 # Increase number of incoming connections backlog
$ sudo sysctl vm.overcommit_memory=1
$ sudo sysctl -w net.core.somaxconn=1024

THP を無効にする。

$ cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
$ su -
# echo never > /sys/kernel/mm/transparent_hugepage/enabled
$ sudo vi /etc/rc.local
echo never > /sys/kernel/mm/transparent_hugepage/enabled

サービス再起動

$ sudo systemctl restart redis
$ ls -l /var/run/redis
total 4.0K
-rw-rw---- 1 redis www-data 8 Oct 10 06:15 redis-server.pid
srwxrwxr-x 1 redis www-data 0 Oct 10 06:15 redis-server.sock=
$ sudo service redis-server status
? redis-server.service - Advanced key-value store
   Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-12-15 10:52:54 UTC; 6min ago
     Docs: http://redis.io/documentation,
           man:redis-server(1)
  Process: 15648 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 15651 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS)
 Main PID: 15668 (redis-server)
    Tasks: 4 (limit: 4915)
   CGroup: /system.slice/redis-server.service
           +-15668 /usr/bin/redis-server 127.0.0.1:6379

Dec 15 10:52:54 ns9990598 systemd[1]: Starting Advanced key-value store...
Dec 15 10:52:54 ns9990598 systemd[1]: Started Advanced key-value store.
$ ls -l /var/run/redis/redis-server.pid
-rw-rw---- 1 redis www-data 8 Oct 10 06:18 /var/run/redis/redis-server.pid

3 . ログの確認と対応 log

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

4 . 動作確認

$ redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> set test "It's working!"
OK
127.0.0.1:6379> get test
"It's working!"
127.0.0.1:6379> exit

ベンチマークプログラム

$ sudo -u www-data redis-benchmark -s /var/run/redis/redis-server.sock -c 100 -n 100000 -d 256 -q
PING_INLINE: 152671.77 requests per second
PING_BULK: 152207.00 requests per second
SET: 152207.00 requests per second
GET: 148588.42 requests per second
INCR: 148148.14 requests per second
LPUSH: 150150.14 requests per second
RPUSH: 137551.58 requests per second
LPOP: 150829.56 requests per second
RPOP: 151285.92 requests per second
SADD: 151745.08 requests per second
HSET: 144300.14 requests per second
SPOP: 150375.94 requests per second
LPUSH (needed to benchmark LRANGE): 152439.02 requests per second
LRANGE_100 (first 100 elements): 30129.56 requests per second
LRANGE_300 (first 300 elements): 9078.53 requests per second
LRANGE_500 (first 450 elements): 5640.48 requests per second
LRANGE_600 (first 600 elements): 4366.24 requests per second
MSET (10 keys): 99206.34 requests per second

5 . 情報確認

$ redis-cli info
# Server
redis_version:4.0.9
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:76095d16786fbcba
redis_mode:standalone
os:Linux 4.15.0-42-generic x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:7.3.0
process_id:15668
run_id:fc7b8b8b9aaeecd49a86c8708bed038c04bbf05f
tcp_port:6379
uptime_in_seconds:344
uptime_in_days:0
hz:10
lru_clock:1367646
executable:/usr/bin/redis-server
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:374296
used_memory_human:365.52K
used_memory_rss:2215936
used_memory_rss_human:2.11M
used_memory_peak:374296
used_memory_peak_human:365.52K
used_memory_peak_perc:118.63%
used_memory_overhead:365142
used_memory_startup:315512
used_memory_dataset:9154
used_memory_dataset_perc:15.57%
total_system_memory:33621499904
total_system_memory_human:31.31G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:5.92
mem_allocator:jemalloc-3.6.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1544871174
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
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
aof_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:0
instantaneous_ops_per_sec:0
total_net_input_bytes:14
total_net_output_bytes:0
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:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:master
connected_slaves:0
master_replid:9d5c54ea3f1e1c87f618086a5caaa90421fb3fdb
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.28
used_cpu_user:0.08
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace

Flushing The Cache

$ redis-cli flushall
$ sudo redis-cli -s /var/run/redis/redis-server.sock flushall
OK

memory

$ redis-cli
127.0.0.1:6379> memory help
1) "MEMORY DOCTOR                        - Outputs memory problems report"
2) "MEMORY USAGE  [SAMPLES ] - Estimate memory usage of key"
3) "MEMORY STATS                         - Show memory usage details"
4) "MEMORY PURGE                         - Ask the allocator to release memory"
5) "MEMORY MALLOC-STATS                  - Show allocator internal stats"
127.0.0.1:6379> quit