Google Professional Cloud Developer Question 413
Single answerGoogle Cloud PlatformYou are developing an application that consumes messages from a Google Cloud Pub/Sub subscription. The application processes messages asynchronously, and you want to ensure that each message is successfully acknowledged only after it has been fully processed. How should you implement message acknowledgment to avoid message loss or duplication?
- A
Acknowledge the message as soon as it is received from the subscription.
- B
Acknowledge the message only after the processing logic has successfully completed.
- C
Set the acknowledgment deadline to a very high value to ensure the message doesn't expire during processing.
- D
Use a Pub/Sub dead-letter topic to avoid handling acknowledgments manually.
Show answer and explanation
Correct answer: B
Explanation
In a Pub/Sub consumer application, messages should only be acknowledged after they have been successfully processed. This ensures reliable message delivery and avoids both message loss (due to premature acknowledgment) and duplication (due to failure to acknowledge after successful processing). Acknowledging messages before processing completion can lead to data inconsistencies, while relying solely on acknowledgment deadlines or dead-letter topics does not address the core issue of ensuring successful processing before acknowledgment.
- A. Incorrect.
Acknowledging the message as soon as it is received may lead to message loss if the application fails during processing since Pub/Sub assumes the message has been successfully handled.
- B. Correct.
Acknowledging the message only after the processing logic has successfully completed ensures that no unprocessed messages are lost or duplicated. This is the correct approach for reliable message handling.
- C. Incorrect.
Setting the acknowledgment deadline to a high value is not a reliable solution because it does not address the need to ensure processing is complete before acknowledgment. It also increases the risk of delays in re-delivery if the application fails.
- D. Incorrect.
While a dead-letter topic is useful for handling unprocessable messages, it does not replace the need for proper acknowledgment logic. Dead-letter topics handle failed messages, not acknowledgment timing.