Redis maxmemory Explained: How to Limit Memory Usage and Prevent OOM Errors
Introduction
The maxmemory directive controls the maximum amount of memory that Redis can use. It is one of the most important configuration options because Redis primarily stores data in memory.
Without a memory limit, Redis may continue allocating memory until the operating system runs out of available RAM, potentially causing performance problems or triggering the Linux Out Of Memory (OOM) killer.
This guide explains how maxmemory works, how to configure it, and best practices for production environments.
Test Environment
Default Value
Many Redis installations have no memory limit configured by default.
maxmemory 0
A value of 0 means Redis does not enforce a maximum memory limit.
Syntax
maxmemory
Examples:
maxmemory 256mb
maxmemory 1gb
maxmemory 4gb
Redis supports several memory units:
Why Is maxmemory Important?
Redis stores active data in RAM.
If memory usage continues to grow without limits, several problems may occur:
The operating system may run out of memory.
Other applications may become unstable.
Linux may terminate Redis using the OOM killer.
Server performance may degrade significantly.
Setting an appropriate memory limit helps maintain predictable performance.
How Redis Behaves
Suppose a server has:
RAM: 8 GB
Redis configuration:
maxmemory 6gb
Redis can use approximately 6 GB for storing data.
Once memory reaches the configured limit, Redis follows the configured eviction policy (maxmemory-policy) to decide how to handle new writes.
How to Configure maxmemory
Open the Redis configuration file.
sudo nano /etc/redis.conf
Locate or add:
maxmemory 2gb
Save the file.
Restart Redis.
sudo systemctl restart redis
Verify the Configuration
Run:
redis-cli CONFIG GET maxmemory
Example output:
1) "maxmemory"
2) "2147483648"
The value is displayed in bytes.
Change the Setting Without Restarting
Redis allows runtime configuration.
redis-cli CONFIG SET maxmemory 1gb
Verify:
redis-cli CONFIG GET maxmemory
Note that runtime changes may not persist after restarting unless the configuration file is also updated.
Choosing the Right Value
There is no universal setting.
Consider:
Total server memory.
Operating system requirements.
Other running applications.
Expected dataset size.
Future growth.
Example recommendations:
Always leave sufficient memory for the operating system and other services.
Common Mistakes
Setting maxmemory Too High
Redis may leave insufficient memory for the operating system, increasing the risk of swapping or OOM termination.
Forgetting the Eviction Policy
maxmemory alone does not determine what happens when memory is exhausted.
The maxmemory-policy directive controls whether Redis evicts keys or rejects write operations.
Ignoring Memory Fragmentation
Actual memory consumption may exceed the configured dataset size because of allocator overhead and fragmentation.
Monitor memory usage regularly.
Best Practices
Always configure maxmemory for production servers.
Leave free RAM for the operating system.
Combine maxmemory with an appropriate maxmemory-policy.
Monitor memory usage using INFO memory.
Test memory limits before deploying to production.
Avoid using swap for Redis whenever possible.
Related Configuration
Redis maxmemory-policy Explained
Conclusion
The maxmemory directive defines the maximum amount of memory Redis can use and is essential for maintaining server stability. A carefully chosen memory limit helps prevent out-of-memory conditions while ensuring Redis delivers consistent performance.
For most production deployments, maxmemory should always be configured together with a suitable eviction policy and regular memory monitoring to achieve the best balance between performance and reliability.
Explore More
› Redis Key Management Explained: Managing Keys, Expiration, and Memory Usage
› Redis maxmemory-policy Explained: Choosing the Right Eviction Policy
› Redis Explained: In-Memory Data Store, Persistence, and Best Practices
› Redis INFO Memory Explained: Understanding Redis Memory Statistics
› Why Do People in Southeast Asia Love Iced Drinks? The Climate, Culture, and Science Explained
› Northern vs Southern Chinese Business Culture: Key Differences Explained