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
maxmemorytoo low can cause frequent evictions and degrade performance. - Not setting an eviction policy with
maxmemory-policycan cause Redis to return errors when memory is full. - Using incorrect units or forgetting to restart Redis after changing
redis.confcan lead to the setting not applying.
plaintext
Wrong way: maxmemory 100 Right way: maxmemory 100mb
Quick Reference
| Setting | Description | Example |
|---|---|---|
| maxmemory | Maximum memory Redis can use | maxmemory 256mb |
| maxmemory-policy | Eviction policy when max memory is reached | maxmemory-policy allkeys-lru |
| CONFIG SET maxmemory | Set max memory dynamically | CONFIG SET maxmemory 100mb |
| CONFIG GET maxmemory | Get current max memory setting | CONFIG 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.