Set Redis maximum memory consumption

Source: Internet
Author: User
Tags allkeys

54945667

Does Redis need to set the maximum memory consumption? What if the Redis memory usage exceeds the set maximum value?

Set Redis maximum memory consumption

Redis sets the maximum memory footprint, opens the Redis profile, finds the following paragraph, sets the MaxMemory parameter, MaxMemory is the bytes byte type, note the conversion. The changes are as follows:

Vim
123456 # in < Span class= "crayon-i" >short. if you  have slaves attached  it is suggested < Span class= "CRAYON-E" >that you set  a lower # limit  for maxmemory  so that there  is some free  ram on the  system for  slave # output buffers ( needed if the policy C15>is ' noeviction '). ## maxmemory <bytes> maxmemory 268435456

Native Server Redis profile path:/etc/redis/6379.conf, since the native memory is only 1 g, it is generally recommended that Redis set memory to three-fourths of the maximum physical memory, so set 0.75G, and Byte is 751619276.

You can enter a command under CentOS: Find/-name Redis finds Redis directory:

[[email protected] ~]# Find/-name Redis
/usr/share/nginx/html/blog.tanteng.me/wp-content/cache/supercache/blog.tanteng.me/tag/redis
/etc/redis
/var/redis

Redis profiles are typically under the Redis installation directory under etc.

Redis uses a maximum value of over set

What if the use of Redis exceeds the maximum set value? Let's change the configuration above and deliberately set the maximum value to 1 bytes to try.

Vim
1234 # output buffers ( needed if the policy is ' noeviction '). ## maxmemory <bytes> maxmemory 1

Open debug Mode page, prompt error: OOM command not allowed when used memory > ' maxmemory '.

The MaxMemory option is set, and Redis memory usage reaches the upper limit. You can free up space by setting the LRU algorithm to remove some keys. By default, if the set time is not added, the expiration time will result in the data being filled maxmemory.

If you do not set maxmemory or set to 0, 64-bit systems do not limit memory, and 32-bit systems use up to 3GB of memory.

LRU is the least recently used algorithm for least recently used.

    1. VOLATILE-LRU is removed based on the expiration time generated by the LRU algorithm.
    2. ALLKEYS-LRU, remove any key according to the LRU algorithm.
    3. Volatile-random----deletes keys randomly based on expiration settings.
    4. Allkeys->random-no-difference random deletion.
    5. Volatile-ttl---based on the most recent expiration time (supported by TTL)
    6. Noeviction, who does not delete, the error is returned directly in the write operation.

If you set up MaxMemory, you typically set an expiration policy. The profile that opens Redis has the following description, and Redis has six expiration policies:

Vim
123456 # volatile-< Span class= "crayon-i" >lru ->  remove the key with an expire  set using  an lru algorithm # allkeys-< Span class= "crayon-i" >lru ->  remove any key accordingly to the  lru algorithm # volatile-< Span class= "crayon-i" >random ->  remove a  random key  with an expire set # allkeys-< Span class= "crayon-i" >random ->  remove a  random key , any  Key # volatile-< Span class= "crayon-i" >ttl ->  remove the key with the nearest  expire time  (minor ttl) # noeviction don 't expire at all , just Return an error on write operations

Open the configuration file, add the following line, and use the VOLATILE-LRU expiration policy:

Vim
1 MaxMemory-policy volatile-LRU

Save the file to exit and restart the Redis service.

Info command to view Redis memory usage

directory where Server Redis resides:/USR/LOCAL/REDIS-3.0.7/SRC

In terminal input./REDIS-CLI, open the Redis client and enter the info command.

Out the following information:

[Email protected] 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

One of the used_memory:816232, only used about 0.7M.

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

Redis Configuration

Redis can be started with a built-in configuration without a configuration file, but this startup method is only available for development and testing.

A reasonable way to configure Redis is to provide a Redis configuration file, which is often called redis.conf .

The redis.conf file contains a number of simple-to-format instructions as follows:

    1. Keyword argument1 argument2 ... argumentn
    2. Keyword parameter 1 parameter 2 ... Parameter n

The following is an example of a configuration directive:

slaveof 127.0.0.1 6380

If the argument contains spaces, you can enclose it in double quotation marks, as follows:

"hello world"

The configuration, meaning, and in-depth use of these instructions can be found in the redis.conf documentation that comes with each Redis release version.

    • Self-describing document REDIS.CONF for Redis 2.8
    • Self-describing document REDIS.CONF for Redis 2.6.
    • Self-describing document REDIS.CONF for Redis 2.4.
To pass a parameter through the command line

Redis configuration parameters can be passed directly from the command line from Redis2.6 onwards. This method can be used for testing. Here is an example: this example configures a newly run Redis instance with a port of 6380, so that it is configured as a slave for the 127.0.0.1:6379redis instance.

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

The format of the configuration parameters passed through the command line is exactly the same as the format of the configuration parameters set in redis.conf, except that a prefix is required before the keyword -- .

It is important to note that the process of passing parameters through the command line generates a temporary configuration file in memory (which may be appended directly to the configuration file specified by the command), and these passed parameters are converted to the same form as the Redis configuration file.

Run-time configuration changes

Redis allows you to change the server configuration without restarting the server while running, while also enabling you to programmatically query and set the configuration using special config set and config get commands.

Not all configuration directives support this usage, but most of them are supported. Please refer to config Set and config get page for more relevant information.

It is necessary to make sure that the settings that are made through the Config set command are also changed in the redis.conf file. Future Redis has a plan to provide a config rewrite command to rewrite the redis.conf file based on the current server configuration while not changing the existing configuration file.

Configure Redis to be a cache

If you want to use Redis as a cache and all keys have an expiration time, you might consider using the following settings (assuming a maximum memory usage of 2M):

    1. MaxMemory 2MB
    2. Maxmemory-policy ALLKEYS-LRU

The above settings do not require our app to use the expire (or similar command) command to set the expiration time for each key, because some keys are automatically deleted using the class LRU algorithm as long as memory usage reaches 2m,redis.

Using cache settings is a more efficient way to use memory than to store the expiration time of multiple keys with extra memory space. It is also a more recommended way to use LRU than when each key is fixed, because it allows the application's hot data (more frequently used keys) to stay in memory longer.

Redis is basically configured so that it can be used as a memcached.

When we use Redis as a cache, it is highly recommended to use two Redis instances if the application also needs to use Redis as a storage system. One is the cache, which is configured using the above method, the other is storage, is configured according to the application's persistence requirements, and only the data that does not need to be cached is stored.

Note : Users need to read the sample redis.conf file in detail to determine what memory cap processing policy to use.

Set Redis maximum memory consumption

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.