生产环境Redis中的热点key如何发现并优化? 与上期叶问一样,Redis的热点key问题同样值得我们关注,Redis的热点key出现主要有以下几种情况 1、用户消费的数据远大于生产的数据(热卖商品、热点新闻、热点评论、明星直播) 2、请求分片集中,超过单Server的性能极限。 热点key可能造成如下问题: 1、流量集中,达到物理网卡上限 2、请求过多,缓存分片服务被打垮 3、DB击穿,引起业务雪崩 如何发现热点key: 1、通过tcpdump抓包,可以分析抓取到网络包分析key的频率 2、redis客户端抓取,例如请求key的时候记录日志,分析日志得到key的访问频率 3、redis的monitor可以记录redis的所有操作,记录并分析monitor日志得到key的访问频率(注意:monitor可能会造成性能问题,慎重使用) 4、Redis 4.0提供了—hot-keys配合maxmemory-policy可以统计热点key 5、第三方开源项目,如facebook开源项目redis-faina,原理同3 解决方案如下: 1、对于”get”类型的热点key,通常可以为redis添加slave,通过slave承担读压力来缓解 2、服务端本地缓存,服务端先请求本地缓存,缓解redis压力 3、多级缓存方案,通过多级请求,层层过滤解决热点key问题 4、proxy方案,有些方案会探测分片热点key,缓存在proxy上缓解redis压力 5、同解决big方案类似,将一个key通过hash分解为多个key,value值一样,将这些key分散到集群多个分片中,需要访问时先根据hash算出对应的key,然后访问的具体的分片