redis-003内存紧张bgsave失败

阅读量: zyh 2017-12-30 14:08:11
Categories: > Tags:

前言

之前安装完redis,启动redis,总会告诉你让你默认修改一些参数,不知其原因,但每次都照办。
后来遇到一些redis内存使用紧张的时候,从而导致 redis-bgsave 失败,再一次想到了这个问题。
经过查阅资料,发现官方文档里告诉了原因,在此记录一下。如有不对之处,还请指出。

问题

redis-bgsave与overcommit_memory的关系。
当剩余物理内存低于当前redis所用内存的时候,overcommit_memory=1的意义

官方解释在此

https://redis.io/topics/faq

Background saving fails with a fork() error under Linux even if I have 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 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.overcommit_memory

Setting to 1 tells Linux to relax and perform the fork in a more optimistic allocation fashion, and this is indeed what you want for Redis.overcommit_memory

A good source to understand how Linux Virtual Memory works and other alternatives for and is this classic from Red Hat Magazine, “Understanding Virtual Memory”. You can also refer to the proc(5) man page for explanations of the available values.overcommit_memory``overcommit_ratio

翻译后的大致意思

官方的FAQ,给人一种这么个意思。 如果你不设置overcommit_memory=1,那么COW机制将无法使用,所以当空余内存小于当前redis占用内存时,redis-bgsave 因为无法申请到足够的内存,将导致分配内存失败。
而COW机制的意思就是:父子进程公用内存页,因此只会copy变化的数据。因此,在COW机制下,redis-bgsave只需要申请到支撑变化数据的内存即可。