redis cluster下的keys,scan,pipeline使用

环境:

redis部署架构:cluster

redis客户端:lettuce

操作api:RedisTemplate

keys命令:

RedisTemplate可以执行,jedis和lettuce都是通过获取所有节点,然executeCommandOnAllNodes;组装所有节点返回结果。

scan命令:

jedis:Scan is not supported across multiple nodes within a cluster

lettuce:clusterScan,在多个节点scan后,拼装结果

批量操作:

redis 集群模式由于根据key进行hash分片,所有multi-key operations(多key的批量操作命令可能受限)。

Commands performing complex multi-key operations like set unions and intersections are implemented for cases where all of the keys involved in the operation hash to the same slot.

Redis Cluster implements a concept called hash tags ( {user1000}.followers )that can be used to force certain keys to be stored in the same hash slot. However, during manual resharding, multi-key operations may become unavailable for some time while single-key operations are always available.

Redis Cluster does not support multiple databases like the standalone version of Redis. We only support database 0; the SELECT command is not allowed.

pipeline 操作:

JedisClusterConnection:Pipeline is currently not supported for JedisClusterConnection.
LettuceConnection:支持单机,集群。 对于 executePipelined(RedisCallback),如果使用正确的话,会使用 asyncDedicatedConn 私有连接执行。那么怎么算使用正确呢?
需要使用回调的连接connection对象进行 Redis 调用,不能直接使用 redisTemplate 调用,否则 pipeline 不生效。

使用 PIPELINE 之后,我们可以看出,其实它的原理,就是客户端先将所有命令拼接在一起然后本地缓存起来,之后统一发到服务端,服务端执行所有命令之后,统一响应。

Lettuce 的连接有一个 AutoFlushCommands 配置,就是指在这个连接上执行的命令,如果发送到服务端。默认是 false,即收到一个命令就发到服务端一个。如果配置为 false,则将所有命令缓存起来,手动调用 flushCommands 的时候,将缓存的命令一起发到服务端,这样其实就是实现了 Pipeline。


在Redis集群模式下使用pipeline进行批量操作——实现原理

Redis集群是没法执行一些批量操作命令的,如mget,pipeline等。这是因为redis将集群划分为16384个slot,不同的key会划分到不同的slot,集群中的每个节点对应一批slot。jedis客户端并没有封装批量操作的命令。

但是在Redis集群中,每个redis节点都能单独对外提供服务。同时Jedis客户端提供了计算key的slot方法,已经slot和节点之间的映射关系,通过这两个数据,就可以计算出每个key所在的节点,然后对相同节点上的数据使用pipeline获取数据。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值