Unlocking Redis Memory: How to Measure, Understand, and Optimize Its Usage

This article explains how to monitor Redis memory usage with the INFO command, interprets key metrics such as used_memory, used_memory_rss, and mem_fragmentation_ratio, and dives into Redis's internal memory layout, including allocators, redisObject, SDS, and the various object types and their encodings.

Efficient Ops
Efficient Ops
Efficient Ops
Unlocking Redis Memory: How to Measure, Understand, and Optimize Its Usage

1. Redis Memory Statistics

Before discussing Redis memory, we first explain how to collect memory usage. Connect with redis-cli and run info memory to display memory‑related fields. Important fields include:

used_memory : total memory allocated by Redis allocator (bytes), includes virtual memory.

used_memory_rss : memory the Redis process occupies in the OS (bytes), matches values from top/ps and also includes process overhead and fragmentation.

mem_fragmentation_ratio : ratio used_memory_rss / used_memory, indicating memory fragmentation. Values >1 mean fragmentation; values <1 suggest heavy swapping.

mem_allocator : allocator used (libc, jemalloc, tcmalloc); default is jemalloc.

When used_memory_rss / used_memory is around 1.03, the state is considered healthy for jemalloc.

2. Redis Memory Partition

Redis memory is divided into several parts:

Data

All key‑value pairs are stored here and counted in used_memory . Values can be strings, hashes, lists, sets, or sorted sets, each with multiple internal encodings.

Process overhead

The main Redis process consumes a few megabytes for code, constant pools, etc. This memory is not allocated by jemalloc and therefore not included in used_memory . Child processes created for AOF or RDB rewriting also use memory but are not counted in used_memory or used_memory_rss .

Buffer memory

Includes client output buffers, replication backlog, and AOF buffers. These buffers are allocated by jemalloc and are part of used_memory .

Memory fragmentation

Fragmentation occurs during allocation and reclamation. Large fragmentation increases mem_fragmentation_ratio . Restarting Redis can reduce fragmentation by rebuilding data structures.

3. Details of Redis Data Storage

3.1 Overview

Redis data storage involves the memory allocator (jemalloc), Simple Dynamic Strings (SDS), five object types, and the redisObject wrapper. The following diagram shows the model when executing SET hello world:

Key is stored in an SDS structure, while the value is wrapped in a redisObject that points to the actual data.

3.2 jemalloc

Redis can be compiled with libc, jemalloc, or tcmalloc; the default is jemalloc, which reduces fragmentation. jemalloc divides memory into small, large, and huge classes, each further split into size‑classes. For example, a 130‑byte object is placed into a 160‑byte slot.

3.3 redisObject

The redisObject structure (16 bytes) holds metadata for a value:

type : object type (string, list, hash, set, sorted set).

encoding : internal encoding (e.g., int, embstr, raw for strings).

lru : last access time used for LRU eviction.

refcount : reference count; values >1 indicate shared objects (currently only integer strings).

ptr : pointer to the actual data (e.g., an SDS for a string).

The size calculation is:

4bit+4bit+24bit+4Byte+8Byte=16Byte。

3.4 SDS (Simple Dynamic String)

SDS replaces C strings with a structure that stores length and free space:

struct sdshdr {
    int len;
    int free;
    char buf[];
};

This design provides O(1) length queries, prevents buffer overflows, and supports binary data.

4. Redis Object Types and Internal Encodings

Redis supports five object types, each with at least two possible encodings. Encodings are chosen based on size and element count, and conversion only occurs from a more compact encoding to a larger one.

String

int – 8‑byte integer.

embstr – ≤39 bytes, stored as a single allocation.

raw – >39 bytes, stored with separate allocations.

List

Encoded as ziplist (compressed list) when element count < 512 and each element < 64 bytes; otherwise as a doubly linked list.

Hash

Internal encoding can be ziplist or hashtable, with the same size/length thresholds as lists.

Set

Encoded as intset when all elements are integers and count < 512; otherwise as a hashtable.

Sorted Set

Encoded as ziplist for < 128 elements with short members; otherwise as a skiplist.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend DevelopmentredisData Structuresjemalloc
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.