Search code examples
google-cloud-pubsub

How does an unacknowledged message gets stored in GCloud pubsub subscription?


I read most of the documentation available about how GCloud pubsub works and it's still not clear to me what happens when a message fails to be acknowledged by the subscriber? I can see on the subscriber settings you have the option to store unacknowledged messages for up to 7 days for free.1 I assume this should work if after many attempts the delivery is still unsuccessful and the message doesn't arrive where it's supposed to?

If this is the case, what is the point of using dead lettering for this subscription?(which is not free) 2. What if you use dead lettering but don't tick the box to enable message retaining (img 2), does it still retain unacknowledged messages?If yes for how long? Also while the subscription already stores it too?


Solution

  • If a message is not acknowledged, then it remains in the set of unacknowledged messages for a subscription. If there is no dead letter topic set up, then Cloud Pub/Sub attempts to redeliver this message up to the message expiration time, which can be up to seven days as you point out.

    You may want to use a dead letter topic for several reasons:

    1. The message that is not acknowledged could be of a format that your subscribers did not expect and is therefore causing them to crash. This would impact the ability of your subscribers to process any messages.
    2. If you are using push subscriptions, then repeatedly not acknowledging messages results in the backoff of delivery of all messages as the service assumes that the endpoint is unhealthy. Therefore, your end-to-end latency on all of your messages could go up.
    3. A message that you never acknowledge and just leave in the backlog could cause any Cloud Monitoring alerting on oldest unacknowledged message to fire.

    A dead letter topic is not any different from a regular Pub/Sub topic. When it is set as the dead letter topic for a subscription, it just means that the Pub/Sub service itself publishes to this topic on your behalf with the messages that were not acknowledged after being delivered up to the maximum number of delivery attempts. Once published to the dead letter topic, Cloud Pub/Sub acknowledges the message in the original subscription on your behalf, meaning it is no longer an unacknowledged message on that subscription.

    Consequently, the the message is independent of the original version of the message and is stored separately. If you do not select enable message retention nor have a subscription set up on the dead letter topic, then the message is not retained. If you do, then the message is stored until the topic retention deadline if using the former or the subscription message retention duration if using the latter.