Optimize Redis 7 memory usage and performance with advanced configuration tuning

Intermediate 25 min Apr 03, 2026 1,636 views
Ubuntu 24.04 Ubuntu 22.04 Debian 12 AlmaLinux 9 Rocky Linux 9 Fedora 41

Learn to optimize Redis 7 memory consumption and performance through advanced configuration tuning, including eviction policies, compressed data structures, active defragmentation, and comprehensive monitoring techniques.

Prerequisites

  • Redis 7 installed and running
  • Root or sudo access
  • Basic understanding of Redis configuration
  • At least 4GB available RAM

What this solves

Redis memory optimization is critical for high-performance applications that handle large datasets or serve thousands of concurrent connections. Without proper configuration, Redis can consume excessive memory, experience performance bottlenecks, or evict important data unexpectedly.

This tutorial shows you how to optimize Redis 7 memory usage through advanced configuration tuning, including proper eviction policies, compressed data structures, active memory defragmentation, and effective monitoring strategies for production environments.

Prerequisites and preparation

Verify Redis installation and version

Ensure you have Redis 7 installed and running. If not, follow our comprehensive installation guide first.

redis-server --version
sudo systemctl status redis

Create configuration backup

Always backup your existing Redis configuration before making changes.

sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.backup
echo "Backup created: $(date)" | sudo tee -a /etc/redis/redis.conf.backup

Install monitoring tools

Install redis-cli and system monitoring utilities for performance analysis.

sudo apt update
sudo apt install -y redis-tools htop iotop
sudo dnf install -y redis htop iotop

Configure memory allocation and eviction policies

Set maximum memory limit

Configure Redis to use a specific amount of system memory to prevent out-of-memory conditions.

sudo nano /etc/redis/redis.conf

Add or modify these memory settings based on your available RAM:

# Set max memory to 75% of available RAM (adjust for your system)
maxmemory 6gb

# Configure memory policy for key eviction
maxmemory-policy allkeys-lru

# Memory usage sampling for eviction decisions
maxmemory-samples 5

# Enable memory usage tracking
maxmemory-eviction-tenacity 10

Configure optimal eviction policy

Choose the eviction policy that matches your application's data access patterns.

# For cache workloads with mixed data types
maxmemory-policy allkeys-lru

# For applications with TTL-based expiration
# maxmemory-policy volatile-lru

# For least frequently used eviction
# maxmemory-policy allkeys-lfu

# For random eviction (fastest but least optimal)
</code><h2><code>maxmemory-policy allkeys-random</code></h2></pre>
</div>

<div class="info"><strong>Note:</strong> The allkeys-lru policy works best for general caching scenarios, while volatile-lru only evicts keys with TTL set, preserving persistent data.</div>

# Optimize Redis 7 compressed data structures

<div class="step">
### Configure hash field compression
<p>Enable efficient memory usage for hash data structures with compression thresholds.</p>
<pre class="terminal"><code># Hash compression settings
hash-max-listpack-entries 512
hash-max-listpack-value 64

# List compression for better memory efficiency
list-max-listpack-size -2
list-compress-depth 1

# Set compression settings
set-max-intset-entries 512
set-max-listpack-entries 128
set-max-listpack-value 64

Enable sorted set optimization

Configure memory-efficient storage for sorted sets and hyperloglogs.

# Sorted set optimization
zset-max-listpack-entries 128
zset-max-listpack-value 64

# HyperLogLog sparse representation
hll-sparse-max-bytes 3000

# Stream configuration for memory efficiency
stream-node-max-bytes 4kb
stream-node-max-entries 100

Configure key expiration settings

Optimize how Redis handles key expiration to reduce memory overhead.

# Active expiration frequency (1-10, higher = more CPU, less memory)
hz 10

# Lazy expiration settings
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
replica-lazy-flush yes

Configure active memory defragmentation

Enable active defragmentation

Configure Redis 7's active defragmentation to reduce memory fragmentation automatically.

# Enable active defragmentation
activedefrag yes

# Minimum percentage of fragmentation to start defragmentation
active-defrag-ignore-bytes 100mb
active-defrag-threshold-lower 10

# Maximum percentage of fragmentation to trigger aggressive defrag
active-defrag-threshold-upper 100

# CPU percentage limits for defragmentation
active-defrag-cycle-min 1
active-defrag-cycle-max 25

# Maximum scan fields in main dictionary scan
active-defrag-max-scan-fields 1000

Configure jemalloc memory allocator

Optimize jemalloc settings for better memory management and reduced fragmentation.

sudo mkdir -p /etc/redis/conf.d
# Jemalloc configuration for Redis
# Set background thread for memory purging
jemalloc-bg-thread yes

# Memory purge frequency
memory-purge-frequency 1

Include additional configuration

Add the memory configuration to your main Redis config.

# Include additional memory configurations
include /etc/redis/conf.d/memory.conf

Optimize connection pooling and client buffering

Configure connection limits and timeouts

Set optimal connection limits and timeout values to prevent memory exhaustion from idle connections.

# Maximum number of connected clients
maxclients 10000

# Client connection timeout (0 disables timeout)
timeout 300

# TCP keepalive
tcp-keepalive 300

# TCP backlog queue length
tcp-backlog 511

Configure client output buffers

Set client output buffer limits to prevent memory exhaustion from slow clients.

# Client output buffer limits
# Format: class hard-limit soft-limit soft-seconds
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60

# Query buffer limit for client connections
client-query-buffer-limit 1gb

# Protocol buffer limit
proto-max-bulk-len 512mb

Enable advanced networking optimizations

Configure Redis networking settings for better performance and reduced memory overhead.

# Enable SO_REUSEPORT for better load distribution
so-reuseport yes

