# How do architects manage transaction boundaries and rollback limitations when combining database DML with external callouts?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Transaction Boundaries, Rollback Limitations

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-architects-manage-transaction-boundaries-and-rollback-limitations-when-combining-database-dml-with

## Answer

**Basics:** A Salesforce transaction is all-or-nothing for database changes, but an external system's change is outside that transaction. If you save a record then call out and the call fails, you can roll back Salesforce but not the remote system. Worse, Salesforce blocks a callout after DML with the error "You have uncommitted work pending. Please commit or rollback before calling out".

**How it works:**
1. Pattern A: callout first, then DML (works in one transaction if the logic allows it).
2. Pattern B: DML commits, then an asynchronous job (Queueable with Database.AllowsCallouts, @future(callout=true), or platform event subscriber) performs the callout and updates status.
3. Pattern C: outbox, where the transaction inserts an "outbound message" record; a job processes the outbox and marks it Sent or Failed.
4. On failure, apply compensation (a reversing call) or retry with idempotency keys.

**Advanced / deep dive:** Savepoints and Database.rollback restore only Salesforce data; each setSavepoint and rollback counts as a DML statement toward the 150 limit. Triggers cannot make synchronous callouts, so triggers should enqueue jobs. Platform events default to Publish After Commit, so subscribers only act on committed data; Publish Immediately is not rolled back and is for logging. Distributed consistency uses eventual consistency and a saga: each step has a compensating step. Idempotency keys prevent duplicates when retrying after a timeout. Queueable chaining continues after failure only if you coded it; track attempts and use a dead-letter status. Batch Apex with Database.AllowsCallouts supports callouts per execute, with 100 callouts per execute transaction.

**Code / config example:**
```apex
public class OutboxJob implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext ctx) {
        List<Outbox__c> rows = [SELECT Id, Payload__c, Attempts__c FROM Outbox__c
                                WHERE Status__c = 'Pending' LIMIT 50];
        for (Outbox__c r : rows) {            // callouts first
            HttpRequest req = new HttpRequest();
            req.setEndpoint('callout:ERP/orders'); req.setMethod('POST');
            req.setHeader('Idempotency-Key', r.Id); req.setBody(r.Payload__c);
            try { r.Status__c = new Http().send(req).getStatusCode() < 300 ? 'Sent' : 'Failed'; }
            catch (CalloutException e) { r.Status__c = 'Failed'; }
            r.Attempts__c = (r.Attempts__c == null ? 0 : r.Attempts__c) + 1;
        }
        update rows;                          // single DML after all callouts
    }
}
```

**Hidden gotchas & must-remember points:**
- DML then callout in the same transaction throws an error; callout then DML is fine.
- Rollback does not undo external calls.
- Savepoints and rollbacks consume DML statements.
- Triggers and Flow after-save logic must use async paths for callouts.
- Retries need idempotency keys.
- Platform event "Publish Immediately" is not undone by rollback.

**Best practice:** Prefer the outbox pattern with idempotent retries for anything business critical.

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