redis 4.0
本人是cluster的集群,本实例只显示一次demo。
知识点
Redis的内存淘汰
既然可以设置Redis最大占用内存大小,那么配置的内存就有用完的时候。那在内存用完的时候,还继续往Redis里面添加数据不就没内存可用了吗?
实际上Redis定义了几种策略用来处理这种情况:
noeviction(默认策略):对于写请求不再提供服务,直接返回错误(DEL请求和部分特殊请求除外)
allkeys-lru:从所有key中使用LRU算法进行淘汰
volatile-lru:从设置了过期时间的key中使用LRU算法进行淘汰
allkeys-random:从所有key中随机淘汰数据
volatile-random:从设置了过期时间的key中随机淘汰
volatile-ttl:在设置了过期时间的key中,根据key的过期时间进行淘汰,越早过期的越优先被淘汰
当使用volatile-lru、volatile-random、volatile-ttl这三种策略时,如果没有key可以被淘汰,则和noeviction一样返回错误
查看当前redis 信息
127.0.0.1:7000> info
# Server
redis_version:4.0.11
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:fb2c7e4cd1f51894
redis_mode:cluster
os:Linux 3.10.0-862.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:1676
run_id:2455112a5cd1a7b57c6a77bfb1efea59e2cbc2dd
tcp_port:7000
uptime_in_seconds:12373568
uptime_in_days:143
hz:10
lru_clock:9270930
executable:/usr/local/redis_cluster/bin/redis-server
config_file:/usr/local/redis_cluster/7000/redis.conf
# Clients
connected_clients:19
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:36241973
used_memory_human:34.56M
used_memory_rss:456327168
used_memory_rss_human:435.19M
used_memory_peak:3556716582
used_memory_peak_human:3.31G
used_memory_peak_perc:1.02%
used_memory_overhead:6023708
used_memory_startup:1392208
used_memory_dataset:30218265
used_memory_dataset_perc:86.71%
total_system_memory:45158117376
total_system_memory_human:42.06G
used_memory_lua:44032
used_memory_lua_human:43.00K
maxmemory:36700160
maxmemory_human:35.00M
maxmemory_policy:volatile-lru
mem_fragmentation_ratio:12.59
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:261
rdb_bgsave_in_progress:0
rdb_last_save_time:1586329089
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:27377664
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:3
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:18972672
aof_current_size:66467466
aof_base_size:34853373
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:8909
# Stats
total_connections_received:420908
total_commands_processed:529689844
instantaneous_ops_per_sec:12
total_net_input_bytes:32372857159
total_net_output_bytes:19414000423
instantaneous_input_kbps:0.82
instantaneous_output_kbps:1.54
rejected_connections:0
sync_full:4
sync_partial_ok:44
sync_partial_err:4
expired_keys:215737
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:4647
keyspace_hits:148169049
keyspace_misses:82187868
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:14521
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:1
slave0:ip=192.168.11.243,port=7005,state=online,offset=3196639014,lag=0
master_replid:70733f08c64d1a309c7c0ed800f435bd5539baf6
master_replid2:2fda6494f497c1f6549266a2de3614b89f52e787
master_repl_offset:3196639014
second_repl_offset:958986374
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:3195590439
repl_backlog_histlen:1048576
# CPU
used_cpu_sys:38435.42
used_cpu_user:20813.27
used_cpu_sys_children:6579.14
used_cpu_user_children:18832.50
# Cluster
cluster_enabled:1
# Keyspace
db0:keys=43834,expires=17127,avg_ttl=1077470502
获取当前内存淘汰策略和当前内存限制:
#默认是noeviction策略,由于我改过为 volatile-lru了
127.0.0.1:7000> config get maxmemory-policy
1) "maxmemory-policy"
2) "volatile-lru"
#我设置过为35M的内存了
127.0.0.1:7000> config get maxmemory
1) "maxmemory"
2) "36700160"
设置内存淘汰策略和当前内存限制
127.0.0.1:7000> config set maxmemory-policy allkeys-lru
OK
127.0.0.1:7000> config set maxmemory 100mb
OK
ps:如果不设置最大内存大小或者设置最大内存大小为0,在64位操作系统下不限制内存大小
参考官方资料:
lru-cache
Redis的内存淘汰策略
网友评论