英文:
Redis Pub/Sub Ack/Nack
问题
Redis Pub/Sub中有确认(acknowledgements)的概念吗?
例如,当使用RabbitMQ时,我可以在不同的机器上运行两个工作进程,当我向队列发布消息时,只有一个工作进程会确认/拒绝并处理该消息。
然而,我发现在Redis Pub/Sub中,两个工作进程都会处理该消息。
考虑以下简单示例,我在两个不同的机器/客户端上运行以下Go例程:
go func() {
for {
switch n := pubSubClient.Receive().(type) {
case redis.Message:
process(n.Data)
case redis.Subscription:
if n.Count == 0 {
return
}
case error:
log.Print(n)
}
}
}()
当我发布一条消息时:
conn.Do("PUBLISH", "tasks", "task A")
两个Go例程都会接收并运行process函数。
是否有一种实现类似RabbitMQ行为的方法?例如,只有第一个确认消息的工作进程才能接收并处理它。
英文:
Is there a concept of acknowledgements in Redis Pub/Sub?
For example, when using RabbitMQ, I can have two workers running on separate machines and when I publish a message to the queue, only one of the workers will ack/nack it and process the message.
However I have discovered with Redis Pub/Sub, both workers will process the message.
Consider this simple example, I have this go routine running on two different machines/clients:
go func() {
for {
switch n := pubSubClient.Receive().(type) {
case redis.Message:
process(n.Data)
case redis.Subscription:
if n.Count == 0 {
return
}
case error:
log.Print(n)
}
}
}()
When I publish a message:
conn.Do("PUBLISH", "tasks", "task A")
Both go routines will receive it and run the process function.
Is there a way of achieving similar behaviour to RabbitMQ? E.g. first worker to ack the message will be the only one to receive it and process it.
答案1
得分: 5
Redis PubSub更像是一种广播机制。
如果你想要队列,你可以使用BLPOP和RPUSH来实现相同的交互。请注意,RabbitMQ在Redis中并没有真正存在的其他功能。但是,如果你只是想要简单的作业调度/请求处理方式,这个方法完全可以胜任。
英文:
Redis PubSub is more like a broadcast mechanism.
if you want queues, you can use BLPOP along with RPUSH to get the same interraction. Keep in mind, RabbitMQ does all sorts of other stuff that are not really there in Redis. But if you looking for simple job scheduling / request handling style, this will work just fine.
答案2
得分: 3
Redis流(现在,使用Redis 5.0)支持通过组完成任务时的确认。
https://redis.io/topics/streams-intro
英文:
Redis streams (now, with Redis 5.0) support acknowledgment of tasks as they are completed by a group.
答案3
得分: 2
不,Redis的PubSub不保证消息的传递,也不限制可能接收消息的订阅者数量。
英文:
No, Redis' PubSub does not guarantee delivery nor does it limit the number of possible subscribers who'll get the message.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论