Redis读写分离功能验证

基础信息

组件版本

V5.0.6

网络信息

服务器信息

角色 IP 操作系统 CPU 内存 磁盘空间 顺序读 顺序写
redis1 10.241.90.2 CentOS Linux release 7.6.1810 4 8 300G 238MB/s 137MB/s
redis2 10.241.90.22 CentOS Linux release 7.6.1810 4 8 300G 142MB/s 126MB/s
redis3 10.241.40.2 CentOS Linux release 7.6.1810 4 8 300G 267MB/s 136MB/s
压测写机器 10.241.40.4 CentOS Linux release 7.6.1810 4 8 36G 261MB/s 193MB/s
压测读机器 10.241.90.34 CentOS Linux release 7.6.1810 4 4 99G 272MB/s 155MB/s

读写测试:

  • 顺序写:
    1. time dd if=/dev/zero of=/data/ddtest bs=1k count=10000000 conv=fdatasync
  • 顺序写:
    1. time dd if=/data/ddtest of=/dev/null bs=1k count=10000000 status=progress

功能场景验证

背景

读写分离功能验证

关键参数

  1. replica-read-only yes

验证

查看redis主从关系

  • 主:10.241.90.22:7100——>从:10.241.40.2:7200
  • 主:10.241.90.2:7100——>从:10.241.90.22:7200
  • 主:10.241.40.2:7100——>从:10.241.90.2:7200
  1. 10.241.40.2:7100> cluster nodes
  2. 223b5fbba94261c798822ca5b6021ad849cf430c 10.241.40.2:7200@17200 slave 87d6b0cf0c8be6422cf810083a3e8bc9117588da 0 1637635856000 6 connected
  3. 87d6b0cf0c8be6422cf810083a3e8bc9117588da 10.241.90.22:7100@17100 master - 0 1637635857000 3 connected 5461-10922
  4. e68a5e5faffb57c16dc5afc0a6224cd829d9d52f 10.241.90.22:7200@17200 slave e29dac0a295dd2c195bf7d90b6065191377ef8b0 0 1637635859187 4 connected
  5. e29dac0a295dd2c195bf7d90b6065191377ef8b0 10.241.90.2:7100@17100 master - 0 1637635859000 1 connected 0-5460
  6. aee6dc2ef81bc2d166252fd8ad7b6a96bd482cd1 10.241.90.2:7200@17200 slave 75ad64eda7d46a68308a4397485fcb5659d8ab39 0 1637635859000 5 connected
  7. 75ad64eda7d46a68308a4397485fcb5659d8ab39 10.241.40.2:7100@17100 myself,master - 0 1637635858000 5 connected 10923-16383

登录从节点10.241.40.2:7200,通过keys *命令发现该从节点有一个key为zhuting

通过get命令读取zhuting的值,发现无法获取,会提示去对应的主节点10.241.90.22:7100获取

手动通过readonly命令修改从节点的只读属性,再次通过get命令就能够获取到zhuting的值了

  1. [BONREE bonree@stresskafka03:/data/br/base/redis]$ redis-cli -h 10.241.40.2 -p 7200 -a Bonree@365
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. 10.241.40.2:7200> keys *
  4. 1) "zhuting"
  5. 10.241.40.2:7200> get zhuting
  6. (error) MOVED 8894 10.241.90.22:7100
  7. 10.241.40.2:7200> readonly
  8. OK
  9. 10.241.40.2:7200> get zhuting
  10. "123"
  11. 10.241.40.2:7200> exit

通过readonly设置只读属性之后,退出redis,再次登录redis,需要重新设置readonly属性。

  1. [BONREE bonree@stresskafka03:/data/br/base/redis]$ redis-cli -h 10.241.40.2 -p 7200 -a Bonree@365
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. 10.241.40.2:7200> get zhuting
  4. (error) MOVED 8894 10.241.90.22:7100
  5. 10.241.40.2:7200>
  6. 10.241.40.2:7200> readonly
  7. OK
  8. 10.241.40.2:7200> get zhuting
  9. "123"

readwrite取消readonly命令的设置,恢复slave节点默认状态

  1. [BONREE bonree@stresskafka03:/data/br/base/redis]$ redis-cli -h 10.241.40.2 -p 7200 -a Bonree@365
  2. Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
  3. 10.241.40.2:7200> readonly
  4. OK
  5. 10.241.40.2:7200> get zhuting
  6. "123"
  7. 10.241.40.2:7200>
  8. 10.241.40.2:7200> readwrite
  9. OK
  10. 10.241.40.2:7200> get zhuting
  11. (error) MOVED 8894 10.241.90.22:7100

结论

1、redis集群模式下,必须通过readonly命令来给slave节点只读的权限,且每次断开redis连接,就必须重新通过readonly修改权限

2、因为slave节点从master节点同步数据需要时间,slave节点和master节点会存在一定的数据不一致,所以使用读写分离需要业务可以容忍一定程度的数据不一致

3、读写分离在主从+哨兵模式下比较实用,当主节点读取性能不够时,通过增加从节点分摊读取数据的性能。cluster模式下不再适用读写分离,当集群性能不够时,直接扩展master节点。