Google Cloud Functions can't use Pub/Sub dead-letter topic

Viewed 1346

It seems GCP Cloud Functions always ACK Pubsub PUSH messages when invoked, even if function crashes or fails.

This prevents the use of the new Pub/Sub "dead-letter" topic feature, which required message delivery to fail before forwarding it to a different topic. Presently, messages never fail.

Is there a way to utilize "dead-letter topic" or pubsub re-delivery with Cloud Functions? Apart for setting the "retry" flag on the function itself, that doesn't solve this issue.

2 Answers

A push subscription impies 2 things:

  • create your function in --trigger-http mode
  • create a push PUSH subscription that use the Cloud Functions URL.

Here you have created a function in --trigger-topic mode, it's a background function.

You have here what you have to do for marking the function as failed.

Share your code if you need more help

One way that this can be handled (until GCP starts supporting manual acknowledgement for cloud function) is by re-queueing to the same topic with the retry count incremented in case of failure. You can store the retry counts in the payload itself like:

{
  "retry_count": 1,
  "data": {...}
}

Also to avoid the a faulty message being retried permanently, you can configure it to push to the dead-letter instead if the retry attempts > x.

This only solved the problem if the topi has only one subscriber though.

Related