Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to Trigger a Retry in PubSub When Using Push Subscriptions

I have a google cloud function that is subscribed to a pubsub push-type subscription. Because of the setup, the function does not use http requests to receive or respond to the subscription.

What I am trying to do is trigger a retry for a message for when there is an error in my function (e.g. api limit is hit), so that I can utilize the retry functionality of pubsub to use exponential backoff to keep trying.

I have implement try catch blocks in my function and throwing errors accordingly to try to trigger a retry, but none of the messages are ever retried.

Am I missunderstanding how PubSub works with these retries? Do I need to handle retries in the function's code instead? I am somewhat new to GCP and software development, so I would appreciate any advice.

0 1 695
1 REPLY 1

Hi @johndobson,

Welcome to Google Cloud Community!

If you're using Cloud Functions v2 API, note that the retries for Pub/Sub topics are disabled for these triggers which may have caused the issue. This can be re-enabled using the Cloud Functions v2 API. You can re-enable this using gcloud or by through Google Cloud console. You should enable this if you want every Pub/Sub push to be processed before continuing.

In addition, make sure that your try catch block catches the correct error. If not, the message may be acknowledged and the retry will not occur. Regarding retries in Cloud Function code, Pub/Sub handles the retries for you automatically when your Cloud Function throws an error, given that your subscription's retry policy has been correctly set.

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.