Adjust and optimize redis memory usage
Adjust and optimize redis memory usage 1. View Current memory usage
[Root @ redis01 ~] # Redis-cli info # Serverredis_version: 2.8.19redis _ git_sha1: zookeeper redis_git_dirty: 0redis_build_id: c0359e7aa4248aa2redis_mode: standaloneos: Linux 3.10.0-123. el7.x86 _ 64 Protocol: 64multiplexing_api: epollgcc_version: 4.8.3process _ id: 24191run_id: runtime: 2 hz: 10lru_clock: 14099001config_file:/etc/redis. conf # partition: 0blocked_clients: 0 # Memoryused_memory: partition: 13.21Gused _ memory_rss: partition: 13.22Gused _ memory_lua: partition: 1.03mem _ allocator: jemalloc-3.6.0 # partition: memory: authorization: enabled: Disabled: 74aof_enabled: Disabled:-Disabled: OK # Statstotal_connections_received: 280094total_commands_processed: Disabled: export _ output_kbps: 77.55rejected _ connections: 0sync_full: Role: 0keyspace_hits: Role: 195303 # Replicationrole: Role: 1slave0: ip = x.0.x. x, port = 6379, state = online, offset = 11332087470, lag = 0master_repl_offset: 11332106737 # the actual iprepl_backlog_active: 1repl_backlog_size: Disabled: 1048576 # CPUused_cpu_sys: 6134.57used _ cpu_user: 2565.36used _ cpu_sys_children: 8045.19used _ cpu_user_children: 97035.76 # Keyspacedb0: keys = 337475, expires = 180183, avg_ttl = 1286629728
2. analyze the actual memory consumption of 13 Gb
used_memory_human:13.21G
View the memory usage history of redis in zabbix in the last month
3.5 GB a month ago, linear growth to 13 Gb
Average expiration time
db0:keys=337475,expires=180183,avg_ttl=1286629728
Actual memory settings
[root@redis01 ~]# redis-cli config get maxmemory1) "maxmemory"2) "0"
3. Optimization Measure 3.1. Set the memory limit to prevent the maximum memory usage from being reached. Set the maximum memory usage to 10 Gb for downtime. Adjust the runtime first.
127.0.0.1:6379> config set maxmemory 10737418240OK(10.76s)127.0.0.1:6379> info......# Memoryused_memory:10730048552used_memory_human:9.99G
Set redis. conf to take effect after the next restart
maxmemory 10737418240
3.2 adjust the expiration time and clear the memory occupied by the expiration time keys
By default, the expiration time is transferred to development and adjusted. The unit of avg_ttl is ms. After the change, the average value is 15 days, which takes a long time. If no default expiration time is set for keys, it is transferred to development and adjusted;
3.3 adjust system parameters to ensure normal operation of bgsave
[root@redis01 ~]# cat /etc/sysctl.confvm.overcommit_memory = 1
Take effect after running the command
sysctl -p
3.4 analyze keys and find out the largest memory-used keys list. Submit the development analysis and install redis-rdb-tools.
Process omitted
Generate redis Snapshot
Rdb-c memory dump. rdb> memory.csv # dump. rdb is the dump file generated by redis.
Find the 20 objects with the maximum memory usage
sort -t, -k4nr memory.csv |heand -n 20