The inspection and scoring feature of Database Autonomy Service (DAS) takes the memory fragmentation ratio of Redis into consideration. This topic describes the definition, causes, and threshold values of memory fragments in Redis.

What are memory fragments in Redis

For example, assume that an operating system has an available memory space of larger than N bytes, but its available memory is made up of memory segments smaller than N bytes. Therefore, when Redis requests a contiguous memory segment of N bytes, the operating system fails to provide it. In this case, the memory segments smaller than N bytes are memory fragments.

In ApsaraDB for Redis, memory fragments do not affect the available memory that is allocated to instances and do not incur fees.
Note For example, assume that the memory capacity of an ApsaraDB for Redis instance is 1 GB, and 700 MB of the memory capacity has been used. Even if the instance has a memory fragmentation ratio of 2, which means its data actually occupies 2 GB of system memory, the instance still has 300 MB of available memory space (1 GB - 700 MB).

What causes memory fragments

Memory fragments are caused by both internal and external reasons.
  • Internal reason: The mechanism of Redis memory allocators cannot allocate memory fully based on your requirements.
    • Redis uses multiple memory allocators, including libc, jemalloc, and tcmalloc. jemalloc is the default memory allocator.
    • A memory allocator allocates memory by a fixed set of amounts, not based on the amount of memory an application actually requests.

      For example, jemalloc always divides memory space by the power of two, such as 8 bytes, 16 bytes, 32 bytes, 2KB, and 4KB. When an application requests a certain amount of memory, jemalloc chooses an amount that is the closest to the requested amount and allocates that amount of memory to the application.

  • External reason: Key-value pairs are in difference sizes and can be modified or deleted.
    • Due to the memory allocation mechanism, the memory actually allocated to an application is slightly larger than the requested amount in most cases. The extra memory becomes memory fragments.
    • Key-value pairs can be modified or deleted, and the used memory space is changed or released in the process.

How to check whether memory fragments exist in a Redis instance

You can use the following INFO command provided by Redis in DAS to query detailed information about memory usage:
INFO memory
# Memory
used_memory:350458970752
used_memory_human:326.39G
used_memory_rss:349066919936
used_memory_rss_human:325.09G
…
mem_fragmentation_ratio:1.00
  • used_memory: the amount of memory that Redis requests to store data.
  • used_memory_rss: the amount of physical memory that the operating system allocates to Redis. The sizes of memory fragments are counted in this value.
  • mem_fragmentation_ratio: the current memory fragmentation ratio of Redis.
    The memory fragmentation ratio is calculated based on the following formula: mem_fragmentation_ratio = used_memory_rss/used_memory
    • A mem_fragmentation_ratio value from 1 to 1.5 indicates an acceptable fragmentation ratio.
    • If the mem_fragmentation_ratio value is larger than 1.5, memory fragments whose size is over half the size of the used memory exist in the instance. We recommend that you take immediate actions in this case.