0
0
GCPcloud~5 mins

Message retention and acknowledgment in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you send messages between services, you want to make sure they are not lost and are processed only once. Message retention keeps messages available for a time, and acknowledgment tells the system you received and handled a message.
When you want to keep messages available for a short time in case the receiver is slow or temporarily down
When you want to confirm that a message was processed so it is not sent again
When you want to control how long unacknowledged messages stay in the system before being redelivered
When you want to avoid losing messages during network or service interruptions
When you want to manage message flow to prevent overload on your processing service
Config File - subscription.yaml
subscription.yaml
apiVersion: pubsub.cnrm.cloud.google.com/v1beta1
kind: PubSubSubscription
metadata:
  name: example-subscription
spec:
  topicRef:
    name: example-topic
  ackDeadlineSeconds: 20
  retainAckedMessages: true
  messageRetentionDuration: 600s

This file creates a subscription to a Pub/Sub topic.

ackDeadlineSeconds sets how many seconds the subscriber has to acknowledge a message before it is redelivered.

retainAckedMessages keeps messages even after acknowledgment for the retention duration.

messageRetentionDuration sets how long messages are kept (600s = 10 minutes).

Commands
This command creates a subscription named 'example-subscription' to the topic 'example-topic'. It sets the acknowledgment deadline to 20 seconds, retains acknowledged messages, and keeps messages for 10 minutes.
Terminal
gcloud pubsub subscriptions create example-subscription --topic=example-topic --ack-deadline=20 --retain-acked-messages --message-retention-duration=600s
Expected OutputExpected
Created subscription [example-subscription].
--ack-deadline - Sets how long to wait for message acknowledgment before redelivery
--retain-acked-messages - Keeps messages after acknowledgment for the retention duration
--message-retention-duration - Sets how long messages are retained
This command pulls one message from the subscription and automatically acknowledges it, telling the system the message was received and processed.
Terminal
gcloud pubsub subscriptions pull example-subscription --auto-ack --limit=1
Expected OutputExpected
DATA: "Hello, world!" ACK ID: ACK_ID_1234567890
--auto-ack - Automatically acknowledges the message after pulling
--limit - Limits the number of messages pulled
This command shows the current settings of the subscription, including acknowledgment deadline and message retention settings.
Terminal
gcloud pubsub subscriptions describe example-subscription
Expected OutputExpected
name: projects/my-project/subscriptions/example-subscription ackDeadlineSeconds: 20 retainAckedMessages: true messageRetentionDuration: 600s
Key Concept

If you remember nothing else from this pattern, remember: acknowledgment tells the system you processed a message, and retention keeps messages available for a set time even after acknowledgment.

Common Mistakes
Not acknowledging messages after pulling them
Messages will be redelivered repeatedly, causing duplicate processing
Always acknowledge messages after processing, either manually or with auto-ack flags
Setting acknowledgment deadline too short
Messages may be redelivered before processing finishes, causing duplicates
Set acknowledgment deadline long enough for your processing time
Not enabling message retention when needed
Messages are deleted immediately after acknowledgment, losing history or retry options
Enable retainAckedMessages and set messageRetentionDuration if you want to keep messages after acknowledgment
Summary
Create a subscription with acknowledgment deadline and message retention settings.
Pull messages and acknowledge them to prevent redelivery.
Check subscription settings to verify retention and acknowledgment configurations.