← All Exercises

Route Exhausted Messages to a Dead Letter Queue

basicjavascript~20min

Background

Failed messages that stay in the main queue block healthy consumers. A deterministic move rule keeps throughput predictable and poison messages inspectable.

Problem

Given message attempt counts and a maximum, separate messages that must move to the dead letter queue from those that stay in the retry queue.

Input

JSON object: { messages: [{ id, attempts }], maxAttempts }

Output

JSON object: { deadLettered: string[], retry: string[] }

Sample tests

Sample 1

Input: {"messages":[{"id":"m1","attempts":3},{"id":"m2","attempts":1},{"id":"m3","attempts":5}],"maxAttempts":3}
Expected: {"deadLettered":["m1","m3"],"retry":["m2"]}

Sample 2

Input: {"messages":[{"id":"m4","attempts":0}],"maxAttempts":3}
Expected: {"deadLettered":[],"retry":["m4"]}

Your solution

Learning objective

Keep poison messages out of the hot path without losing them.