Search code examples
google-cloud-platformgoogle-cloud-pubsub

GCP Pub/Sub - Message Expiry


As per the GCP documentation, messages are retained as below:

  • By default, a topic discards messages as soon as they are acknowledged by all subscriptions attached to the topic. A topic can retain published messages for a maximum of 31 days
  • By default, Pub/Sub discards a message from a subscription as soon as the message is acknowledged. Unacknowledged messages are retained for a default of 7 days (configurable)
  • However, messages can be retained in a subscription for more than 7 days if the message retention duration configured on its topic is greater than 7 days.

Question - Lets say we have single subscriber. Pub/Sub Topic is set to 2 days retention. In this case, does pub/sub retains message for 2 days after it is being read or discards immediately and not available for reply if needed within 2 days? Little confused with the statement "By default, Pub/Sub discards a message from a subscription as soon as the message is acknowledged".


Solution

  • Retention time is measured purely in terms of publish time. So two days of retention means two days since the time of publish. If you want to seek to redeliver the message after it has been acknowledged by the subscriber, Then two things must be true:

    1. The subscription must have retain ack messages enabled or the topic must have message retention enabled.
    2. The retention time must be greater than the time since the message was published.

    So in the case where topic retention is enabled and set to two days, then if the message has been delivered and acknowledged by a subscriber for the subscription, then it is possible to replay the message via a seek within two days of the original publish time.