Nitish N Banakar
Nitish N Banakar

Reputation: 149

Acknowledgement Behaviour of GCP Pub/sub messages

I'm working on micro-service that contains subscriptions to a topic in GCP Pub/Sub. As multiple instances of a Microservices run on more than one host (multiple clusters on cloud), I wanted to know acknowledging behaviour of messages from subscriptions. When a subscription on one instance receives, process and acknowledges the message, does the same subscription on other hosts receive the message?

I expect that once the subscriber acknowledges, pub/sub doesn't further send the message, but what if two subscribers on same subscription on different hosts receives message at the same time, does it cause duplication?

Upvotes: 1

Views: 2083

Answers (1)

Andrei Tătar
Andrei Tătar

Reputation: 8295

Pub/Sub delivers each published message at least once for every subscription.

https://cloud.google.com/pubsub/docs/subscriber#at-least-once-delivery

If you want multiple "workers" to not receive message clones, you need to use a single subscription for all of them.

This is because for events you can have multiple systems listening on the same topic, on different subscriptions so that all the systems receive the event that something has happened.

For commands, you usually want a single system to handle them (even if split between multiple workers) so you would need a single subscription that is shared among all the workers.

By the way, your system should be idempotent in processing events/commands from a topic. The general rule of thumb is that each message is guaranteed to be received by a subscriber at least one time. Meaning the same system could potentially receive the same command two times.

Upvotes: 1

Related Questions