What is an "allocator" in the context of Redis?
An allocator in the context of Redis is a low-level memory manager responsible for allocating, reallocating, and freeing chunks of RAM whenever Redis creates, modifies, or deletes data.
In other words, it's the component that manages how Redis obtains and uses RAM.
1. Why Redis needs its own allocator
Redis is an in-memory database, and it spends almost all of its time working with memory.
That's why the standard system malloc/free (from the C standard library) turned out to be too slow and inefficient.
To reduce fragmentation and speed up small allocations, Redis uses specialized external allocators.
2. Which allocators Redis can use
Redis can be compiled with different memory-allocator implementations:
| Allocator | Description | Used by default |
|---|---|---|
| jemalloc | fast, efficient, low-fragmentation, supports memory profiling | Yes (the default) |
| tcmalloc | from Google, optimized for multi-threading | Sometimes, if set explicitly |
| malloc (glibc) | the standard system one, less efficient for Redis | No |
The allocator can be chosen when building Redis:
make MALLOC=jemalloc3. How the allocator works inside Redis
When Redis creates a new key, value, or internal structure:
- It asks the allocator to reserve a block of memory of the needed size.
- The allocator returns a pointer to the allocated region on the heap.
- Redis writes the data there (a string, a structure, a number, etc.).
- When the data is deleted, Redis calls
free(), and the allocator returns the memory to a pool for reuse.
The allocator keeps internal records of every allocated block and can redistribute them to reduce fragmentation.
4. Why Redis chose jemalloc
jemalloc ("Jason Evans malloc") was designed specifically for systems with a high rate of small allocations. It:
- spreads memory across arenas, independent regions, which reduces fragmentation;
- groups blocks by size classes, speeding up reuse;
- has built-in statistics (
jemalloc stats), which Redis exposes viaINFO MEMORY; - runs stably under heavy load and doesn't "bloat" over time, unlike standard malloc.
5. How Redis interacts with the allocator
Redis doesn't manage memory directly, it delegates allocation to the allocator. But Redis:
- tracks the total amount of memory used (
used_memory); - can trigger cleanup (
MEMORY PURGE); - can inspect the allocator's state (
MEMORY STATS,MEMORY DOCTOR).
6. The fragmentation problem
Even with jemalloc, Redis can run into fragmentation, when free memory is scattered in pieces and can't be reused.
Redis tracks a fragmentation ratio (mem_fragmentation_ratio), computed as:
mem_fragmentation_ratio = used_memory_rss / used_memoryIf the value exceeds 1.5, fragmentation is high, and Redis can trigger defragmentation.
7. Summary
In the context of Redis, an allocator is the component that:
- allocates and frees chunks of RAM for keys, values, and data structures;
- minimizes fragmentation and overhead;
- is implemented through the jemalloc library (by default);
- directly affects Redis's performance and stability.
The allocator is Redis's "invisible engine", managing every byte of memory inside the process.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.