Setting Up Dead Letter Topics in Google Cloud Pub/Sub
📖 Scenario: You are managing a messaging system using Google Cloud Pub/Sub. Sometimes, messages fail to be processed correctly. To handle these failures safely, you want to create a special topic called a dead letter topic where failed messages will be sent for later inspection.
🎯 Goal: Build a Pub/Sub subscription with a dead letter topic configured. This setup ensures that messages that cannot be processed after a certain number of attempts are moved to the dead letter topic automatically.
📋 What You'll Learn
Create a Pub/Sub topic named main-topic
Create a Pub/Sub topic named dead-letter-topic
Create a subscription named main-subscription to main-topic
Configure main-subscription with deadLetterPolicy pointing to dead-letter-topic
Set the maximum delivery attempts to 5
💡 Why This Matters
🌍 Real World
Dead letter topics help keep messaging systems reliable by isolating messages that cannot be processed, so they don't block the flow of good messages.
💼 Career
Understanding dead letter topics is important for cloud engineers and developers working with event-driven architectures and message queues to build fault-tolerant systems.
Progress0 / 4 steps
1
Create the main Pub/Sub topic
Write the gcloud command to create a Pub/Sub topic named main-topic.
GCP
Hint
Use the gcloud pubsub topics create command followed by the topic name.
2
Create the dead letter Pub/Sub topic
Write the gcloud command to create a Pub/Sub topic named dead-letter-topic.
GCP
Hint
Use the same command as before but with the new topic name.
3
Create the subscription to the main topic
Write the gcloud command to create a subscription named main-subscription to the topic main-topic.
GCP
Hint
Use gcloud pubsub subscriptions create with the subscription name and --topic flag.
4
Configure the subscription with dead letter topic and max delivery attempts
Write the gcloud command to update the subscription main-subscription to set the dead letter topic to dead-letter-topic and set the maximum delivery attempts to 5.
GCP
Hint
Use gcloud pubsub subscriptions update with --dead-letter-topic and --max-delivery-attempts flags. The dead letter topic must be specified with its full resource name.
Practice
(1/5)
1. What is the main purpose of a dead letter topic in Google Cloud Pub/Sub?
easy
A. To store all successfully delivered messages
B. To speed up message delivery
C. To catch messages that fail delivery multiple times
D. To encrypt messages before sending
Solution
Step 1: Understand message delivery failures
Messages that cannot be delivered after several attempts need special handling to avoid blocking the main flow.
Step 2: Role of dead letter topics
Dead letter topics collect these failed messages separately so the main topic stays clean and reliable.
Final Answer:
To catch messages that fail delivery multiple times -> Option C
Quick Check:
Dead letter topic = catch failed messages [OK]
Hint: Dead letter topics catch failed messages only [OK]
Common Mistakes:
Thinking dead letter topics speed delivery
Confusing dead letter topics with encryption
Assuming they store successful messages
2. Which block in a Pub/Sub subscription configuration is used to set a dead letter topic?
easy
A. message_storage_policy
B. retry_policy
C. ack_deadline_seconds
D. dead_letter_policy
Solution
Step 1: Identify configuration blocks for subscriptions
Pub/Sub subscriptions have specific blocks for retry, dead letter, and storage policies.
Step 2: Locate dead letter topic setting
The dead_letter_policy block is where you specify the dead letter topic and max delivery attempts.
Final Answer:
dead_letter_policy -> Option D
Quick Check:
Dead letter topic set in dead_letter_policy [OK]
Hint: Dead letter topic is in dead_letter_policy block [OK]
What happens when a message fails delivery 6 times?
medium
A. The message is retried indefinitely
B. The message is sent to the dead letter topic
C. The message is deleted immediately
D. The message is sent back to the main topic
Solution
Step 1: Understand max_delivery_attempts meaning
Messages are retried up to the max_delivery_attempts count before moving to dead letter topic.
Step 2: Apply max_delivery_attempts = 5
After 5 failed attempts, the 6th failure triggers sending the message to the dead letter topic.
Final Answer:
The message is sent to the dead letter topic -> Option B
Quick Check:
Failures > max_delivery_attempts = dead letter send [OK]
Hint: Messages move to dead letter after max attempts exceeded [OK]
Common Mistakes:
Thinking messages retry forever
Assuming immediate deletion on failure
Believing message returns to main topic
4. You configured a dead letter topic but messages are not appearing there after failures. What is a likely cause?
medium
A. Dead letter topic does not exist or lacks permissions
B. max_delivery_attempts is set to 1
C. Subscription ack deadline is too long
D. Main topic has no messages
Solution
Step 1: Check dead letter topic existence and permissions
If the dead letter topic is missing or the subscription lacks publish permission, messages cannot be sent there.
Step 2: Understand other options
max_delivery_attempts=1 means one try before dead letter, ack deadline affects delivery timing, and main topic empty means no messages to fail.
Final Answer:
Dead letter topic does not exist or lacks permissions -> Option A
Quick Check:
Dead letter topic must exist and be accessible [OK]
Hint: Dead letter topic must exist and allow publishing [OK]
Common Mistakes:
Ignoring permissions errors
Confusing ack deadline with dead letter setup
Assuming empty main topic causes dead letter issues
5. You want to ensure messages that fail more than 3 times go to a dead letter topic, but also want to limit the total retry time to 10 minutes. Which configuration approach is best?
hard
A. Set dead_letter_policy.max_delivery_attempts to 3 and configure retry_policy.minimum_backoff and maximum_backoff to limit retry duration
B. Set dead_letter_policy.max_delivery_attempts to 10 and rely on ack deadline for timing
C. Only set retry_policy.maximum_backoff to 10 minutes without dead letter policy
D. Set dead_letter_policy.max_delivery_attempts to 3 and disable retries
Solution
Step 1: Use max_delivery_attempts to limit retries
Setting max_delivery_attempts to 3 ensures messages move to dead letter after 3 failed deliveries.
Step 2: Control retry timing with retry_policy
Configuring minimum and maximum backoff controls how long retries take, helping keep total retry time within 10 minutes.
Step 3: Evaluate other options
Set dead_letter_policy.max_delivery_attempts to 10 and rely on ack deadline for timing ignores retry timing, C lacks dead letter handling, and D disables retries which may lose messages prematurely.
Final Answer:
Set dead_letter_policy.max_delivery_attempts to 3 and configure retry_policy minimum and maximum backoff -> Option A
Quick Check:
Combine max_delivery_attempts and retry_policy for retries and dead letter [OK]
Hint: Use max_delivery_attempts plus retry_policy for timing control [OK]
Common Mistakes:
Ignoring retry_policy for timing
Disabling retries loses messages
Setting max_delivery_attempts too high
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.