设为首页 收藏本站
查看: 934|回复: 0

[经验分享] redis 启动警告及处理

[复制链接]

尚未签到

发表于 2018-11-3 12:09:54 | 显示全部楼层 |阅读模式
  起因: 生产环境的一台Redis机器
Can't save in background: fork: Cannot allocate memory  导致redis服务停止,但是当时机器的内存是64G,redis使用到的内存只有40多G
  我们都知道,redis 如果开启了持久化,RDB模式的bgsave 以及 AOF模式下,重写appendonly.aof 都会导致redis fork 出一个子进程。但是难道操作系统的进程fork难道不应该是copy-on-write 的吗?
  这件事让我重新关注起redis启动时的日志来。
  首先来看看redis启动时所报的日志
1610:M 12 Sep 07:46:20.524 # Server started, Redis version 3.0.11610:M 12 Sep 07:46:20.524 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1’ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1’ for this to take effect.1610:M 12 Sep 07:46:20.524 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled’ as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.1610:M 12 Sep 07:46:20.525 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.1610:M 12 Sep 07:46:20.525 * The server is now ready to accept connections on port 6379  
1610:M 12 Sep 07:57:21.819 * Background saving started by pid 1615
  
1615:C 12 Sep 07:57:21.827 * DB saved on disk
  
1615:C 12 Sep 07:57:21.827 * RDB: 4 MB of memory used by copy-on-write
  
1610:M 12 Sep 07:57:21.925 * Background saving terminated with success
  可以看到警告有3个
1. overcommit_memory  
2. THP
  
3. TCP backlog
  overcommit_memory
Defines the conditions that determine whether a large memory request is accepted or denied. There are three possible values for this parameter:  
0 — The default setting. The kernel performs heuristic memory overcommit handling by estimating the amount of memory available and failing requests that are blatantly invalid. Unfortunately, since memory is allocated using a heuristic rather than a precise algorithm, this setting can sometimes allow available memory on the system to be overloaded.
  
1 — The kernel performs no memory overcommit handling. Under this setting, the potential for memory overload is increased, but so is performance for memory-intensive tasks.
  
2 — The kernel denies requests for memory equal to or larger than the sum of total available swap and the percentage of physical RAM specified in overcommit_ratio. This setting is best if you want a lesser risk of memory overcommitment.
  当操作系统收到内存分配请求时,它会依据overcommit_memory 设定的条件,考虑接受或者拒绝这个请求
  0 – 默认设置 内核使用启发式算法,来估算可用的内存量,直接拒绝不合理的请求
  1 – 内核不考虑内存是否够用,直接同意请求,在这种设置下,潜在的内存过载风险增加了,但有利于内存密集型任务
  2 – 如果程序请求的内存分配大于等于 交换分区和物理内存的总和 * overcommit_ratio / 100 则拒绝这个请求
  默认是 交换分区和物理内存总和的50%
  默认设置是0,只要内存请求超过物理内存的剩余量,请求就会被拒绝。设置1,不管实际物理内存使用量,直接同意请求。设置1是一种比较粗放式的对内存请求的管理方式,我认为更为优雅的方式是使用2,并且将overcommit_ratio 的值设为60 ~ 80
echo "vm.overcommit_memory=2" >> /etc/sysctl.conf  
echo "vm.overcommit_ratio=70" >> /etc/sysctl.conf
  
sysctl -p
  Transparent Huge Pages
  
  操作系统默认的内存页大小是4kB,可以如果使用更大的内存页比如2MB,就可以使用同样多的页表项,管理更大的内存空间,但是对于redis这样的内存数据库,它会导致内存分配的速度变慢,并且导致内存的实际使用率下降,因此redis推荐我们关闭此项
echo never > /sys/kernel/mm/transparent_hugepage/enabled  TCP backlog
The behavior of the backlog argument on TCP sockets changed with Linux 2.2. Now it specifies the queue length for completely established sockets waiting to be accepted, instead of the number of incomplete connection requests. The maximum length of the queue for incomplete sockets can be set using /proc/sys/net/ipv4/tcp_max_syn_backlog. When syncookies are enabled there is no logical maximum length and this setting is ignored. See tcp(7) for more information.  
If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently truncated to that value; the default value in this file is 128. In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128.
  对于linux 而言backlog 是指已经完成了3次握手,且等待 accept 的连接
  如果被没有被accept, 连接会一直在队列中排队,队列的最大长度为 backlog
  可以想见, 在client 非常多且创建和关闭连接非常频繁的场景下,这个参数会非常有用。
echo "net.core.somaxconn = 511" >> /etc/sysctl.conf  
sysctl -p



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-630224-1-1.html 上篇帖子: Redis集群搭建与简单使用 下篇帖子: Redis性能分析工具redis-faina
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表