# Errors, Retries and Dead-Letter Queues — Serverless & Edge

Source: https://www.skillbyai.com/en/serverless-edge/se-failure-handling

> Route failed events to a place you can see, retry only transient errors, and back off with jitter.

## Every failed event must land somewhere visible

In an event-driven system, every failure needs a destination. For **synchronous** calls, return a clear error status and let the client decide: client errors (4xx) should not be retried, transient server errors (5xx, timeouts) might succeed on retry. For **asynchronous** invocations, the platform retries for you and then sends the event to a **dead-letter queue (DLQ)** or an **on-failure destination** if you configured one; without one, the event is dropped after the retries. For **queue** sources, a message that keeps failing returns until it reaches the queue's maximum receive count and then moves to the queue's own DLQ. Every DLQ needs an alarm and a redrive plan, or it is just a quieter way to lose data. When *your* code calls another service, retry only transient errors, cap the attempts, and use **exponential backoff with jitter** so thousands of instances do not retry in lockstep and knock over a recovering dependency.

## The path of a failing event

An invocation fails, the platform or your code retries with growing delays, and after the last attempt the event is parked in a dead-letter queue for inspection.

![Four connected stages from left to right: an invocation, a failure, retries with backoff, and a dead-letter queue.](assets/figures/serverless-edge/section-8-map.svg) — Figure 8.1: invoke -> failure -> retry with backoff -> dead-letter queue

## Exponential backoff with full jitter

The ceiling doubles each attempt up to a cap, and the actual wait is a random value below it, which spreads retries out across many callers.

```js
// "Full jitter" backoff: wait a random time between 0 and an exponentially growing cap.
function backoffMs(attempt, baseMs = 100, capMs = 5000) {
  const ceiling = Math.min(capMs, baseMs * 2 ** attempt);
  return { ceiling, wait: Math.floor(Math.random() * ceiling) };
}

for (let attempt = 0; attempt < 7; attempt++) {
  console.log(`attempt ${attempt}: wait somewhere in [0, ${backoffMs(attempt).ceiling}) ms`);
}
```

Output:

```
attempt 0: wait somewhere in [0, 100) ms
attempt 1: wait somewhere in [0, 200) ms
attempt 2: wait somewhere in [0, 400) ms
attempt 3: wait somewhere in [0, 800) ms
attempt 4: wait somewhere in [0, 1600) ms
attempt 5: wait somewhere in [0, 3200) ms
attempt 6: wait somewhere in [0, 5000) ms
```

## Poison messages

A message that can never succeed, such as malformed JSON, will be retried until it hits the max receive count. Validate early, send clearly invalid messages straight to the DLQ with a reason, and do not let one bad record hold up a whole batch.

**Quiz:** An asynchronously invoked function keeps failing and has no DLQ or on-failure destination configured. What happens to the event after the retries are exhausted?

- [ ] It stays in the platform queue forever
- [x] It is discarded
- [ ] It is returned to the original caller as an HTTP error
- [ ] It is written automatically to an S3 bucket

*Answer:* It is discarded. Without a DLQ or destination, the event is dropped after the retries. Configure one and alarm on it.
