What robust error handling and retry strategies should architects implement for mission-critical inbound integrations?
Answer
Basics: Integrations fail: timeouts, 5xx errors, rate limits, record locks, bad data. Without a strategy, data silently disappears or duplicates. Inbound means external systems calling Salesforce (Apex REST, REST API, Platform Event subscribers), so you control the response; outbound retries are the caller's job.
How it works:
- Validate input early and return precise status codes: 400 or 422 for bad data (not retryable), 409 for conflicts, 429 or 503 with
Retry-Afterfor throttling, 500 for unexpected errors. - Catch exceptions, and write the failed payload, error and correlation ID to an error object (for example
Integration_Error__c). - Callers retry only on retryable codes (REQUEST_LIMIT_EXCEEDED, UNABLE_TO_LOCK_ROW, 5xx, timeouts) using exponential backoff with jitter.
- After N attempts, park the message for manual review (dead-letter) and notify.
- Operations must be idempotent so retries are safe.
Advanced / deep dive: Apex has no sleep, so implement delay using Queueable chaining with System.enqueueJob(job, delayMinutes) (0 to 10 minutes) or scheduled Apex. A critical trap: if the transaction fails, your error-log DML rolls back too. Log through a Platform Event with Publish Immediately behavior or from a separate Queueable, or capture the error with Database partial-save methods so the transaction still commits. For batch inbound loads use Database.insert(records, false) and process SaveResult. Platform event triggers can retry with EventBus.RetryableException, which re-delivers the batch up to the limit (verify the current count). Add a circuit breaker so you stop hammering a failing endpoint, monitor with dashboards, and alert via custom notifications, email or Slack.