tarek salem
tarek salem

Reputation: 701

google cloud pub sub multiple subscriber for the same message with the same subscription

I am working on implementing event driven using GCP Pub/Sub.

I have a topic called orders, and that topic will have a subscription named orderPlacedSubscription I have two services that want to listen to all messages for this subscription and perform different actions, so I have paymentService and notificationService, the paymentService will listen to each message filtered by orderPlacedSubscription and process the payment as well as the notification service will listen to the same message and send a notification.

My question

Upvotes: 8

Views: 14150

Answers (3)

iGanja
iGanja

Reputation: 2464

I thought I would try to answer the OP a little more directly and clearly:

Q1) "does pub-sub support having two subscribers related to one subscription and both receive messages and acknowledge them separately?" This is also the main question in the title.

A1) In short NO. To clarify however, the use of the term "subscriber"s in the question is not correct, or at best confusing. In GCP Pub/Sub, each TOPIC can have N "subscribers" accomplished through "Subscriptions". Each Subscription can be processed by a scalable number of clients, BUT once a message is "ack"ed or "nack"ed, by any client, it is considered read and processed for that Subscription. No other client of that Subscription will receive that message.

Q2) "In case if each subscriber can acknowledge the message separately without affecting the other subscriber, does google cloud pub-sub support retry for different subscribers in case of failure from one subscriber?"

A2) In short YES, but this answer may be interpreted incorrectly because of how the word "subscriber" is incorrectly used in the first question. Each GCP Pub/Sub Topic Subscription gets its own "queue" of all messages to the Topic. i.e. Every message for the Topic is sent to every Subscription (subscription filtering aside), and the clients for those Subscriptions must ack or nack them. There are retry settings for every Subscription, and "dead letter queues" DLQ's can be set for each Subscription to handle eventual failures.

Upvotes: 1

fedemart
fedemart

Reputation: 61

Pub/Sub will send the same message to all subscriptions for a given topic, for each subscription it will send the message to one subscriber, the message gets deleted once at least one subscriber acknowledges the message.

This means you can't have multiple subscribers subscribe to the same subscription and have them all process the same message.

Link to documentation

Upvotes: 6

Sakshi Gatyan
Sakshi Gatyan

Reputation: 2116

Yes, a subscription can have multiple subscriber clients.

In a subscription workflow, if a message is not acknowledged by a subscriber, Pub/Sub will attempt to redeliver the outstanding message. In the process of redelivering the outstanding message, Pub/Sub holds back and tries not to deliver the outstanding message to any other subscriber on the same subscription. Once the outstanding message is acknowledged it can be delivered to other subscribers.

You can refer subscription workflow and this documentation for more information.

Upvotes: 10

Related Questions