Memory Management
Redis is an in-memory database. How it handles RAM is the difference between peak performance and a system crash. Explore allocation, overhead, and eviction.
LRU (Least Recently Used)
Favors retaining keys that were accessed recently. Good for general caching where old data is less likely to be needed.
LFU (Least Frequently Used)
Favors keys with high access counts. Better for scenarios where popularity remains stable over time.
Redis doesn't allocate memory directly from the OS for every key. It uses a general-purpose allocator like jemalloc or libc malloc.
Jemalloc is the default on Linux because it reduces memory fragmentation, which is the biggest enemy of long-running Redis instances.
Fragmentation Ratio
The ratio between memory requested from the OS and memory actually used by Redis. A ratio > 1.5 usually indicates serious fragmentation.
| Policy | Behavior |
|---|---|
| noeviction | Returns errors when memory is full. |
| allkeys-lru | Evicts least recently used keys. |
| volatile-lru | Only evicts keys with an expire set. |
| allkeys-random | Evicts random keys to make room. |
Even a tiny 1-byte string can take up to 40+ bytes in RAM due to the object header and allocator alignment.