15

An early version of Google Cloud Functions had a limitation with regards to retries when errors occurred. They have since provided enhancements that resolve this issue.


We are using a cloud function triggered by Pub/Sub to ensure delivery of an e-mail. Sometimes the e-mail service takes a long time to respond and our cloud function terminates before we get an error back. Since the message has already been acknowledged our e-mail gets lost.

The cloud function appears to be sending an ACK the Pub/Sub message automatically when we are called. Is there a way to delay the ACK until the successful completion of our code? Alternatively is there a way to catch timeouts and requeue the message for delivery? Something else we could try?

Geoffrey
  • 377
  • 2
  • 11

2 Answers2

18

I heard from Google support that they do not currently provide the means to delay the ACK when a cloud function is invoked by Pub/Sub. If you want to use cloud functions with Pub/Sub you need to handle the error case yourself. For example you could have your cloud function requeue a message for the retry with a retry count.

This would seem to make it unnecessarily difficult to guarantee execution with Pub/Sub and cloud functions.

Geoffrey
  • 377
  • 2
  • 11
  • 4
    this was a terrible design decision. They should have left it up to the cloud function to ack the message. – amohr Aug 04 '17 at 19:21
  • 1
    Or perhaps only ack'd it when the function finished successfully. – Andy Hume Sep 26 '17 at 06:09
  • 2
    Any best practices on how to overcome this? I thought about using a timer and posting the same message to the topic again. Anything better? – Leo Oct 15 '17 at 13:47
  • Any update on this? For the time being I have instead been using a HTTP trigger where I can ack the message myself with a 2xx response but it feels like it's an unnecessary workaround. – Robula Jun 28 '18 at 07:38
  • 3
    There is another way by setting `--retry` on the trigger function. https://cloud.google.com/functions/docs/bestpractices/retries – Robula Jun 28 '18 at 08:22
  • That's a much less useful way though; pubsub topics support backoff, function retries are immediate. – El Yobo Mar 26 '21 at 02:00
  • This is very very big limitation... – Jonathan Chevalier Nov 28 '21 at 19:49
7

This is a problem because Functions ACKing a message on invoke, even if they crash, prevents the use of the new "dead-letter" feature.

Also, it goes against the docs. see note after this code sample: https://cloud.google.com/functions/docs/calling/pubsub#sample_code

Nihil
  • 146
  • 1
  • 5
  • 3
    It looks like Google has added some retry support since this question was asked three years ago. I agree with you that the answer has changed. – Geoffrey May 16 '20 at 12:43