# What purpose do dead-letter queues serve in managing failed integration transactions and preventing data loss?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Dead-Letter Queues, Fault Tolerance

Read it on the site: https://www.forcechallenges.com/interview-questions/what-purpose-do-dead-letter-queues-serve-in-managing-failed-integration-transactions-and-preventing-data-loss

## Answer

**Basics:** In messaging, some messages can never succeed as sent (bad data, a deleted record, a bug), and endless retries just waste capacity. A DLQ isolates these "poison" messages. Salesforce has no single native DLQ product, so you build the pattern with what the platform offers, or rely on middleware (for example MuleSoft Anypoint MQ DLQs, or a broker such as Kafka or SQS).

**How it works:**
1. Process a message; on failure retry a limited number of times with backoff.
2. After the limit, persist the payload, correlation Id, error, attempt count and timestamp in a custom object (for example `Integration_Failure__c`) or Big Object.
3. Alert owners (Flow, email, Slack, dashboard).
4. After the fix, a replay job or admin action re-submits records marked "Ready to retry".

**Advanced / deep dive:** Platform building blocks:
- Platform event triggers: throw `EventBus.RetryableException` to have Salesforce redeliver the batch (up to 9 retries); after that, you must catch and persist the failure yourself.
- Queueable Finalizers (`System.Finalizer`, attached via `System.attachFinalizer`) run even after uncaught exceptions or limit failures and can log or re-enqueue.
- Batch Apex: implement `Database.RaisesPlatformEvents` to receive `BatchApexErrorEvent` for unhandled failures in a batch scope.
- Outbound messages queue and retry for 24 hours (extendable; verify the setting), and failed ones are visible in Setup, Monitor, Outbound Messages.
Key design point: write the failure record in a transaction that will not roll back. If the failing transaction is rolled back, a plain insert of the log disappears with it; use a platform event published immediately, or log in a separate async transaction. Include enough payload to replay, mask sensitive data, and set retention rules.

**Code / config example:**
```apex
public class SyncJob implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext qc) {
        System.attachFinalizer(new SyncFinalizer(qc.getJobId()));
        // ... callout work that may throw
    }
}
public class SyncFinalizer implements Finalizer {
    private Id jobId;
    public SyncFinalizer(Id j) { jobId = j; }
    public void execute(FinalizerContext ctx) {
        if (ctx.getResult() == ParentJobResult.UNHANDLED_EXCEPTION) {
            insert new Integration_Failure__c(Job_Id__c = jobId,
                Error__c = ctx.getException().getMessage());
        }
    }
}
```

**Hidden gotchas & must-remember points:**
- Retries without a cap create poison-message loops.
- Logging inside a rolled-back transaction loses the log.
- Replays must be idempotent, or you create duplicates.
- Platform events expire after the replay window, so they cannot serve as long-term storage.
- Store failures with a status field so replays are auditable.

**Best practice:** Cap retries, park failures with full context in a durable store, alert on arrival, and provide a controlled replay path.

---
Source: ForceChallenges · last updated 2026-11-02
