Sub topic 2
Synchronous and asynchronous communication
This chapter assumes you know the Salesforce data model, basic Apex, HTTP methods and status codes, and how to read JSON. It covers the timing choice that sits underneath every integration pattern in this tutorial. That choice is whether the caller waits for an answer.
The core distinction
In synchronous communication, the caller sends a message and blocks until the receiver responds. The caller's next step depends on that response.
In asynchronous communication, the caller hands the message off and continues. The receiver processes it later, and the caller either never hears back or learns the outcome through a separate channel.
The three patterns map onto this choice:
| Pattern | Timing | Caller waits? |
|---|---|---|
| Request-reply | Synchronous | Yes, for the response |
| Fire-and-forget | Asynchronous | No, only for acceptance |
| Batch sync | Asynchronous | No, results are checked later |
Trade-offs
Each style gives you something and costs you something. The comparison below uses three dimensions: coupling, failure behavior, and consistency.
| Dimension | Synchronous | Asynchronous |
|---|---|---|
| Coupling in time | Both systems must be up at the same moment | Systems can be up at different times |
| Failure behavior | The caller sees the error immediately and can react | Failures surface later, so you need retry and monitoring |
| Data freshness | The answer is current when it returns | The receiver's state lags behind |
| User experience | A slow receiver makes the user wait | The user is not blocked |
| Complexity | Simple to reason about | Needs idempotency, ordering, and replay handling |
Latency and the user
Salesforce runs your Apex in a shared, multitenant environment. A callout that holds a transaction open uses up a limited resource. A slow external system therefore affects your users directly, and it can also hit platform limits. The next chapters cover those limits in detail. For now, remember that a synchronous design is only as fast and as available as the slowest system it calls.
Failure behavior
A synchronous failure is loud. The call times out or returns a 5xx status code, and your code decides what to do right away. An asynchronous failure is quiet. The publisher got an acknowledgement, so it believes the work is done. If the subscriber then fails, something else must notice. That is why asynchronous designs rely on retries, replay, and monitoring.
Asynchronous is not one thing
Two different asynchronous shapes appear in this tutorial, and they behave differently.
- Event or message delivery. Small messages are published and consumed independently. The caller receives only an acceptance result. This is fire-and-forget.
- Bulk or deferred processing. A large set of records is submitted as a job. You poll for completion or check results afterward. This is batch sync.
Both free the caller from waiting, but they differ in volume, ordering, and how you learn the result.
Making the choice
Ask these questions in order:
- Does the caller need the answer to continue? If yes, lean synchronous. An example is showing a live invoice status on a page.
- Can the receiver be unavailable for a while without harm? If yes, lean asynchronous.
- Is the volume large? If yes, a bulk asynchronous job usually fits better than many individual calls.
- Can the user tolerate waiting? If not, move the work off the user's request.
You can also combine styles. A common design is a synchronous call that only validates and enqueues a request, followed by asynchronous processing of the real work. The caller gets a fast answer, and the heavy work happens later.
Common mistakes
- Making everything synchronous. Chaining several external calls in one transaction multiplies latency and failure points.
- Treating asynchronous as reliable by default. Without retries and idempotency, a message can be lost or processed twice.
- Ignoring ordering. Asynchronous messages may arrive out of order, so design receivers that tolerate this.
- Hiding failures. Silent asynchronous errors stay unnoticed until the data is already wrong.
Exercise
A sales rep clicks a button to check a customer's credit limit in an external system, and the page must show the result. A separate requirement says that after an order ships, a warehouse system should be notified, but Salesforce does not need a reply. Which timing style fits each requirement, and why?
Show answer
The credit check is synchronous (request-reply) because the page cannot continue without the answer, and the rep is waiting. The shipping notification is asynchronous (fire-and-forget) because Salesforce needs no reply, and the warehouse system being briefly unavailable should not block the order process. For the asynchronous case, plan for retries and monitoring, since failures will not be visible to the publisher.
Exercise
A publisher receives a success result after publishing an event. Why can it not conclude that the subscriber updated its data?
Show answer
The success result only confirms that the platform accepted the event. The subscriber processes it separately and later, and that processing can fail or be delayed without the publisher being told.