Solution to the "Background saving terminated by signal 9" error in Redis.

It is quite common for Redis service to report this error when there is a failure in the available memory allocation and is unable to save data. If when you start the Redis service you get an alert like the following
WARNING overcommit_memory is set to 0! Background save may fail under low memory condition
You are most likely having errors due to insufficient available memory. When there is a memory error, it will generally be written in the Redis log
Background saving terminated by signal 9
To verify this, you can use the Redis CLI and observe some memory values.
user@server$ redis-cli info memory | grep _human
used_memory_human:69.72M
used_memory_rss_human:120.87M
used_memory_peak_human:154.82M
total_system_memory_human:23.32G
used_memory_lua_human:44.00K
used_memory_scripts_human:200B
maxmemory_human:0B
Let's make it a little clearer by highlighting the most important points:
Memory assigned by Redis
used_memory_human: 69.72M
Memory reported by the OS
used_memory_rss_human: 120.87M
Highest peak of consumed memory
used_memory_peak_human: 154.82M
On the other hand, it seems that the memory for Redis is quite large (total_system_memory_human), however, this corresponds to the fact that this data comes from an instance of Redis on Linux, so the total memory will be the host memory. Redis will be subject to the maximum memory allocated in the container.
Docker
If you use Docker, you can check if these values are very close to the available threshold for the container with the following command:
user@server$ docker stats
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM %
3cf88e6f3fdb test-redis 0.25% 126.2MiB / 128MiB 98.58%
When the memory is almost at 99%, it is almost impossible to ignore a memory allocation error.
Inside the instance, you can try to obtain the maximum available memory.
user@server$ cat /sys/fs/cgroup/memory.max
134217728
Since 134217728 to Mib = 128MB, that will be our current memory limit.
In this case, more memory must be assigned to the container in resource limits as shown below.
redis:
container_name: test-redis
deploy:
resources:
limits:
memory: 128M