Message retention and acknowledgment in GCP - Time & Space Complexity
When working with message systems, it is important to know how long messages stay and how acknowledging them affects processing time.
We want to understand how the time to process messages grows as more messages are handled.
Analyze the time complexity of the following operation sequence.
// Pull messages from a subscription
const [messages] = await subscription.pull({ maxMessages: n });
// Process each message
for (const message of messages) {
processMessage(message);
// Acknowledge message to remove it from the queue
await subscription.acknowledge(message.ackId);
}
This sequence pulls n messages, processes each one, and acknowledges them to remove from the queue.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Pulling messages and acknowledging each message.
- How many times: Pull is called once per batch; acknowledge is called once per message (n times).
As the number of messages n increases, the number of acknowledgments grows linearly because each message needs one acknowledgment.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 pull + 10 acknowledges = 11 |
| 100 | 1 pull + 100 acknowledges = 101 |
| 1000 | 1 pull + 1000 acknowledges = 1001 |
Pattern observation: The total operations grow roughly in direct proportion to the number of messages.
Time Complexity: O(n)
This means the time to process and acknowledge messages grows linearly with the number of messages.
[X] Wrong: "Acknowledging messages happens once for the whole batch, so time stays constant no matter how many messages."
[OK] Correct: Each message requires its own acknowledgment call, so more messages mean more acknowledgments and more time.
Understanding how message acknowledgment scales helps you design systems that handle growing workloads smoothly and reliably.
"What if acknowledgments were batched instead of sent individually? How would the time complexity change?"
Practice
Solution
Step 1: Understand message retention
Message retention defines how long unacknowledged messages are kept before deletion.Step 2: Effect of no acknowledgment within retention
If a message is not acknowledged before retention expires, it is deleted and lost.Final Answer:
The message is deleted and no longer available for delivery. -> Option BQuick Check:
Unacknowledged message + retention expired = deleted [OK]
- Thinking messages auto-acknowledge after retention
- Assuming messages move to dead-letter queue automatically
- Believing messages duplicate on retention expiry
Solution
Step 1: Identify acknowledgment method
In Pub/Sub client libraries, acknowledging a message is done by callingack()on the message object.Step 2: Verify other options
Methods likedelete(),complete(), orfinish()are not valid for acknowledgment.Final Answer:
Callmessage.ack()after processing the message. -> Option AQuick Check:
Use message.ack() to acknowledge [OK]
- Using incorrect method names like delete or finish
- Not acknowledging messages at all
- Confusing acknowledgment with message deletion
def callback(message):
print(f"Received: {message.data.decode('utf-8')}")
# No acknowledgment called here
subscriber.subscribe(subscription_path, callback=callback)
What will happen to the messages if the callback never calls message.ack()?Solution
Step 1: Understand callback behavior without ack
Ifmessage.ack()is not called, Pub/Sub considers the message unacknowledged.Step 2: Effect on message delivery
Unacknowledged messages are redelivered repeatedly until acknowledged or retention period expires.Final Answer:
Messages will be redelivered until acknowledged or retention expires. -> Option CQuick Check:
No ack means redelivery until retention ends [OK]
- Assuming messages auto-acknowledge after callback
- Thinking messages are lost immediately without ack
- Believing messages go to dead-letter topic without config
Solution
Step 1: Check retention vs acknowledgment
Retention controls how long unacknowledged messages stay. If messages are acknowledged, they are removed immediately.Step 2: Analyze disappearance cause
If messages disappear early, likely they were acknowledged by the subscriber before 1 day.Final Answer:
Messages are being acknowledged by the subscriber before 1 day. -> Option AQuick Check:
Acknowledged messages removed before retention ends [OK]
- Assuming retention is ignored by Pub/Sub
- Believing push subscriptions don't support retention
- Thinking messages auto-delete after 24 hours always
Solution
Step 1: Understand retention and acknowledgment roles
Retention keeps unacknowledged messages for specified time. Acknowledgment removes messages to avoid duplicates.Step 2: Choose configuration to meet goals
Setting retention to 3 days keeps messages if unacknowledged. Prompt acknowledgment removes processed messages, preventing duplicates.Final Answer:
Set message retention duration to 3 days and ensure subscriber acknowledges messages promptly. -> Option DQuick Check:
Retention + prompt ack = no duplicates, messages kept [OK]
- Disabling acknowledgments causing duplicates
- Setting retention to 0 losing unacked messages quickly
- Never acknowledging causing message buildup and duplicates
Ready to prove it?
Pass the test and this pattern moves unknown
