5

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

  • does pub-sub support having two subscribers related to one subscription and both receive messages and acknowledge them separately?
  • 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?
Daniel Collins
  • 752
  • 3
  • 14
tarek salem
  • 540
  • 1
  • 6
  • 20

1 Answers1

8

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.

Sakshi Gatyan
  • 1,903
  • 7
  • 13
  • thank you for the clarification, I have another question is there a possible way to process one message at a time for each client for example I have a payment topic and have three instances of payment service and these instances are connected to pub-sub, what I want is each message go to one instance without going to another one because if more than one instance received the same message it will duplicate payment which is invalid behavior, I know that this could be resolved using idempotency, but what I mean is to make pub-sub works as a queue, send the message to only one listener – tarek salem Jul 30 '22 at 11:20
  • Hi @tareksalem, According to Stackoverflow best practices, I would advise you to open a new thread for any new additional questions. It increases the question visibility and the chances of finding answers. – Sakshi Gatyan Aug 15 '22 at 10:20
  • 1
    The answer is not true. Please, check the PubSub architecture in the section "The Basics of a Publish/Subscribe services". Your scenario is a subscription with two subscribers. Here messages will be processed by one subscriber only. https://cloud.google.com/pubsub/architecture There are cases were both subscribers will process the message. This is because of their Pull-behaviour in which both subscribers have a buffer containing the same message. https://stackoverflow.com/questions/57983777/google-pubsub-python-multiple-subscriber-clients-receiving-duplicate-messages – Matthias Jul 10 '23 at 15:57
  • 1
    @Matthias thanks for pointing out the official docs, I needed to confirm this too: messages are load balanced among subscribers on the same suscription. – Codigo Morsa Jul 26 '23 at 09:54