Full answer
Flow
- The consumer fails to process a message. It does not ack. The message returns to the queue after a delay.
- The delay grows with each attempt: base * 2^(attempt - 1) (for example 5 s, 10 s, 20 s), with random jitter to avoid retry waves.
- After maxReceiveCount attempts, the broker (SQS redrive policy, RabbitMQ dead-letter exchange) or the consumer moves the message to the DLQ.
- Someone is alerted. The team inspects the payload and error, fixes the code or data, and redrives the messages to the main queue.
Good practice
- Classify errors: validation failure or unknown type is permanent (straight to the DLQ). A timeout or a 503 from a dependency is transient (retry).
- Keep the original message, attempt count, error message and timestamps in the DLQ.
- Put an alert on DLQ depth and on the age of the oldest message. A DLQ nobody watches is a silent loss.
- Retries make duplicates, so the consumer must be idempotent.
- Set a retention on the DLQ that is long enough for investigation. Test redrive in staging.
- Protect dependencies with a circuit breaker instead of letting every retry hit a failing service.
javascript
const MAX_RECEIVES = 3;
const backoff = (receiveCount) => 5 * 2 ** (receiveCount - 1); // 5 s, 10 s, 20 s (add jitter in real systems)
const queue = [
{ id: 'welcome-email-1', payload: { to: 'asha@example.com' }, visibleAt: 0, receives: 0 },
{ id: 'welcome-email-2', payload: { to: 'not-an-email' }, visibleAt: 0, receives: 0 }, // a poison message
{ id: 'welcome-email-3', payload: { to: 'ravi@example.com' }, visibleAt: 0, receives: 0 },
];
const dlq = [], done = [], log = [];
let fixed = false;
const handler = (msg) => { if (!/@/.test(msg.payload.to) && !fixed) throw new Error('invalid address'); };
function tick(now) {
const msg = queue.find((m) => m.visibleAt <= now);
if (!msg) return;
msg.receives++;
try { handler(msg); done.push(msg.id); queue.splice(queue.indexOf(msg), 1); }
catch (err) {
if (msg.receives >= MAX_RECEIVES) {
queue.splice(queue.indexOf(msg), 1);
dlq.push({ ...msg, lastError: err.message });
log.push(`t=${now}s ${msg.id}: attempt ${msg.receives} failed, moved to the DLQ`);
} else {
msg.visibleAt = now + backoff(msg.receives);
log.push(`t=${now}s ${msg.id}: attempt ${msg.receives} failed, retry in ${backoff(msg.receives)}s`);
}
}
}
for (let now = 0; now <= 60; now++) tick(now);
console.log(log.join('\n'));
console.log('processed :', done.join(', '));
console.log('dead letter :', dlq.map((m) => `${m.id} (${m.receives} attempts, "${m.lastError}")`).join(', '));
console.log('main queue left :', queue.length);
// After the bug or the data is fixed, redrive the DLQ.
fixed = true;
while (dlq.length) { const m = dlq.shift(); m.receives = 0; m.visibleAt = 0; queue.push(m); }
for (let now = 61; now <= 65; now++) tick(now);
console.log('after redrive :', done.join(', '), '| dead letter =', dlq.length);Simulated time in seconds. One message always fails until the 'fix', and the backoff, DLQ move and redrive are printed in order. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): t=1s welcome-email-2: attempt 1 failed, retry in 5s t=6s welcome-email-2: attempt 2 failed, retry in 10s t=16s welcome-email-2: attempt 3 failed, moved to the DLQ processed : welcome-email-1, welcome-email-3 dead letter : welcome-email-2 (3 attempts, "invalid address") main queue left : 0 after redrive : welcome-email-1, welcome-email-3, welcome-email-2 | dead letter = 0
Key points
- Limit retries and back off between attempts.
- Move messages that keep failing to a DLQ.
- Separate transient from permanent errors.
- Alert on DLQ depth and redrive after the fix.
- Retries need idempotent consumers.
Common mistakes
- Retrying forever with no limit.
- Retrying immediately in a loop, hammering a failing dependency.
- A DLQ with no alert.
- Dropping failed messages instead of keeping them.
- Retrying permanent errors.
Follow-ups
- How does SQS manage the redrive policy?
- What is jitter and why add it?
- How would you handle a message that fails because a downstream service is down for an hour?
Tips
- Walk through attempt, delay, DLQ, redrive.
- Separate transient from permanent errors.
- Mention alerts.