Richard Knop
Richard Knop

Reputation: 83697

Redis Pub/Sub Ack/Nack

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.

Upvotes: 8

Views: 11371

Answers (3)

David Parks
David Parks

Reputation: 32071

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

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

Upvotes: 3

David Budworth
David Budworth

Reputation: 11626

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.

Upvotes: 5

Itamar Haber
Itamar Haber

Reputation: 49942

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

Upvotes: 2

Related Questions