0
0
RedisHow-ToBeginner · 3 min read

How to Set Max Memory in Redis: Simple Guide

To set the maximum memory Redis can use, configure the maxmemory directive in the Redis configuration file or use the CONFIG SET maxmemory <bytes> command. This limits Redis memory usage and helps manage data eviction when the limit is reached.
📐

Syntax

The maxmemory setting defines the maximum amount of RAM Redis will use. It can be set in bytes, kilobytes (KB), megabytes (MB), or gigabytes (GB).

Example units: maxmemory 100mb means Redis will use up to 100 megabytes of memory.

You can set it in the redis.conf file or dynamically with the CONFIG SET command.

plaintext
maxmemory <bytes|kb|mb|gb>
💻

Example

This example shows how to set max memory to 100 megabytes dynamically using the Redis CLI and verify the setting.

redis
127.0.0.1:6379> CONFIG SET maxmemory 100mb
OK
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "104857600"
Output
OK 1) "maxmemory" 2) "104857600"
⚠️

Common Pitfalls

  • Setting maxmemory too low can cause frequent evictions and degrade performance.
  • Not setting an eviction policy with maxmemory-policy can cause Redis to return errors when memory is full.
  • Using incorrect units or forgetting to restart Redis after changing redis.conf can lead to the setting not applying.
plaintext
Wrong way:
maxmemory 100

Right way:
maxmemory 100mb
📊

Quick Reference

SettingDescriptionExample
maxmemoryMaximum memory Redis can usemaxmemory 256mb
maxmemory-policyEviction policy when max memory is reachedmaxmemory-policy allkeys-lru
CONFIG SET maxmemorySet max memory dynamicallyCONFIG SET maxmemory 100mb
CONFIG GET maxmemoryGet current max memory settingCONFIG GET maxmemory

Key Takeaways

Set max memory using the maxmemory directive to limit Redis RAM usage.
Use CONFIG SET maxmemory <value> to change max memory without restarting Redis.
Always configure an eviction policy with maxmemory-policy to handle memory limits gracefully.
Specify units (kb, mb, gb) clearly to avoid misconfiguration.
Restart Redis after changing redis.conf for the settings to take effect.