8

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.

Richard Knop
  • 81,041
  • 149
  • 392
  • 552

3 Answers3

5

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.

David Budworth
  • 11,248
  • 1
  • 36
  • 45
  • 1
    Thanks. This is what I ended up using, works as expected. I only needed this as an extra option for a broker for people who don't/can't use RabbitMQ with my library but AMQP is the standard. – Richard Knop Aug 17 '15 at 13:18
3

Redis streams (now, with Redis 5.0) support acknowledgment of tasks as they are completed by a group.

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

David Parks
  • 30,789
  • 47
  • 185
  • 328
2

No, Redis' PubSub does not guarantee delivery nor does it limit the number of possible subscribers who'll get the message.

Itamar Haber
  • 47,336
  • 7
  • 91
  • 117