Search code examples
pythongoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-pubsub

Consume messages from Pub/Sub with Cloud Functions untill topic is empty


I'm facing a technical challenge with a message processing system using a Pub/Sub topic, where my Cloud Function processes messages individually. However, I've encountered a scenario where if there are messages that fail to be read and remain in the topic, my Cloud Function continues to execute repeatedly.

I'm seeking advice or solutions to implement a more robust approach to prevent my Cloud Function to execute infinitely when unprocessed messages persist. Any help or ideas would be greatly appreciated. Thank you in advance


Solution

  • If your function does not process correctly the message, it must return a NACK, materialized by a non 2XX HTTP error code (usually a 5XX or a 4XX). And indeed, the message is resubmitted to your Cloud Functions (if you check the retry option).

    The solution here is to unbind the Cloud Functions and the Topic. In fact, when you deploy a Cloud Functions on a topic, a PubSub subscription is created automatically and attached to your Cloud Functions.

    The idea is the following:

    • Create an HTTP functions (and not a background function that receive PubSub event)
    • Create a PubSub subscription that invoke your Cloud Functions endpoint.

    From here, you will have the same behavior. Now the trick: Because you manage the PubSub subscription, you can configure a dead letter topic on it. It means, after 5 retries (min) or more, the message is posted to the dead letter topic and remove from the subscription.

    Then, do whatever you want with the dead letter messages (log them, trash them,....)