Databases 9 min read

Redis Memory Analysis and Management: Fragmentation, Usage, and Optimization

This article explains how Redis uses memory, covering memory fragmentation analysis, object and buffer memory consumption, sub‑process memory overhead, and practical configuration of maxmemory limits and eviction policies to optimize performance and prevent out‑of‑memory failures.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Redis Memory Analysis and Management: Fragmentation, Usage, and Optimization

Redis relies heavily on memory, making memory analysis essential for performance tuning.

1. Redis Memory Analysis

Use the INFO MEMORY command to view memory statistics. The fragment ratio can be calculated as 226893824/209522728≈1.08, indicating memory fragmentation; the allocator used is jemalloc.

The used_memory_rss value is usually larger than used_memory because of fragmentation, but when the OS swaps Redis memory to disk, the memory_fragmentation_ratio may drop below 1, severely impacting performance.

2. Redis Memory Usage

Redis memory consists of the process overhead (negligible), object memory (actual stored data), buffer memory (client buffers, replication backlog, AOF buffer), and fragmentation.

Object memory is essentially key-size + value-size. Keys are strings; values can be strings, lists, hashes, sets, sorted sets, bitmaps, HyperLogLog, etc. Proper key design and data type selection are crucial.

2.1 Buffer Memory

Buffer memory includes client buffers, replication backlog, and AOF buffer.

Client Buffers

TCP input buffers can grow up to 1 GB; output buffers are configurable via client-output-buffer-limit. Example for slave clients: client-output-buffer-limit slave 256mb 64mb 60. Pub/Sub clients default to client-output-buffer-limit pubsub 32mb 8mb 60. Normal clients often have no explicit limit.

Replication Backlog

Configured with repl-backlog-size (default 1 MB) to store partial replication data for slaves.

AOF Buffer

During AOF rewrite, incremental writes are stored in a buffer whose size depends on rewrite duration and write volume.

3. Sub‑process Memory Consumption

When Redis performs persistence (RDB/AOF), it forks a child process. The child shares memory pages with the parent until writes occur (copy‑on‑write). Linux Transparent Huge Pages (THP) can increase page size to 2 MB, slowing fork; disabling THP is recommended for high‑concurrency workloads.

Set vm.overcommit_memory=1 to allow the OS to allocate all physical memory needed for forks.

4. Redis Memory Management

4.1 Memory Limit (maxmemory)

Define an upper bound for Redis memory to trigger eviction and prevent the server from exhausting physical RAM. Adjust dynamically with CONFIG SET maxmemory <bytes>. Consider fragmentation when setting limits.

4.2 Memory Eviction Policies

When memory reaches maxmemory, Redis applies the policy defined by maxmemory-policy:

noeviction – stop writes, return errors.

volatile‑lru – LRU eviction of keys with an expiration.

allkeys‑lru – LRU eviction of any key.

allkeys‑random – random key eviction.

volatile‑random – random eviction of expiring keys.

volatile‑ttl – evict keys with nearest expiration.

Configure with CONFIG SET maxmemory-policy <policy>. Ensure maxmemory > used_memory to avoid immediate eviction.

Both fast and slow eviction modes use the same deletion logic; only the timing differs.
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.

performanceCacheMemory Managementredislinuxdatabases
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.