BookatlasTwo-Day MuleSoft → Salesforce Bootcamp

Module 12 — Idempotency and Duplicate Prevention

AllInterview Q&A 76Cheat sheets 17Code 278

This is one of the most important integration concepts in the whole bootcamp because distributed systems regularly retry, redeliver, and lose responses.

The key question is:

If the same logical request is processed twice, do I get one correct business result or two unintended side effects?

12.1What idempotency meansA simple definition:18 words12.2Why duplicates happen even when your code is “correct”Imagine:37 words12.3codeThe easiest Salesforce idempotency mechanism: External ID + UpsertSuppose source sends:23 words12.4Business key vs Salesforce IDSuppose Mule gets:21 words12.5External ID needs to be truly stableBad external ID candidate:30 words12.6codeIdempotency keySometimes the operation itself doesn't naturally have a stable entity key.36 words12.7Business key vs request idempotency keyThese solve different problems.30 words12.8codeExample: order creationRequest:14 words12.9codeExample where Upsert can be wrongSuppose source sends:43 words12.10Where do you store processed idempotency keys?Common choices:24 words12.11Mule Object Store mental modelMule's Object Store is effectively persistent key/value storage available for state such as:19 words12.12But “check then insert” has a race conditionSuppose two Mule workers receive the same request simultaneously.32 words12.13Atomicity mattersYou need some mechanism that guarantees:25 words12.14Why Salesforce unique External IDs are valuableSuppose two Mule workers both simultaneously perform:43 words12.15The Query → Create raceClassic bug:14 words12.16codeIdempotency and database uniquenessIf Mule owns a database table:27 words12.17Status matters tooSuppose request key exists:27 words12.18codeA robust idempotency recordConceptually:14 words12.19Reused key with different payloadFirst request:21 words12.20TTL / expiryDo you store idempotency keys forever?40 words12.21codeEvent deduplicationMessage:17 words12.22But sometimes event ID dedupe is unnecessaryIf event means:33 words12.23Idempotent update vs non-idempotent incrementThis is a useful distinction.21 words12.24VersioningNow suppose events can arrive out of order.25 words12.25Idempotency vs orderingMemorize:15 words12.26Optimistic concurrencyThis is related.33 words12.27Last-write-winsThis is the simplest policy:18 words12.28Duplicate prevention at multiple layersA mature design may have:21 words12.29Example: customer upsertRequirement:18 words12.30Example: payment requestRequirement:19 words12.31The hardest payment failureSequence:31 words12.32Downstream-supported idempotency is bestIf payment API itself supports:29 words12.33Salesforce Create vs Upsert under concurrent retriesBad:21 words12.34Duplicate rules in SalesforceSalesforce itself can have duplicate-management rules.36 words12.35Why email-based duplicate rules aren't enoughTwo Contacts:28 words12.36Batch idempotencyNightly file:30 words12.37Job-level idempotencySuppose same input file is uploaded twice.22 words12.38Multi-level idempotencyA sophisticated import might have:11 words12.39Idempotency and side effectsSuppose flow does:23 words12.40Idempotent Salesforce write + non-idempotent notificationPossible fix:20 words12.41Another classic problem: create then publishRetry after timeout:10 words12.42Compensation is not idempotencySuppose duplicate Account is accidentally created and you later delete it.17 words12.43Exactly-once business outcomeInterviewers may say:50 words12.44Race condition scenarioInterviewer:64 words12.45Retry scenarioSalesforce times out during Account creation. What do you do?56 words12.46Duplicate event scenarioKafka/queue delivers the same customer event twice.44 words12.47Out-of-order scenarioVersion 42 is processed, then version 41 arrives.37 words12.48How to test idempotencyThis should be part of your automation suite.16 words12.49Test 2 — repeated retrySend:14 words12.50Test 3 — ambiguous timeoutMock downstream so that:14 words12.51Test 4 — same idempotency key, different payloadRequest 1:9 words12.52Test 5 — concurrent duplicate messagesPublish:6 words12.53Test 6 — stale versionCurrent Salesforce:9 words12.54Test 7 — workflow side effectsRequest does:12 words12.55Common bad patternsunder concurrency.33 words12.56Design checklistWhenever you see a write operation, ask:16 wordscheat sheetModule 12 Cheat SheetThe interview sentence to memorize is:106 words