剑指Offer(Redis)——Redis集群管理

如何从海量数据中快速找到需要的数据是在开发中关注的问题。。

分片:按照规则去划分数据,分散在多个节点上可以通过使用分片,降低单节点的压力。

Redis Cluster采用五中心结构,节点之间使用Gossip协议发送消息,或者发现新的节点,既然Redis目的是将key分布式的放在不同的节点上,这是如何实现的呢???

一般会获取到key的哈希值,根据节点数求模值,缺点是无法在一个节点上找到连续的key,因此需要介绍一下连续性的哈希算法。

一致性哈希算法:对2^32取模,使用哈希值模拟一个虚拟的圆环,将哈希值空间组织成一个虚拟的圆环。

将数据key使用相同的函数Hash计算出哈希值,确定此数据在环上的位置,从此位置沿环顺时针”行走“,遇到的第一台服务器就是其应该定位到的服务器。

假如有ObjectA、ObjectB、ObjectC、ObjectD四个数据对象, 经过哈希值计算后,环空间上的位置如下:
在这里插入图片描述
一致性算法,数据A被定位到NodeA上,B被定义到NodeB上,C被定义到NodeC上,D被定义到NodeD上。接下来分析一下一致性哈希算法的容错性和可扩展性。
假设NodeC宕机,看到A、B、C对象不会受到影响,只有C被重新定位到NodeD上。
在这里插入图片描述
在一致性算法中,如果一台服务器不可用,受到影响的数据仅仅是此服务器到其环空间中上一台服务器(即沿着逆时针方向行走遇到的第一台服务器)之间的数据,其他的对象不受影响。

在系统中增加一台服务器NodeX,结果如下图所示:
在这里插入图片描述
对象A、B、C不会受到任何影响,对象C重新定位到NodeX上。在一致性哈希算法中如果增加一台机器,受到影响的数据仅仅是新服务器到其环空间中上一台服务器(沿逆时针方向行走遇到的第一台服务器)之间的数据,其他数据不会受到任何影响。

综上,一致性哈希算法对于节点的增减都只需要重定位环空间中的一小部分数据具有较好的容错性和可扩展性。

但是一致性哈希算法也是存在一定缺点的,会带来数据倾斜。

这是随机的算法,所以可能会造成节点分布不均匀可能缓存会集中在一台服务器上,还会造成不好的结果,于是一致性哈希算法设置了虚拟节点,即对每一个服务节点计算多个哈希,每个计算结果位置都放置一个此服务节点称为虚拟节点。具体做法可以在服务器IP或主机名后面增加编号来实现。
在这里插入图片描述
同样还可以引入Redis主从分布,哨兵模式,保证Redis集群的高可用性。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
redis-cluster-tool 是一个非常便利的 Redis 集群管理工具。help        Usage: redis-cluster-tool [-?hVds] [-v verbosity level] [-o output file]                  [-c conf file] [-a addr] [-i interval]                  [-p pid file] [-C command] [-r redis role]                  [-t thread number] [-b buffer size]    Options:      -h, --help             : this help      -V, --version          : show version and exit      -d, --daemonize        : run as a daemon      -s, --simple           : show the output not in detail      -v, --verbosity=N      : set logging level (default: 5, min: 0, max: 11)      -o, --output=S         : set logging file (default: stderr)      -c, --conf-file=S      : set configuration file (default: conf/rct.yml)      -a, --addr=S           : set redis cluster address (default: 127.0.0.1:6379)      -i, --interval=N       : set interval in msec (default: 1000 msec)      -p, --pid-file=S       : set pid file (default: off)      -C, --command=S        : set command to execute (default: cluster_state)      -r, --role=S           : set the role of the nodes that command to execute on (default: all, you can input: all, master or slave)      -t, --thread=N         : set how many threads to run the job(default: 8)      -b, --buffer=S         : set buffer size to run the job (default: 1048576 byte, unit:G/M/K)        Commands:        cluster_state                 :Show the cluster state.        cluster_used_memory           :Show the cluster used memory.        cluster_keys_num              :Show the cluster holds keys num.        slots_state                   :Show the slots state.        node_slot_num                 :Show the node hold slots number.        new_nodes_name                :Show the new nodes name that not covered slots.        cluster_rebalance             :Show the cluster how to rebalance.        flushall                      :Flush all the cluster.        cluster_config_get            :Get config from every node in the cluster and check consistency.        cluster_config_set            :Set config to every node in the cluster.        cluster_config_rewrite        :Rewrite every node config to echo node for the cluster.        node_list                     :List the nodes            del_keys                      :Delete keys in the cluster. The keys must match a given glob-style pattern.(This command not block the redis)ExampleGet the cluster state:        $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_state -r master    master[127.0.0.1:34504] cluster_state is ok     master[127.0.0.1:34501] cluster_state is ok     master[127.0.0.1:34502] cluster_state is ok     master[127.0.0.1:34503] cluster_state is ok     all nodes cluster_state is ok    Get the cluster used memory:    $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_used_memory -r master    master[127.0.0.1:34504] used 195 M 25%    master[127.0.0.1:34501] used 195 M 25%    master[127.0.0.1:34502] used 195 M 25%    master[127.0.0.1:34503] used 195 M 25%    cluster used 780 MRebalance the cluster slots:    $redis-cluster-tool -a 127.0.0.1:34501 -C cluster_rebalance    --from e1a4ba9922555bfc961f987213e3d4e6659c9316 --to 785862477453bc6b91765ffba0b5bc803052d70a --slots 2048    --from 437c719f50dc9d0745032f3b280ce7ecc40792ac --to cb8299390ce53cefb2352db34976dd768708bf64 --slots 2048    --from a497fc619d4f6c93bd4afb85f3f8a148a3f35adb --to a0cf6c1f12d295cd80f5811afab713cdc858ea30 --slots 2048    --from 0bdef694d08cb3daab9aac518d3ad6f8035ec896 --to 471eaf98ff43ba9a0aadd9579f5af1609239c5b7 --slots 2048Then you can use "redis-trib.rb reshard --yes --from e1a4ba9922555bfc961f987213e3d4e6659c9316 --to 785862477453bc6b91765ffba0b5bc803052d70a --slots 2048 127.0.0.1:34501" to rebalance the cluster slots     Flushall the cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C flushall -s    Do you really want to execute the "flushall"?    please input "yes" or "no" :        yes    OKGet a config from every node in cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "cluster_config_get maxmemory" -r master    master[127.0.0.1:34501] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34502] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34503] config maxmemory is 1048576000 (1000MB)    master[127.0.0.1:34504] config maxmemory is 1048576000 (1000MB)    All nodes config are Consistent    cluster total maxmemory: 4194304000 (4000MB)Set a config from every node in cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "cluster_config_set maxmemory 10000000" -s    Do you really want to execute the "cluster_config_set"?    please input "yes" or "no" :    yes        OKDelete keys in the cluster:    $redis-cluster-tool -a 127.0.0.1:34501 -C "del_keys abc*"    Do you really want to execute the "del_keys"?    please input "yes" or "no" :    yes    delete keys job is running...    delete keys job finished, deleted: 999999 keys, used: 4 s 标签:redis

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值