# Why is idempotency a critical architectural requirement when designing reliable inbound and outbound API integrations?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Idempotency, Architectural Reliability

Read it on the site: https://www.forcechallenges.com/interview-questions/why-is-idempotency-a-critical-architectural-requirement-when-designing-reliable-inbound-and-outbound-api

## Answer

**Basics:** In a distributed integration you cannot know whether a timed-out request was processed. The client must retry, so the server must tolerate repeats. Without idempotency, a retried "create payment" produces two payments. HTTP GET, PUT and DELETE are idempotent by definition; POST and PATCH are not, unless you design them to be.

**How it works:**
1. The caller generates a stable unique key per business operation (an order number or an `Idempotency-Key` UUID header).
2. Salesforce stores it in an **External ID** field (marked unique) on the record, or in a log object of processed messages.
3. Writes use **upsert** by that External ID: `Database.upsert(recs, Order__c.Ext_Id__c, false)` or REST `PATCH /sobjects/Order__c/Ext_Id__c/123`.
4. A repeated request updates the same record instead of inserting another.
5. For side effects (emails, payments) check a processed-keys table first and skip if it exists.

**Advanced / deep dive:** Upsert is atomic per record but two concurrent upserts for a not-yet-existing key can collide, one failing with `DUPLICATE_VALUE`; handle by retrying once, which then finds the record. Mark External ID fields unique so Salesforce enforces it (and choose the case-sensitivity setting deliberately). For outbound calls, send an idempotency key header if the target supports it. Events (Platform Events, Outbound Messages) can be delivered more than once and out of order, so use replay IDs, event UUIDs or version numbers: apply an update only if its version or timestamp is newer. Idempotent consumers also make retry and replay recovery safe after outages. Avoid relying on field-value comparison alone for deduplication; keys are more reliable.

**Code / config example:**
```apex
// Idempotent bulk load: reruns update, never duplicate
List<Order__c> recs = new List<Order__c>();
for (OrderDto d : dtos) {
    recs.add(new Order__c(Ext_Id__c = d.orderNo, Status__c = d.status, Version__c = d.version));
}
Database.UpsertResult[] res = Database.upsert(recs, Order__c.Ext_Id__c, false);
for (Integer i = 0; i < res.size(); i++) {
    if (!res[i].isSuccess()) { /* log res[i].getErrors() and decide retry */ }
}
```

**Hidden gotchas & must-remember points:**
- Upsert without an External ID field uses the record Id, which does not deduplicate.
- External ID fields must be marked unique to prevent duplicates.
- Concurrent first-time inserts can throw DUPLICATE_VALUE.
- An External ID can be up to 255 characters and counts toward the limit of 25 External ID fields per object.
- Ordering matters: ignore stale updates using a version field.
- Idempotent does not mean "ignore errors"; return the same outcome on a repeat.

**Best practice:** Require a unique business key on every write, upsert by it, and record processed message IDs for non-record side effects.

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