Skip to content

Instantly share code, notes, and snippets.

@aamsur-mkt
Last active June 25, 2020 03:55
Show Gist options
  • Save aamsur-mkt/2614023a2ad3321d742e1d15578248e6 to your computer and use it in GitHub Desktop.
Save aamsur-mkt/2614023a2ad3321d742e1d15578248e6 to your computer and use it in GitHub Desktop.
REDIS - MISCONF Redis is configured to save RDB snapshots

Problem

  1. [RedisException] MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
  2. On /var/log/redis/redis-server.log show # Can't save in background: fork: Cannot allocate memory

Explanation

This error occurs because of BGSAVE being failed. During BGSAVE, Redis forks a child process to save the data on disk. Although exact reason for failure of BGSAVE can be checked from logs (usually at /var/log/redis/redis-server.log on linux machines) but a lot of the times BGAVE fails because the fork can't allocate memory. Many times the fork fails to allocate memory (although the machine has enough RAM available) because of a conflicting optimization by the OS.

As can be read from Redis FAQ (https://redis.io/topics/faq):

Redis background saving schema relies on the copy-on-write semantic of fork in modern operating systems: Redis forks (creates a child process) that is an exact copy of the parent. The child process dumps the DB on disk and finally exits. In theory the child should use as much memory as the parent being a copy, but actually thanks to the copy-on-write semantic implemented by most modern operating systems the parent and child process will share the common memory pages. A page will be duplicated only when it changes in the child or in the parent. Since in theory all the pages may change while the child process is saving, Linux can't tell in advance how much memory the child will take, so if the overcommit_memory setting is set to zero fork will fail unless there is as much free RAM as required to really duplicate all the parent memory pages, with the result that if you have a Redis dataset of 3 GB and just 2 GB of free memory it will fail.

Setting overcommit_memory to 1 says Linux to relax and perform the fork in a more optimistic allocation fashion, and this is indeed what you want for Redis. Redis doesn't need as much memory as the OS thinks it does to write to disk, so may pre-emptively fail the fork.

Solving

$ echo 'vm.overcommit_memory = 1' >> /etc/sysctl.conf
$ sysctl vm.overcommit_memory=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment