設置Redis最大佔用內存

設置Redis最大佔用內存

Redis須要設置最大佔用內存嗎?若是Redis內存使用超出了設置的最大值會怎樣?html

設置Redis最大佔用內存

Redis設置最大佔用內存,打開redis配置文件,找到以下段落,設置maxmemory參數,maxmemory是bytes字節類型,注意轉換。修改以下所示:linux

Vimnginx

 

1git

2github

3redis

4算法

5編程

6api

# 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試試。

Vim

 

1

2

3

4

# 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 近期最少使用算法。

  1. volatile-lru -> 根據LRU算法生成的過時時間來刪除。
  2. allkeys-lru -> 根據LRU算法刪除任何key。
  3. volatile-random -> 根據過時設置來隨機刪除key。
  4. allkeys->random -> 無差異隨機刪。
  5. volatile-ttl -> 根據最近過時時間來刪除(輔以TTL)
  6. noeviction -> 誰也不刪,直接在寫操做時返回錯誤。

若是設置了maxmemory,通常都要設置過時策略。打開Redis的配置文件有以下描述,Redis有六種過時策略:

Vim

 

1

2

3

4

5

6

# 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的過時策略:

Vim

 

1

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左右。

 

轉載:https://blog.tanteng.me/2016/03/redis-maxmemory/

 

 

 

 

 

Redis配置

 

Redis能夠在沒有配置文件的狀況下經過內置的配置來啓動,可是這種啓動方式只適用於開發和測試

合理的配置Redis的方式是提供一個Redis配置文件,這個文件一般叫作 redis.conf

redis.conf文件中包含了不少格式簡單的指令以下:

keyword argument1 argument2 ... argumentN
關鍵字   參數1     參數2      ... 參數N

以下是一個配置指令的示例:

slaveof 127.0.0.1 6380

若是參數中含有空格,那麼能夠用雙引號括起來,以下:

requirepass "hello world"

這些指令的配置,意義以及深刻使用方法都能在每一個Redis發佈版本自帶的的redis.conf文檔中找到。

經過命令行傳參

自Redis2.6起就能夠直接經過命令行傳遞Redis配置參數。這種方法能夠用於測試。 如下是一個例子:這個例子配置一個新運行並以6380爲端口的 Redis實例,使配置它爲127.0.0.1:6379Redis實例的slave。

./redis-server --port 6380 --slaveof 127.0.0.1 6379

經過命令行傳遞的配置參數的格式和在redis.conf中設置的配置參數的格式徹底同樣, 惟一不一樣的是須要在關鍵字以前加上 前綴--

須要注意的是經過命令行傳遞參數的過程會在內存中生成一個臨時的配置文件(也許會直接追加在 命令指定的配置文件後面),這些傳遞的參數也會轉化爲跟Redis配置文件同樣的形式。

運行時配置更改

Redis容許在運行的過程當中,在不重啓服務器的狀況下更改服務器配置,同時也支持 使用特殊的CONFIG SET和 CONFIG GET命令用編程方式查詢並設置配置。

並不是全部的配置指令都支持這種使用方式,可是大部分是支持的。更多相關的信息請查閱CONFIG SET和 CONFIG GET頁面。

須要確保的是在經過CONFIG SET命令進行的設置的同時,也需在 redis.conf文件中進行了相應的更改。 將來Redis有計劃提供一個CONFIG REWRITE命令在不更改現有配置文件的同時, 根據當下的服務器配置對redis.conf文件進行重寫。

配置Redis成爲一個緩存

若是你想把Redis當作一個緩存來用,全部的key都有過時時間,那麼你能夠考慮 使用如下設置(假設最大內存使用量爲2M):

maxmemory 2mb
maxmemory-policy allkeys-lru

以上設置並不須要咱們的應用使用EXPIRE(或類似的命令)命令去設置每一個key的過時時間,由於 只要內存使用量到達2M,Redis就會使用類LRU算法自動刪除某些key。

相比使用額外內存空間存儲多個鍵的過時時間,使用緩存設置是一種更加有效利用內存的方式。並且相比每一個鍵固定的 過時時間,使用LRU也是一種更加推薦的方式,由於這樣能使應用的熱數據(更頻繁使用的鍵) 在內存中停留時間更久。

基本上這麼配置下的Redis能夠當成memcached使用。

當咱們把Redis當成緩存來使用的時候,若是應用程序同時也須要把Redis當成存儲系統來使用,那麼強烈建議 使用兩個Redis實例。一個是緩存,使用上述方法進行配置,另外一個是存儲,根據應用的持久化需求進行配置,而且 只存儲那些不須要被緩存的數據。

請注意:用戶須要詳細閱讀示例redis.conf文件來決定使用什麼內存上限處理策略。

 

轉載:http://www.redis.cn/topics/config.html

相關文章
相關標籤/搜索