Redis memory model
Redis memory usage is mainly divided into the following parts:
- The data.
- Memory occupied by processes running. In used_memory_rss, the memory occupied by the main Redis process is not counted.
- Buffer memory, including:
- Client buffer: The input/output buffer that stores client connections
- Replication backlog buffer: used for primary/secondary replication
- AOF buffer: Used to save the most recent write command when AOF is overwritten
- Memory fragmentation. The solution to memory fragmentation is reboot.
Redis memory statistics
You can use the info memory command to view the memory usage of Redis.
127.0.0.1:6379> info memory
# Memory
used_memory:708288
used_memory_human:691.69K
used_memory_rss:671360
used_memory_rss_human:655.63K
used_memory_peak:709144
used_memory_peak_human:692.52K
total_system_memory:0
total_system_memory_human:0B
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:0.95
mem_allocator:jemalloc- 3.6.0
Copy the code
- Used_memory: The total amount of memory allocated by the Redis memory allocator, including the virtual memory used.
- Used_memory_rss: Redis processes occupy the memory of the operating system, which is consistent with the values seen by the top and ps commands; In addition to the memory allocated by the allocator, usED_memory_RSS includes memory needed by the process itself to run, memory fragments, and so on, but not virtual memory.
- Used_memory_peak: redis memory consumption peak.
- Used_memory_lua: memory size used by the Lua engine.
- Mem_fragmentation_radio: ratio of USED_memory_RSS to USED_momory.
- Mem_allocator: memory allocator used by Redis, jemalloc by default.
Ideally, the value of USED_memory_RSS should be only slightly higher than usED_memory. When RSS > used and the difference is large, internal or external memory fragmentation exists. The fragmentation ratio can be determined by the value of mem_fragmentation_radio. When used > RSS, it indicates that part of redis memory has been swapped out into swap space by the operating system. In this case, the operation may cause significant latency.
Redis monitor command
The monitor command is used to print the commands received by the Redis server for debugging.
It sends all commands executed on the Redis server to each monitor. In the case of high concurrency, it is easy to increase the memory usage of the output buffer of the monitor client.