# Disable protected mode if using authentication
# protected-mode no

# Enable multithreading for I/O operations
io-threads 4
io-threads-do-reads yes
Warning: Only disable protected-mode if you have proper authentication and firewall rules in place. Never expose an unprotected Redis instance to the internet.

Configure monitoring and logging

Enable detailed logging and monitoring

Configure comprehensive logging for memory usage tracking and performance analysis.

# Logging configuration
loglevel notice
logfile /var/log/redis/redis-server.log

# Enable slow query logging
slowlog-log-slower-than 10000
slowlog-max-len 128

# Latency monitoring
latency-monitor-threshold 100

# Memory usage sampling
sampling-stats-enabled yes

Create log directory and set permissions

Ensure proper permissions for Redis log files.

sudo mkdir -p /var/log/redis
sudo chown redis:redis /var/log/redis
sudo chmod 755 /var/log/redis

Restart Redis with new configuration

Apply all configuration changes by restarting Redis service.

sudo systemctl restart redis
sudo systemctl status redis

Monitor memory usage and performance

Check Redis memory statistics

Use redis-cli to monitor current memory usage and performance metrics.

redis-cli info memory
redis-cli info stats
redis-cli info clients

Monitor memory usage in real-time

Use continuous monitoring commands to track memory usage patterns.

# Monitor memory usage every 2 seconds
redis-cli --stat -i 2

# Monitor specific memory metrics
redis-cli info memory | grep used_memory_human

# Check fragmentation ratio
redis-cli info memory | grep mem_fragmentation_ratio

Set up automated memory monitoring

Create a monitoring script for continuous memory usage tracking.

#!/bin/bash

# Redis memory monitoring script
LOG_FILE="/var/log/redis/memory-monitor.log"
ALERT_THRESHOLD=80  # Alert when memory usage exceeds 80%

while true; do
    TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
    MEMORY_INFO=$(redis-cli info memory)
    
    USED_MEMORY=$(echo "$MEMORY_INFO" | grep used_memory: | cut -d: -f2 | tr -d '\r')
    MAX_MEMORY=$(echo "$MEMORY_INFO" | grep maxmemory: | cut -d: -f2 | tr -d '\r')
    FRAGMENTATION=$(echo "$MEMORY_INFO" | grep mem_fragmentation_ratio: | cut -d: -f2 | tr -d '\r')
    
    if [ "$MAX_MEMORY" -gt 0 ]; then
        USAGE_PERCENT=$((USED_MEMORY * 100 / MAX_MEMORY))
        echo "$TIMESTAMP - Memory Usage: ${USAGE_PERCENT}%, Fragmentation: $FRAGMENTATION" >> "$LOG_FILE"
        
        if [ "$USAGE_PERCENT" -gt "$ALERT_THRESHOLD" ]; then
            echo "$TIMESTAMP - ALERT: Memory usage above ${ALERT_THRESHOLD}%" >> "$LOG_FILE"
        fi
    fi
    
    sleep 300  # Check every 5 minutes
done
sudo chmod 755 /usr/local/bin/redis-memory-monitor.sh
sudo chown redis:redis /usr/local/bin/redis-memory-monitor.sh

Verify your setup

Test your Redis memory optimization configuration with these verification commands:

# Check Redis is running with new config
sudo systemctl status redis
redis-cli ping

# Verify memory settings
redis-cli config get maxmemory
redis-cli config get maxmemory-policy

# Check defragmentation status
redis-cli info memory | grep active_defrag

# Monitor current memory usage
redis-cli info memory | grep -E "used_memory_human|mem_fragmentation_ratio|maxmemory_human"

# Test connection limits
redis-cli config get maxclients

# Verify slow query logging
redis-cli slowlog len
redis-cli config get slowlog-log-slower-than

Performance testing and validation

Run memory benchmark tests

Use redis-benchmark to test memory performance with your new configuration.

# Test SET operations with different data sizes
redis-benchmark -t set -n 100000 -d 100
redis-benchmark -t set -n 50000 -d 1000
redis-benchmark -t set -n 10000 -d 10000

# Test hash operations
redis-benchmark -t hset -n 100000

# Monitor memory usage during benchmark
redis-cli --stat -i 1

Test eviction policies

Verify that your eviction policy works correctly under memory pressure.

# Set a low memory limit for testing
redis-cli config set maxmemory 100mb

# Add test data to trigger eviction
for i in {1..10000}; do
    redis-cli set "test:$i" "$(head -c 1024 < /dev/urandom | base64)"
done

# Check evicted keys count
redis-cli info stats | grep evicted_keys

# Restore original memory limit
redis-cli config set maxmemory 6gb

Common issues

SymptomCauseFix
High memory fragmentation ratio (>1.5) Memory fragmentation not being cleaned Enable active defragmentation: redis-cli config set activedefrag yes
Redis consuming more memory than maxmemory Overhead not counted in limit Set maxmemory to 75% of available RAM, account for OS overhead
Frequent key evictions maxmemory set too low Increase maxmemory or optimize data structures
Slow Redis responses Active defragmentation using too much CPU Lower active-defrag-cycle-max: redis-cli config set active-defrag-cycle-max 10
Out of memory errors No eviction policy configured Set appropriate policy: redis-cli config set maxmemory-policy allkeys-lru
Connection refused errors maxclients limit reached Increase client limit: redis-cli config set maxclients 20000

Next steps

Prefere não gerir isto sozinho?

Gerimos a infraestrutura de empresas que dependem do tempo de atividade. Totalmente gerida, com um contacto fixo que conhece o seu ambiente.

Tem um contacto fixo que conhece o seu ambiente

Roterdão 03:10 · acessível por mensagem, sem formulário de tickets