Sub topic 11
Choosing the right pattern
This chapter assumes you know the basics of Salesforce objects, REST and JSON, Apex triggers, and HTTP status codes. It also assumes you have read the earlier chapters on request-reply, fire-and-forget, and batch sync. Here you will turn those three patterns into a decision you can defend.
Start with the requirement, not the tool
Teams often pick a pattern because they already know the API. Start from the business need instead. Four questions cover most cases:
- Does the caller need the answer to continue? A user waiting on a screen for a credit check does. A warehouse that only needs to hear about a shipment does not.
- How much data moves at once? One record, a few records, or hundreds of thousands?
- What happens if the other system is down? Should the user see an error, or should the work queue up and finish later?
- How fresh must the data be? Seconds, minutes, or overnight?
Compare the patterns
| Factor | Request-reply | Fire-and-forget | Batch sync |
|---|---|---|---|
| Timing | Synchronous. The caller waits. | Asynchronous. The publisher moves on. | Scheduled or on demand, in volume. |
| Coupling | Tight. Both systems must be up. | Loose. Publisher and subscriber are independent. | Loose. Systems meet only at job time. |
| Typical volume | One record or a small set | Many small messages | Thousands to millions of records |
| Failure behavior | Caller sees the error right away. | Publisher knows only that the event was accepted. Subscribers handle their own failures. | Job reports per-record failures afterward. |
| Salesforce features | Apex callouts, REST API | Platform events, Change Data Capture | Bulk API 2.0 |
| Main limit pressure | Callout limits and timeouts | Event publishing and delivery allocations | Daily batch and job limits |
Check your own org's current limit values in the Salesforce documentation. They vary by edition and change between releases.
Decision flow
The flow is a starting point. Real designs often combine patterns.
Worked scenarios
Scenario 1: Show an invoice status on an account page. The user is looking at the page and needs the value now. Use request-reply. An Apex callout through a Named Credential fetches the status, as in the InvoiceClient example from the request-reply chapter. Keep the timeout short and show a clear message if the call fails.
Scenario 2: Tell the warehouse when an order ships. Salesforce does not need a reply, and the warehouse may be slow or offline. Use fire-and-forget. Publish Order_Shipped__e with EventBus.publish. The publisher checks isSuccess() and moves on. Subscribers receive the event on their own schedule.
Scenario 3: Load 2 million accounts from the ERP every night. Per-record callouts would exhaust limits and take too long. Use batch sync with a Bulk API 2.0 ingest job. Upsert on an external ID so a rerun does not create duplicates.
POST /services/data/v60.0/jobs/ingest
Content-Type: application/json
Authorization: Bearer <access_token>
{
"object": "Account",
"externalIdFieldName": "External_Id__c",
"contentType": "CSV",
"operation": "upsert",
"lineEnding": "LF"
}Scenario 4: Keep a few records in step between systems. The volume is low, but the caller does not need an answer. Either fire-and-forget or a small REST upsert works. Choose events if several systems need the change. Choose the REST upsert if exactly one system needs it and you want a direct confirmation.
Combine patterns when needed
Many designs use more than one pattern:
- Use fire-and-forget for real-time changes and a nightly batch job to reconcile drift. Events can be missed or retained only for a limited time, so reconciliation catches gaps.
- Use request-reply to validate input at entry, then fire-and-forget for downstream updates.
- Use batch sync for the initial load, then Change Data Capture for ongoing changes.
Common mistakes
- Using request-reply for everything. Synchronous callouts inside triggers or loops hit governor limits and make your users wait on a slow partner.
- Using events when you need an answer. Fire-and-forget gives you no business reply. If you need one, you must build a second event in the other direction, which adds design work.
- Using batch for freshness. A nightly job cannot meet a "within seconds" requirement.
- Skipping idempotency. Every pattern can deliver or run twice. Use external IDs so repeats are safe.
Exercise
A retailer needs to send each completed sale from Salesforce to an analytics system. About 50 sales happen per minute. Analytics can be up to 15 minutes behind, and Salesforce does not need any response. A second requirement is a weekly reload of the full product catalog of 800,000 records into Salesforce. Which pattern would you pick for each requirement, and why?
Show answer
For the sales, use fire-and-forget by publishing a platform event for each completed sale (or using Change Data Capture). No reply is needed, volume is modest and steady, and the analytics system can consume at its own pace without blocking Salesforce users. For the weekly catalog reload, use batch sync with a Bulk API 2.0 upsert job keyed on an external ID. The volume is too large for per-record callouts, the timing is scheduled, and the external ID makes a rerun safe.
Exercise
Why might a design use both Change Data Capture and a nightly Bulk API 2.0 job for the same data?
Show answer
Change Data Capture delivers changes in near real time, but events can be missed if a subscriber is offline longer than the retention window. A nightly batch job reconciles any drift and restores consistency. Using an external ID makes the batch upsert safe even when the records already match.