Skip to main content

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:

AllocatorDescriptionUsed by default
jemallocfast, efficient, low-fragmentation, supports memory profilingYes (the default)
tcmallocfrom Google, optimized for multi-threadingSometimes, if set explicitly
malloc (glibc)the standard system one, less efficient for RedisNo

The allocator can be chosen when building Redis:

bash
make MALLOC=jemalloc

3. How the allocator works inside Redis

When Redis creates a new key, value, or internal structure:

  1. It asks the allocator to reserve a block of memory of the needed size.
  2. The allocator returns a pointer to the allocated region on the heap.
  3. Redis writes the data there (a string, a structure, a number, etc.).
  4. 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 via INFO 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:

javascript
mem_fragmentation_ratio = used_memory_rss / used_memory

If 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:

  1. allocates and frees chunks of RAM for keys, values, and data structures;
  2. minimizes fragmentation and overhead;
  3. is implemented through the jemalloc library (by default);
  4. 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 ready
Premium

A concise answer to help you respond confidently on this topic during an interview.