在现代应用程序中,缓存被广泛应用以提高性能和减轻后端数据库的压力。本文将探讨面对 Redis 集群缓存分配不均问题时的解决方法。
我们的 Redis 集群部署包括 3 主 3 从,每个节点分配内存 4G(服务器总内存32G),内存淘汰策略相同:volatile-lru。
volatile-lru
在性能测试过程中,通过 pinpoint 监控发现报错:OOM
Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException: OOM command not allowed when used memory> 'maxmemory'
清空缓存后,再次执行性能测试一段时间,使用 info memory 查看各节点内存使用情况,发现 2、6 节点内存快速上升,其它节点内存使用率很低。
info memory
例如,2 节点内存达到 3.81G,6 节点内存达到 3.8G,而其他 4 个节点内存仅为 80M 左右。
[root@iZ2ze3e0bvnd1hf5xbkxn3Z redis01]# ./redis-cli -h 192.168.0.213 -p 6692 -c -a Tiye@54L2!Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.213:6692> info memory# Memoryused_memory:4095914816used_memory_human:3.81Gused_memory_rss:4473937920used_memory_rss_human:4.17Gused_memory_peak:4105896552used_memory_peak_human:3.82Gused_memory_peak_perc:99.76%used_memory_overhead:5159996used_memory_startup:1483832used_memory_dataset:4090754820used_memory_dataset_perc:99.91%allocator_allocated:4095978056allocator_active:4728315904allocator_resident:4765335552total_system_memory:33019609088total_system_memory_human:30.75Gused_memory_lua:30720used_memory_lua_human:30.00Kused_memory_scripts:0used_memory_scripts_human:0Bnumber_of_cached_scripts:0maxmemory:4096000000maxmemory_human:3.81Gmaxmemory_policy:volatile-lruallocator_frag_ratio:1.15allocator_frag_bytes:632337848allocator_rss_ratio:1.01allocator_rss_bytes:37019648rss_overhead_ratio:0.94rss_overhead_bytes:-291397632mem_fragmentation_ratio:1.09mem_fragmentation_bytes:378064128mem_not_counted_for_evict:0mem_replication_backlog:1048576mem_clients_slaves:20512mem_clients_normal:2603516mem_aof_buffer:0mem_allocator:jemalloc-5.1.0active_defrag_running:0lazyfree_pending_objects:0lazyfreed_objects:0
[root@iZ2ze3e0bvnd1hf5xbkxn3Z redis02]# ./redis-cli -h 192.168.0.213 -p 6691 -c -a Tiye@54L2!Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.213:6691> info memory# Memoryused_memory:84125536used_memory_human:80.23Mused_memory_rss:107753472used_memory_rss_human:102.76Mused_memory_peak:109050608used_memory_peak_human:104.00Mused_memory_peak_perc:77.14%used_memory_overhead:9292232used_memory_startup:1483832used_memory_dataset:74833304used_memory_dataset_perc:90.55%allocator_allocated:84208120allocator_active:102572032allocator_resident:108343296total_system_memory:33019609088total_system_memory_human:30.75Gused_memory_lua:30720used_memory_lua_human:30.00Kused_memory_scripts:0used_memory_scripts_human:0Bnumber_of_cached_scripts:0maxmemory:4096000000maxmemory_human:3.81Gmaxmemory_policy:volatile-lruallocator_frag_ratio:1.22allocator_frag_bytes:18363912allocator_rss_ratio:1.06allocator_rss_bytes:5771264rss_overhead_ratio:0.99rss_overhead_bytes:-589824mem_fragmentation_ratio:1.28mem_fragmentation_bytes:23669520mem_not_counted_for_evict:0mem_replication_backlog:1048576mem_clients_slaves:20512mem_clients_normal:2603360mem_aof_buffer:0mem_allocator:jemalloc-5.1.0active_defrag_running:0lazyfree_pending_objects:0lazyfreed_objects:0
cluster nodes
[root@iZ2ze3e0bvnd1hf5xbkxn3Z redis06]# ./redis-cli -h 192.168.0.213 -p 6696 -c -a Tiye@54L2!Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.0.213:6696> cluster nodese73a5ec3e26ed23e9b4bf56811527c8820a7bd79 192.168.0.213:6696@16696 myself,slave e2a678a004bc99e76180a16a6a41e2cad1c96052 0 1691992895000 2 connected25317f0f8f7b2eebdbdc0914c659ab96ed3dab18 192.168.0.213:6693@16693 master - 0 1691992897074 3 connected 10923-1638327aba75f54cccbb42125edb20f2f9d7c2f777d6c 192.168.0.213:6695@16695 slave 5e08015f75cdb05b1c7ed78dead1d85cdb0e838f 0 1691992895070 1 connected5e08015f75cdb05b1c7ed78dead1d85cdb0e838f 192.168.0.213:6691@16691 master - 0 1691992894068 1 connected 0-5460053916b96426f790244d984cad3f69f9151e4ece 192.168.0.213:6694@16694 slave 25317f0f8f7b2eebdbdc0914c659ab96ed3dab18 0 1691992896072 3 connectede2a678a004bc99e76180a16a6a41e2cad1c96052 192.168.0.213:6692@16692 master - 0 1691992894000 2 connected 5461-10922
根据节点信息中的master字段判断该节点是主节点还是从节点。如果节点的master字段为空,则表示它是主节点;如果master字段不为空,则表示它是从节点,并且该字段的值是对应的主节点ID。e2a678a004bc99e76180a16a6a41e2cad1c96052 节点是主节点,e73a5ec3e26ed23e9b4bf56811527c8820a7bd79 节点是从节点,并且它的主节点是 e2a678a004bc99e76180a16a6a41e2cad1c96052
综上判断:redis集群6个节点,其中2节点的从节点是6节点。
在 Redis 集群中的任意一个节点上执行 cluster slots 命令,分析哈希槽数量分配是否均匀
cluster slots
192.168.0.213:6692> cluster slots1) 1) (integer) 02) (integer) 54603) 1) "192.168.0.213"2) (integer) 66913) "5e08015f75cdb05b1c7ed78dead1d85cdb0e838f"4) 1) "192.168.0.213"2) (integer) 66953) "27aba75f54cccbb42125edb20f2f9d7c2f777d6c"2) 1) (integer) 54612) (integer) 109223) 1) "192.168.0.213"2) (integer) 66923) "e2a678a004bc99e76180a16a6a41e2cad1c96052"4) 1) "192.168.0.213"2) (integer) 66963) "e73a5ec3e26ed23e9b4bf56811527c8820a7bd79"3) 1) (integer) 109232) (integer) 163833) 1) "192.168.0.213"2) (integer) 66933) "25317f0f8f7b2eebdbdc0914c659ab96ed3dab18"4) 1) "192.168.0.213"2) (integer) 66943) "053916b96426f790244d984cad3f69f9151e4ece"
根据提供的哈希槽信息,我们可以计算出以下三个哈希槽的数量:
综上判断:这三个哈希槽的数量分别是5461、5462和5461。即,哈希槽数量分配均匀。
跟产品开发人员沟通,没有做predixy适配,但可以尝试配置。最后发现产品启动服务报错,放弃该方案。尝试过程记录如下:
wget https://github.com/joyieldInc/predixy/releases/download/1.0.5/predixy-1.0.5-bin-amd64-linux.tar.gz
tar zxvf predixy-1.0.5-bin-amd64-linux.tar.gzcd predixy-1.0.5
① 编辑conf/predixy.conf文件
Include cluster.conf #Include cluster.conf解开注释# Include sentinel.conf# Include try.conf #注释掉Include try.conf
## Worker threadsWorkerThreads 3 #从1改为3,表示开启的进程数
② 编辑conf/cluster.conf文件
lusterServerPool { MasterReadPriority 0 #设置为0代表开启读写分离 Password Tiye@54L2! #redis集群密码 StaticSlaveReadPriority 50 DynamicSlaveReadPriority 50 RefreshInterval 1 ServerTimeout 1 ServerFailureLimit 10 ServerRetryTimeout 1 KeepAlive 120 Servers { + 192.168.0.213:6691 #redis集群节点 + 192.168.0.213:6692 + 192.168.0.213:6693 + 192.168.0.213:6694 + 192.168.0.213:6695 + 192.168.0.213:6696 }}
配置文件解析文档:https://github.com/joyieldInc/predixy/blob/master/doc/config_CN.md
① 创建启动和停止脚本,放到predixy-1.0.5目录下
mkdir -p /opt/predixy-1.0.5/logs/cd /opt/predixy-1.0.5/bin
② 创建启动脚本up.sh
#!/bin/bashpath=`pwd`nohup $path/bin/predixy conf/predixy.conf > $path/logs/predixy.log 2>&1 &
③ 查看日志
tail -f logs/predixy.log
④ 创建停止脚本down.sh
#!/bin/bashpath=`pwd`pid=`ps -ef | grep $path/bin/predixy | grep -v grep | awk '{print $2}'`kill -9 $pid
./redis-cli -p 7617127.0.0.1:7617> mset b1 b2OK127.0.0.1:7617> get b1"b2"
将应用配置文件中的集群地址修改为predixy地址,应用服务启动报错:
...Autowired annotation is not supported on static fields...
尝试缩减为 4 个节点,以提升单个节点的内存配置。但创建集群时提示至少需要 3 个主节点,因此该方案也不可行。
*** ERROR: Invalid configuration for cluster creation.*** Redis Cluster requires at least 3 master nodes.*** This is not possible with 4 nodes and 1 replicas per node.*** At least 6 nodes are required
最终解决方案是修改集群节点内存:1、3、4、5 节点分配内存 3G,2、6 节点分配内存 8G。
面对 Redis 集群缓存分配不均的问题,我们可以通过逐步的优化方法来解决。首先,了解节点信息、主从关系和缓存占用情况,然后分析哈希槽分布情况,尝试不同的优化方案,最后通过调整内存分配来解决问题。
本文来自博客园,作者:查拉图斯特拉面条,转载请注明原文链接:https://www.cnblogs.com/n00dle/p/17631331.html
原文链接:https://www.cnblogs.com/n00dle/p/17631331.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728