Get the App
SLTechnology News&Howtos  ›  Database  › 

Redis: Can't save in background: fork: Cannot allocate memory

Shulou Source: shulou.com Published: 2022-06-01 06:55:00 10月03日 Update

Redis: Can't save in background: fork: Cannot allocate memory

JAVA program error message:

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

View the redis log:

18793:S 02 Dec 10:02:02.069 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:08.088 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:14.006 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:20.021 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:26.038 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:32.054 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:38.067 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:44.086 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:50.002 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:02:56.017 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:03:02.037 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:03:08.056 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:03:14.073 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:03:20.091 # Can't save in background: fork: Cannot allocate memory

18793:S 02 Dec 10:03:26.007 # Can't save in background: fork: Cannot allocate memory

Data writeback is divided into synchronous and asynchronous methods:

Synchronous writeback (SAVE), where the main process writes back data directly to the disk. In the case of a large amount of data, it will cause the system to die for a long time.

Asynchronous writeback (BGSAVE). After the main process fork, copy itself and write back to the disk through this new process. After the writeback ends, the new process shuts down itself.

Because BGSAVE does not need main process blocking and the system will not fake death, BGSAVE is generally used to realize data write-back.

Redis starts the token child process when dump data. Due to insufficient memory, the disk cannot be persisted.

Redis has a default option:

Stop-writes-on-bgsave-error yes

By default, if there is a problem during RDB snapshots persistence, Redis does not allow users to do any updates after setting this parameter.

The incomplete solution is to change this option to false

Stop-writes-on-bgsave-error false

But this only allows the user to continue to update when redis writes a snapshot of the hard disk, but it still fails to write to the hard disk.

Complete solution: directly modify the kernel parameter vm.overcommit_memory = 1

Edit file / etc/sysctl.conf add:

Vm.overcommit_memory=1

Execute sysctl-p to make it effective

The Linux kernel decides whether to release or not based on the setting of the parameter vm.overcommit_memory parameter.

Vm.overcommit_memory = 1, direct release

Vm.overcommit_memory = 2: all the virtual memory allocated by the process plus the virtual memory allocated by this request is compared with the system's current free physical memory plus swap to decide whether to release it.

Tags: Process memory data parameters system allocation kernel situation mode physics user hard disk disk idle synchronization update insufficient information just size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux MySQL Microsoft Docker