Navigation Menu

Skip to content
This repository has been archived by the owner on Feb 20, 2021. It is now read-only.

Memory Configuration

Enrico Giordani edited this page Dec 16, 2015 · 5 revisions

Premise

The following documentation applies to Redis on Windows 2.8.x, for 3.0.x please follow this link.

The UNIX version of Redis relies on the system call fork() to perform point-in-time snapshots of the heap. In addition to the AOF and RDB backup mechanism, the master-slave synchronization and clustering features are also dependent on fork(). Windows doesn't have a fork() API, so in order to simulate this behavior the Redis heap is placed in a memory mapped file that can be shared between the main Redis process and a child process.

Memory Configuration Settings

  • maxheap

    The maxheap value represents the underlying memory and memory mapped file storage for the Redis heap, which includes what reported by Redis as memory usage + fragmentation + internal data structures. maxheap is a hard limit. When it’s reached, Redis will terminate with an out-of-memory exception.

  • maxmemory

    You only need to set maxmemory if you want Redis to limit the size of the data, either by evicting old keys or preventing additional writes when the limit is reached. Otherwise, it’s not required that you set maxmemory.

    WARNING: not setting maxmemory will cause Redis to terminate with an out-of-memory exception if the maxheap limit is reached.

MaxMemory and MaxHeap constrains

The Redis heap must be larger than the value specified by the maxmemory setting, as the heap allocator has its own memory requirements and fragmentation of the heap is inevitable. If only the maxmemory setting is specified, maxheap will be set at 1.5 * maxmemory. If the maxheap setting is specified along with maxmemory, the maxheap setting will be automatically increased if it is smaller than 1.5 * maxmemory.

Default Configuration

The maxheap setting controls the maximum size of the memory mapped file, as well as the total usable space for the Redis heap. Running Redis without either maxheap or maxmemory will result in a memory mapped file being created that is equal to the size of physical memory.

There must be enough disk space available for the page file in order for Redis to launch.

The default configuration places the memory mapped file in the local appdata directory. If you wish to move this file to another directory or to another local disk, use the heapdir setting.

System Paging File Size Requirements

During fork() operations additional commit charge is also put on the system paging file, corresponding to the number of memory pages that get modified (for example, by clients modifying the data set) while the fork() operation is in progress. In the worst case scenario, the total page file commit will max out at around:

(size of physical memory) + (2 * size of maxheap)

For instance, on a machine with 8GB of physical RAM, the max page file commit, with the default maxheap size, will be:

(8GB) + (2 * 8GB) = 24GB

The default page file sizing of Windows will allow for this without having to reconfigure the system. Larger heap sizes are possible, but the maximum page file size will have to be increased accordingly.

If the maxmemory setting is specified but maxheap isn't, maxheap will be set at 1.5 * maxmemory. On a machine with 16GB of physical RAM, the max page file commit, with a maxmemory set to 8GB, will be:

(16GB) + (2 * (1.5 * 8GB) = 40GB

Preventing Redis from running out-of-memory

Explicitly setting maxmemory will prevent Redis from using more memory than the specified amount of bytes. When the memory limit is reached Redis will try to remove keys according to the eviction policy selected by the maxmemory-policy setting.

If Redis can't remove keys according to the policy, or if the policy is set to 'noeviction', Redis will start to reply with errors to commands that would use more memory, like SET, LPUSH, and so on, and will continue to reply to read-only commands like GET.

This option is usually useful when using Redis as an LRU cache, or to set a hard memory limit for an instance (using the 'noeviction' policy).

WARNING: If you have slaves attached to an instance with maxmemory on, the size of the output buffers needed to feed the slaves are subtracted from the used memory count, so that network problems / resyncs will not trigger a loop where keys are evicted, and in turn the output buffer of slaves is full with DELs of keys evicted triggering the deletion of more keys, and so forth until the database is completely emptied.

In short... if you have slaves attached it is suggested that you set a lower limit for maxmemory so that there is some free RAM on the system for slave output buffers (but this is not needed if the policy is 'noeviction').

Disabling Persistence

If Redis is to be used without any kind of asynchronous persistence, then the fork() mechanism used by the background AOF/RDB persistence is unneccessary. As an optimization, all persistence can be turned off in the Windows version of Redis. This will disable the creation of the memory mapped heap file, redirect heap allocations to the system heap allocator, and disable commands that would otherwise cause fork() operations: BGSAVEand BGREWRITEAOF. This setting may not be combined with any of the other settings that configure AOF and RDB operations:

persistence-available [(yes)|no]

Note

Since Redis uses a memory mapped file to allocate the heap memory, the Working Set memory usage showed by the Windows Task Manager or by other tools such as ProcessExplorer will not always be accurate. For example, right after a background save of the RDB or the AOF files, the working set value may drop significantly. In order to check the correct amount of memory used by the redis-server to store the data, use the INFO client command. The INFO command shows only the memory used to store the redis data, not the extra memory used by the Windows process for its own requirements. That extra amount of memory not reported by the INFO command can be calculated subtracting the Peak Working Set reported by the Windows Task Manager and the used_memory_peak reported by the INFO command.