Redis is an in-memory data store, making memory management critical for system performance. Continuously accumulating data in memory can lead to memory shortage issues. In this post, we will explore key configurations and methods for memory management and performance optimization in Redis.
1. Importance of Memory Management in Redis
Redis processes data quickly by storing all data in memory. When storing large amounts of data or continuously performing write operations, performance degradation may occur due to memory shortages. To prevent these issues, Redis provides maxmemory
and maxmemory-policy
settings.

2. Setting Memory Limit: maxmemory
To limit maximum memory usage, set maxmemory
. This setting allows you to explicitly specify the amount of memory Redis can utilize. If no maxmemory
setting is specified, it may use memory up to the system's available range.
maxmemory 256mb # Example: set to 256MB
When the specified memory limit is exceeded, Redis can automatically remove old data based on the data eviction policy.
3. Setting Data Eviction Policy: maxmemory-policy
The policy that determines which data to delete when the memory limit is exceeded is maxmemory-policy
. Redis offers various eviction policies, with the main options being:
- noeviction: Rejects new data storage to minimize data loss
- allkeys-lru: Deletes the least recently used key from all keys, suitable for caching
- volatile-lru: Deletes the least recently used key among those with an expiration time set
- allkeys-random: Randomly deletes data from all keys
- volatile-ttl: Deletes the key with the least remaining time to live
For example, to set the LRU policy to delete old data among all keys, configure it as follows:
maxmemory-policy allkeys-lru
4. Memory Optimization Setting: maxmemory-samples
The maxmemory-samples
option specifies the number of samples to use when applying the memory policy for data eviction, with a default value of 5
. Increasing the sampling number allows for a more accurate policy but can consume more CPU resources.
maxmemory-samples 5
5. Setting TTL for Data Compression and Deletion
Redis allows setting a TTL (time to live) for each key, which automatically deletes the key when it expires, freeing up memory. This is useful for session management or cache data.
EXPIRE <key> <seconds> # Example: expire session_data in 1 hour
Data with a TTL is automatically deleted over time, making it effective for memory savings.
6. Memory Monitoring and Performance Check Commands
Redis provides commands to monitor memory status and check performance.
- INFO memory: Check overall memory usage
- MEMORY USAGE <key>: Check memory usage for a specific key
- MEMORY STATS: Provides detailed memory statistics
- SLOWLOG: Check long-running commands to assess performance
redis-cli INFO memory
redis-cli MEMORY USAGE session_data
redis-cli SLOWLOG GET
7. Practical Example: Memory Optimization for Caching Data and Task Queues with Redis
For cache data, you can use the allkeys-lru
policy in Redis to automatically remove old data. However, in cases where data preservation is critical, such as with task queues, you can set the noeviction
policy and enable the AOF (Append-Only File) feature for data recovery.
maxmemory 256mb
maxmemory-policy allkeys-lru # Suitable for cache data
appendonly yes
appendfsync everysec
maxmemory-policy noeviction # Suitable for task queues
Conclusion
We have examined methods for memory management and performance optimization in Redis. Since Redis is an in-memory database, memory management settings are crucial, allowing you to balance performance and data preservation. In the next part, we will discuss how to run Redis containers using Docker, exploring efficient data preservation and performance maintenance methods for Redis as an independent container.
Add a New Comment