Dead letter topics in GCP - Time & Space Complexity
When using dead letter topics in Google Cloud Pub/Sub, it's important to understand how the time to process messages changes as the number of messages grows.
We want to know how the system behaves when many messages are sent and some are moved to the dead letter topic.
Analyze the time complexity of publishing messages and handling dead letter topics.
// Publish messages to a main topic
for (int i = 0; i < n; i++) {
publishMessage(mainTopic, message[i]);
}
// Messages that fail processing are sent to dead letter topic
subscribe(mainTopic, message => {
if (!process(message)) {
publishMessage(deadLetterTopic, message);
}
});
This sequence publishes n messages to a main topic and routes failed messages to a dead letter topic.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Publishing messages to the main topic and possibly to the dead letter topic.
- How many times: Publishing happens once per message (n times), and dead letter publishing happens for each failed message (up to n times).
As the number of messages n increases, the number of publish operations grows roughly in proportion to n.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 publishes to main topic, plus failed messages to dead letter topic. |
| 100 | About 100 publishes to main topic, plus failed messages to dead letter topic. |
| 1000 | About 1000 publishes to main topic, plus failed messages to dead letter topic. |
Pattern observation: The total operations grow linearly with the number of messages.
Time Complexity: O(n)
This means the time to handle messages and dead letter routing grows directly in proportion to the number of messages.
[X] Wrong: "Dead letter topics add a fixed extra cost regardless of message count."
[OK] Correct: The cost depends on how many messages fail and get sent to the dead letter topic, so it grows with message volume.
Understanding how message processing scales with dead letter topics shows you can reason about system behavior as load grows, a key skill in cloud design.
"What if the dead letter topic also triggers retries? How would that affect the time complexity?"