A Cloud Function triggered by Pub/Sub is executing duplicate processing for some messages. Your team suspects at-least-once delivery is causing duplicate triggers. What is the recommended pattern to achieve exactly-once processing?

Direct Answer (Verified)Reviewed by Sunny (Senior Cloud Engineer)

Correct Answer: Implement idempotency in the function using a unique message ID checked against Firestore or Cloud Spanner before processing

Pub/Sub guarantees at-least-once delivery. The standard pattern for exactly-once semantics is **idempotent consumers**: before processing, check if the message ID (from `message.me...

๐Ÿ“‹Part of Professional Cloud Architect ยท GCP Certification Practice
Real Exam Scenario

A Cloud Function triggered by Pub/Sub is executing duplicate processing for some messages. Your team suspects at-least-once delivery is causing duplicate triggers. What is the recommended pattern to achieve exactly-once processing?

โœ“ Correct Answer & Explanation

Correct Answer

Implement idempotency in the function using a unique message ID checked against Firestore or Cloud Spanner before processing

Why This Answer Is Correct

Pub/Sub guarantees at-least-once delivery. The standard pattern for exactly-once semantics is **idempotent consumers**: before processing, check if the message ID (from `message.messageId`) has already been processed (stored in Firestore/Spanner/Redis). If yes, ack and skip. Pub/Sub's `exactlyOnce` mode reduces duplicates but is not guaranteed at the infrastructure level. Extending ack deadlines only delays redelivery; it does not prevent it.