天天看點

設定Redis最大占用記憶體Redis設定記憶體最大占用值:

設定Redis最大占用記憶體

Redis需要設定最大占用記憶體嗎?如果Redis記憶體使用超出了設定的最大值會怎樣?

設定Redis最大占用記憶體

Redis設定最大占用記憶體,打開redis配置檔案,找到如下段落,設定maxmemory參數,maxmemory是bytes位元組類型,注意轉換。修改如下所示:

# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory 268435456
           

本機伺服器redis配置檔案路徑:/etc/redis/6379.conf,由于本機自帶記憶體隻有1G,一般推薦Redis設定記憶體為最大實體記憶體的四分之三,是以設定0.75G,換成byte是751619276.

可以在CentOS下輸入指令:find / -name redis查找redis目錄:

[root@iZ94r80gdghZ ~]# find / -name redis
/usr/share/nginx/html/blog.tanteng.me/wp-content/cache/supercache/blog.tanteng.me/tag/redis
/etc/redis
/var/redis
           

Redis配置檔案一般在etc下的redis安裝目錄下。

Redis使用超過設定的最大值

如果Redis的使用超過了設定的最大值會怎樣?我們來改一改上面的配置,故意把最大值設為1個byte試試。

# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory 1
           

打開debug模式下的頁面,提示錯誤:OOM command not allowed when used memory > ‘maxmemory’.

設定了maxmemory的選項,redis記憶體使用達到上限。可以通過設定LRU算法來删除部分key,釋放空間。預設是按照過期時間的,如果set時候沒有加上過期時間就會導緻資料寫滿maxmemory。

如果不設定maxmemory或者設定為0,64位系統不限制記憶體,32位系統最多使用3GB記憶體。

LRU是Least Recently Used 近期最少使用算法。

  • volatile-lru -> 根據LRU算法生成的過期時間來删除。
  • allkeys-lru -> 根據LRU算法删除任何key。
  • volatile-random -> 根據過期設定來随機删除key。
  • allkeys->random -> 無差别随機删。
  • volatile-ttl -> 根據最近過期時間來删除(輔以TTL)
  • noeviction -> 誰也不删,直接在寫操作時傳回錯誤。

如果設定了maxmemory,一般都要設定過期政策。打開Redis的配置檔案有如下描述,Redis有六種過期政策:

# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
           

那麼打開配置檔案,添加如下一行,使用volatile-lru的過期政策:

maxmemory-policy volatile-lru
           

儲存檔案退出,重新開機redis服務。

info指令檢視Redis記憶體使用情況

如伺服器Redis所在目錄:/usr/local/redis-3.0.7/src

在終端輸入./redis-cli,打開Redis用戶端,輸入info指令。

出來如下資訊:

[root@iZ94r80gdghZ src]# ./redis-cli
127.0.0.1:6379> info
# Server
redis_version:3.0.7
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:f07a42660a61a05e
redis_mode:standalone
os:Linux 3.10.0-327.10.1.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.8.5
process_id:2165
run_id:8ec8a8dc969d6e2f2867d9188ccb90850bfc9acb
tcp_port:6379
uptime_in_seconds:668
uptime_in_days:0
hz:10
lru_clock:15882419
config_file:/etc/redis/6379.conf

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

# Memory
used_memory:816232
used_memory_human:797.10K
used_memory_rss:7655424
used_memory_peak:816232
used_memory_peak_human:797.10K
used_memory_lua:36864
mem_fragmentation_ratio:9.38
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1458722327
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
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: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
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
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:0.30
used_cpu_user:0.29
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Cluster
cluster_enabled:0

# Keyspace
db0:keys=1,expires=1,avg_ttl=425280
           

其中used_memory:816232,僅用了0.7M左右。

本文在網上查閱資料,在阿裡雲CentOS主機實踐。

Redis設定記憶體最大占用值:

Redis設定占用實體機最大的記憶體

#占用最大20G
maxmemory 20480mb           

Redis設定記憶體裝不下了,有限删除即将過期的

目前已用記憶體超過maxmemory限定時,觸發主動清理政策

  • volatile-lru:隻對設定了過期時間的key進行LRU(預設值)
  • allkeys-lru : 删除lru算法的key
  • volatile-random:随機删除即将過期key
  • allkeys-random:随機删除
  • volatile-ttl : 删除即将過期的
  • noeviction : 永不過期,傳回錯誤當mem_used記憶體已經超過maxmemory的設定,對于所有的讀寫請求;
#超過後删除即将過期的
maxmemory-policy  volatile-ttl