DEV Community

Cover image for The Safety Net: Adding a Dead Letter Queue (DLQ) to AWS Lambda
Eric Rodríguez
Eric Rodríguez

Posted on

The Safety Net: Adding a Dead Letter Queue (DLQ) to AWS Lambda

I've been building my AI Finance Agent for 35 days. It's smart, it's secure, and it's observable. But today, I asked: "What happens if it crashes?" In the serverless world, failed asynchronous events can disappear into the ether if not handled correctly. To prevent data loss, I implemented a Dead Letter Queue (DLQ) pattern.

Why you need a DLQ

When an AWS Lambda function fails (asynchronously), AWS retries it twice. If it fails a third time, the event is discarded. For a financial application, "discarding" a transaction update is unacceptable.

The Architecture

I introduced Amazon SQS (Simple Queue Service) into the mix.

Component: FinanceAgent-DLQ (Standard SQS Queue).

Role: Acting as a storage buffer for failed events.

Implementation Steps

Create Queue: Created a standard SQS queue in the console.

Link Lambda: Navigate to Lambda -> Configuration -> Asynchronous invocation.

Set Target: Set "On failure" to point to the new SQS queue.

Testing the Failure

To prove it works, I intentionally raised an Exception in my code: raise Exception("Testing DLQ!") After 2 retries, the Lambda gave up, but the message appeared instantly in my SQS queue. I could view the payload, understand the error, and I didn't lose the trigger data.

Takeaway: Resilience is cheap to implement (SQS is practically free) but invaluable when things go wrong.

Top comments (0)