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 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 write-back sub-synchronous and asynchronous two ways:
synchronous write back (SAVE), the main process back directly to disk to write data in the case of large volumes of data will cause the system suspended animation for a long time.
Asynchronous write-back (BGSAVE), the main process after the fork, replicate themselves and write disk back through this new process, a new process after the write-back itself off
Since BGSAVE do not need to master the process to block, the system will not play dead, usually using BGSAVE to achieve data write-back.
redis when dump data starts fork child process, because the memory is not enough, can not lead to a persistent fall dish
redis have a default option:
stop-writes-on-bgsave-error yes
By default, this situation, if the problem persisted in the RDB snapshots process, set the parameters, Redis user is not allowed to make any updates.
The solution is not complete, and this option to false
stop-writes-on-bgsave-error false
But this is only a snapshot of the hard disk when redis write error, allowing users to continue to do the update operation, but still failed hard disk write
Complete solution: directly modify kernel parameters vm.overcommit_memory = 1
Edit the file /etc/sysctl.conf added:
vm.overcommit_memory = 1
execute sysctl -p to take effect
Linux kernel will decide whether to release to set parameters based on parameters vm.overcommit_memory.
= vm.overcommit_memory . 1 , the direct release vm.overcommit_memory = 0 : The request is more virtual memory size and allocation of the current system of free physical memory plus swap, decide whether to release. vm.overcommit_memory = 2 : Comparison of all processes will be allocated virtual memory plus virtual memory and system requests the allocation of the current free physical memory plus swap, decide whether to release.