Redis Stream之consumer故障恢复
Redis Stream之consumer故障恢复
通过基础的命令我们可以做到:
- 让多个consumer参与一个group,每次获取一部分信息进行消费
- 消费失败可以查询pending message再次消费出问题的消息
然而现实中,有可能某个consumer始终不能ack他的信息。redis提供了一个方式来处理这种情况
- 将那个始终不能ack自己message的consumer的消息,分给其他人
实现很简单
- 专门的一个consumer去监听pending message
- 使用特殊的命令把这些消息分给其他consumer
xpending
# 查看pending message
127.0.0.1:6379[1]> XREADGROUP group g1 myt streams test 0
1) 1) "test"
2) 1) 1) "1578127264192-0"
2) 1) "msg"
2) "world"
2) 1) "1578127730231-0"
2) 1) "msg"
2) "welcome"
# 查看组内的pending message信息
127.0.0.1:6379[1]> XPENDING test g1
1) (integer) 2
2) "1578127264192-0"
3) "1578127730231-0"
4) 1) 1) "myt"
2) "2"
xpending的完整命令格式为:
xpendign key groupName [start end count] [consumerName]
xpending是一个只读的命令,会输出指定group中所有处于pending message中的消息总个数、开始ID、结束ID、每个consumer中pending message中消息的个数。可以通过指定开始结束id和consumerName来获取更加详细的信息:
127.0.0.1:6379[1]> XPENDING test g1 - + 10
1) 1) "1578127264192-0"
2) "myt"
3) (integer) 131820
4) (integer) 8
2) 1) "1578127730231-0"
2) "myt"
3) (integer) 131820
4) (integer) 7
127.0.0.1:6379[1]> XPENDING test g1 - + 10 myt
1) 1) "1578127264192-0"
2) "myt"
3) (integer) 136594
4) (integer) 8
2) 1) "1578127730231-0"
2) "myt"
3) (integer) 136594
4) (integer) 7
详细的内容为:
- 信息ID
- consumerName
- idle time,单位是milliseconds,意思是最后一次被读取到当前的时间
- 被读取的次数,就是这个消息一共被consumer拿走了几次
xclaim
改变message的所属关系,命令格式为:
xclaim key groupName comsumer minIdleTime id [id ...] [IDLE ms] [TIME msUnixTime] [RETRYCOUNT count] [force] [justid]
27.0.0.1:6379[1]> XPENDING test g1 - + 10
1) 1) "1578127264192-0"
2) "myt"
3) (integer) 1524399
4) (integer) 9
2) 1) "1578127730231-0"
2) "myt"
3) (integer) 1524399
4) (integer) 8
3) 1) "1578134897648-0"
2) "cgj"
3) (integer) 53452
4) (integer) 1
# 改变所属关系
127.0.0.1:6379[1]> XCLAIM test g1 cgj 1524300 1578127264192-0
1) 1) "1578127264192-0"
2) 1) "msg"
2) "world"
127.0.0.1:6379[1]> XPENDING test g1 - + 10
1) 1) "1578127264192-0"
2) "cgj"
3) (integer) 4981
4) (integer) 10
2) 1) "1578127730231-0"
2) "myt"
3) (integer) 1609335
4) (integer) 8
3) 1) "1578134897648-0"
2) "cgj"
3) (integer) 138388
4) (integer) 1
使用xclaim将consumer myt的id为1578127264192-0的信息给了cgj,再次使用xpending查看发现message的所属关系已经发生了变化
TIPS
- xclaim之后的消息,idelTime会重置
- xclaim之后的消息,deliverTime增加,因为改变所属关系相当于再次被consumer读取了
- 使用xclaim时尽量指定minIdleTime,考虑两个consumer同时执行xclaim,可以避免同一个消息发给两个consumer