Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to save in background (redis-server)

Tags:

redis

I have two redis servers running on the same machine. The second one's log files have several instances with notices such as these:

[50818] 19 Feb 06:41:05.007 * 10 changes in 300 seconds. Saving...
[50818] 19 Feb 06:41:05.007 # Can't save in background: fork: Cannot allocate memory

In contrast, the log files of the first one solely contain successful DB saves. If I were out of memory, I reckon both would have similar logs. It perplexes me that only one has this problem, the other doesn't. Any leads?

Moreover, research led me to this blog post, which contends that the issue can be ameliorated if I do sysctl vm.overcommit_memory=1 on the command line. There's no explanation of how this helps. Can someone explain what's going on here in context of redis?

like image 504
Hassan Baig Avatar asked Feb 28 '17 07:02

Hassan Baig


People also ask

What is Bgsave in Redis?

Redis BGSAVE command is used to save the DB in the background. The OK code is immediately returned. Redis forks, the parent continues to serve the clients, the child saves the DB on disk then exits. Syntax: Basic syntax of redis BGSAVE command is shown below: BGSAVE.

How do I turn off Redis?

SHUTDOWN [NOSAVE] [SAVE] Redis SHUTDOWN command is used to stop all clients, perform a blocking SAVE if at least one save point is configured, flush all append only files if AOF is enabled and quit the server.


1 Answers

As Per Redis FAQs :

Background saving is failing with a fork() error under Linux even if I've a lot of free RAM!

Short answer: echo 1 > /proc/sys/vm/overcommit_memory :)

And now the long one:

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.

A good source to understand how Linux Virtual Memory work and other alternatives for overcommit_memory and overcommit_ratio is this classic from Red Hat Magazine, "Understanding Virtual Memory". Beware, this article had 1 and 2 configuration values for overcommit_memory reversed: refer to the proc(5) man page for the right meaning of the available values.

like image 166
LuFFy Avatar answered Oct 18 '22 01:10

LuFFy