Redis Pub/Sub Ack/Nack(确认/否认)

huangapple go评论70阅读模式
英文:

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更像是一种广播机制。

如果你想要队列,你可以使用BLPOPRPUSH来实现相同的交互。请注意,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.

https://redis.io/topics/streams-intro

答案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.

huangapple
  • 本文由 发表于 2015年8月17日 01:11:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/32037803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定