Home > Database > Redis > body text

Redis memory monitoring and management

齐天大圣
Release: 2020-05-25 15:19:43
Original
1843 people have browsed it

All data in Redis is stored in memory, so memory monitoring and management are very important for redis. Let's talk about redis memory from three aspects: memory monitoring, management and optimization.

Memory Monitoring

redis provides us with the info memory command to view memory usage. This command displays a lot of data, here we only look at some of the more important ones.

  • used_memory:1146648 (Amount of memory allocated by the redis allocator)

  • used_memory_human:1.09M (displayed in a readable manner)

  • used_memory_rss:4358144 (Total memory occupied by redis)

  • used_memory_rss_human:4.16M (Readable display of total memory occupied by redis)

  • used_memory_peak:1229448 (userd_memory_peak peak)

  • used_memory_peak_human:1.17M

  • used_memory_overhead:857036 (buffer, backlog, etc.)

  • used_memory_startup:791384 (memory consumed by starting redis)

  • used_memory_dataset:289612 (memory occupied by redis data )

  • used_memory_lua:37888 (memory consumed by lua script)

  • used_memory_lua_human:37.00K (readable display)

  • mem_fragmentation_ratio:4.02 (memory fragmentation rate, used_memory_rss / used_memory)

When mem_fragmentation_ratio is greater than 1, it indicates that there is memory fragmentation. The lower the value, the more serious the fragmentation rate. If the application allows it, redis can be restarted to reduce the fragmentation rate. When it is less than 0, you need to be very careful. It means that the memory is not enough and part of the swap is used.

Memory management

It is generally recommended to set the upper memory limit for redis, maxmory

There are two ways to modify it

  • Modify the configuration file

  • Dynamic settings

The dynamic settings are shown below

127.0.0.1:6379> config set maxmemory 1G
OK
127.0.0.1:6379> config rewrite
OK
Copy after login

Set the maximum Memory has two functions. First, it can limit the memory size occupied by redis to prevent it from exceeding the physical memory size. In addition, when the maximum memory is set and redis exceeds the maximum memory, the memory recycling strategy can be triggered (usually applied to cache scenarios)

Redis can dynamically adjust the maximum memory, which is very useful when server memory If it is not enough, you need to increase the memory. When the server memory increases, dynamically adjust the mammemory of redis.

In addition, there are several configurations of redis's memory recycling policy (maxmemory-policy), among which two words often appear, LRU and LFU, and their meanings are as follows:

  • Least Recently Used

  • Least Frequently Used

Let’s take a look at all Recycling strategy and meaning:

  • noeviction: Default policy, no keys will be deleted. When the maximum memory is exceeded, any write command will be rejected and only read commands will be responded to

  • volatile-lru: Delete expired keys according to the lru algorithm. If there are no keys that can be deleted, fall back to the noeviction strategy.

  • allkeys-lru: Delete keys according to the lru algorithm, regardless of whether the key has expired, until the memory occupied is less than maxmeory.

  • allkeys-lfu: has the same meaning as above, but uses the lfu algorithm to delete.

  • allkeys-random: Randomly delete keys when the maximum memory is reached.

  • volatile-ttl: Delete keys that are about to expire. If not, fallback to noeviction.

Memory Optimization

The above mentioned redis recycling strategy, it is a passive deletion strategy, we can actively delete it Keys that have not been accessed for a long time. You can use scan to traverse the keys and then use object ideltime to view the idle time of the keys and delete those keys that have not been accessed for a long time.

In addition, you can also reduce the memory usage by reducing key-value objects.

127.0.0.1:6379> scan 0
1) "272"
2)  1) "story:course:3915"
    2) "story:course:1681"
    3) "story:course:4982"
……
Copy after login

For example, the above key can be changed to: sy:cos:id to reduce the length of the key. For , values, compression techniques can be used to reduce the length of the value. (Compression will increase the pressure on the CPU and can be applied as appropriate according to specific applications)

The above is the detailed content of Redis memory monitoring and management. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template