Saturday 24 September 2016

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

Redis periodically saves its data in memory onto the disk for backup in the form of rdb file. This is helpful in recovering the data if redis restarts or for backup purposes.
Sometimes while saving the data, redis may give the error: "Can't save in background : fork: Cannot allocate memory".



This error can come if redis has enough memory for the data, but not for saving the rdb file.
For this, you need to check a system param named "sysctl vm.overcommit_memory". Its value should be set to 1 instead of 0 by the command "sysctl vm.overcommit_memory=1" as shown below.



After setting this value, the background save can be successfully started, and it does not give an error.
The Below are the logs after the value was set to 1 and after starting background save.



Note that this error only comes when all three conditions hold

  • redis memory is close to or more than half of allocated memory/system memory(memory is 32+GB and 27 GB is left).
  • vm.overcommit_memory is set to 0
  • when redis does a background save.



Also, to make sure that this setting persists even when the redis machine restarts, it needs to be set in "/etc/sysctl.conf" file.

More details here

2 comments:

  1. Thanks Buddy
    i read your blog then tried as per redis documentation like below
    echo 1 > /proc/sys/vm/overcommit_memory
    so i got permission denied then i tried with sudo too and result was same.
    but your command sysctl vm.overcommit_memory=1 with sudo worked
    thanks a lot.

    ReplyDelete