Restrict no of retry attempts to 3 times in google cloud functions

As mentioned in the subject, I want to restrict the no of max retry attempts to 3 times on using google cloud functions. The documentation states that there is retry windows of 24 hrs with exponential backoff. But instead of the retry window, need a mechanism based on the max retry attempts. Kindly suggest how can i go about achieving this use case..

1 Like

Hello @aaron1789 ,Welcome on Google Cloud Community.

Unfortunately, you can’t directly set a hard limit of 3 retries within the Cloud Functions configuration. However, I see few workarounds for your query:

  1. You can implement Retry Counter in your function
  • Inside your Cloud Function: Add a counter variable to track the number of retries.
  • On Each Retry: Increment the counter.
  • Check the Counter: Before processing the event, check if the counter has reached 3. If it has, stop processing and handle the failure gracefully.
  • Handle gracefully time between retries - Once you’ve reach 3 retries, implement mechanism to wait specific amount of time ( if needed ) and reset retries counter.
  1. Implement approach called " Dead Letter Topic". More info here: https://docs.dapr.io/developing-applications/building-blocks/pubsub/pubsub-deadletter/
  • Configure a Dead Letter Topic: When setting up your Cloud Function, specify a Dead Letter Topic (DLT) in the Pub/Sub subscription.
  • DLT for Failed Events: After 3 retries, the Cloud Function will send the failed event to the DLT.
  • Process DLT Events: You can create a separate Cloud Function or process the DLT events manually to handle the failures.

Summarize: Use combination of Pub/Sub and Cloud Function with DLT approach.


cheers,
DamianS
LinkedIn medium.com Cloudskillsboost