Sub topic 12
When Middleware Is the Better Choice
This chapter assumes you know Salesforce objects and records, can read REST and JSON, and have met the native options (Flow, Apex callouts, Named Credentials, Platform Events) and the limits from earlier chapters. Here the question is when those native tools stop being the right fit and a middleware layer such as MuleSoft earns its cost.
The core test
Native tools work best when Salesforce is one end of a single, simple conversation. Middleware pays off when the integration is a problem of its own that doesn't belong inside a CRM transaction. That means many systems, mismatched formats, strict delivery guarantees, or rules that must hold across several consumers.
A useful question is: "If Salesforce disappeared tomorrow, would this integration logic still need to exist?" If yes, the logic probably belongs in a separate layer. Apex triggers and flows are the wrong place to keep it.
Signals that point to middleware
One business process spans several systems
Suppose "create order" means checking credit in a finance system, reserving stock in an ERP, creating a shipment in a logistics API, and then updating Salesforce. This is orchestration: a sequence of calls with conditions and compensation steps. In Apex you can write it, but you then own the sequencing, state, and rollback logic inside a platform built for something else. Middleware lets you model the process outside Salesforce and expose it as one call.
Data shapes differ widely
Mapping a few fields is fine in a Flow or Apex. It gets harder with deeply nested XML, flat files, EDI documents, or two sources that must be joined before they can be loaded. Middleware has dedicated transformation languages, such as DataWeave, with streaming and schema tooling. The mapping also lives in one place and can be tested separately.
Delivery must be reliable
Native callouts are synchronous and tied to a transaction. If the remote system is down, your options are mostly to catch the exception and retry from a queueable job you write yourself. Middleware platforms provide durable queues, retry policies with backoff, dead-letter handling, and idempotency support. Reach for them when losing or duplicating a message has a real business cost, such as payments, inventory, or regulatory records.
Protocols and endpoints are not REST and JSON
Apex callouts speak HTTP. If a target needs SOAP with WS-Security, SFTP, JMS or AMQP queues, a database connection, or mainframe formats, you need protocol mediation. Middleware has connectors for these. Without it, you end up building a custom adapter somewhere, and that adapter is middleware anyway, just unsupported.
Limits make native design brittle
The limits you met earlier become design constraints at scale:
- A single Apex transaction allows 100 callouts, and you can't make a callout while uncommitted DML is pending.
- Synchronous callouts block the user's request. Slow downstream systems turn into slow pages.
- Daily API request allocations are shared across every integration against the org.
- Heap size caps how large a payload you can handle in one callout.
Moving high-volume or chatty traffic into a layer that batches, caches, and throttles protects the org. The Salesforce-side call becomes one small request instead of dozens.
Governance matters
When ten applications call the same ERP, you want one place to enforce authentication, rate limits, and service-level agreements, and one place to see traffic. Middleware, and API management in particular, gives you policies, client IDs, analytics, and alerting across all consumers. Native Salesforce tooling sees only Salesforce's side of each call.
The same capability is needed in many places
If mobile, web, partner, and Salesforce all need "get customer 360," build it once as a reusable API. This is the reuse argument behind the system, process, and experience API layers covered earlier. Point-to-point native calls duplicate that logic in each consumer.
How the signals map to what middleware gives you
| Signal | Native pain | What middleware adds |
|---|---|---|
| Multi-step process across systems | Long Apex chains, manual state, hard rollback | Orchestration flows with error scopes |
| Heterogeneous formats | Large mapping code, hard to test | Transformation language, reusable mappings |
| Must not lose messages | Custom retry jobs, lost context on failure | Durable queues, retries, dead-letter queues |
| Non-HTTP protocols | Custom adapters, unsupported | Connectors for SFTP, JMS, SOAP, databases |
| High volume or many callouts | Governor and API limits | Batching, throttling, caching |
| Many consumers | Duplicated logic, uneven security | Shared APIs with central policies |
A decision sketch
The diagram turns these signals into a quick screening. Treat it as a first pass, not a rule. Several "yes" answers raise the case for middleware, and a single "yes" rarely settles it.
Worked example: the join Salesforce should not do
A retailer keeps orders in an ERP and customer names in a separate CRM-adjacent master data system. Salesforce needs a combined list of recent orders per customer. Done natively, Apex would call two endpoints, loop and match records in memory, and handle partial failures. That uses two callouts per request, heap for both payloads, and custom error handling.
In middleware, the join happens once, outside Salesforce. This DataWeave script takes a payload with both lists and returns the merged shape:
%dw 2.0
output application/json
var customersById = payload.customers groupBy (c) -> c.id
---
payload.orders map (order) -> {
orderNumber: order.number,
total: order.total,
customerName: (customersById[order.customerId][0].name) default "Unknown",
External_Id__c: order.customerId
}The script assumes customer IDs are strings. groupBy builds a lookup keyed by ID so each order finds its customer without a nested loop. Salesforce then makes one call to one endpoint:
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Orders_API/customers/' + customerId + '/orders');
req.setMethod('GET');
req.setTimeout(20000);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() == 200) {
List<Object> orders = (List<Object>) JSON.deserializeUntyped(res.getBody());
System.debug(orders.size() + ' orders returned');
}The Apex code is almost the same as a direct callout. What changed is that the join, the error handling, and the second system's credentials now live in the integration layer. If the master data system is replaced, Salesforce code doesn't change.
Reliability in practice
Reliability is the signal teams most often underestimate. Compare what happens when the ERP times out:
- Native, synchronous: the user sees an error or a partial save. Any retry exists only if you built one, and a retry may create a duplicate order.
- Through middleware: the request is accepted and queued. The layer retries with backoff, checks an idempotency key so a retry cannot create a second order, and moves permanently failing messages to a dead-letter queue where support staff can inspect and replay them.
Platform Events help on the Salesforce side, because they decouple publisher from subscriber. They don't replace this. Subscribers still need to handle failures, ordering, and duplicates, and event retention is limited. Middleware commonly subscribes to the events and takes over delivery from there.
Signals that are weaker than they look
Some reasons teams give for middleware don't hold up on their own:
- "We might add more systems later." Plan for it, but don't pay for it today. A clean Named Credential and a small Apex service class can be migrated later.
- "It's more professional." Operating a platform has real cost, covered in the next chapter.
- "Apex can't do transformation." It can, for modest mappings. The signal is mapping complexity and the number of places that mapping is reused.
- One integration of moderate volume. A single request-reply call with a retry-safe design rarely justifies a platform.
Weigh these against the strong signals above. The best case for middleware combines several of them at once, such as many consumers plus a required delivery guarantee plus a non-REST protocol.
Checking your reasoning
For any proposal, write down which specific signals apply and what each would cost to handle natively. If you can't name concrete signals, the case for middleware isn't made yet. If you can name three or more, such as orchestration, guaranteed delivery, and shared reuse, a native build is probably accumulating hidden middleware inside your org.
Exercise
A company wants Salesforce to send a shipped-order notice to one internal REST service. The service is reliable, accepts JSON that matches Salesforce's field names, and no other system needs this data. A second proposal needs the same order event to update an ERP over SOAP, drop a file on an SFTP server for a partner, and notify a warehouse queue, with no lost messages allowed. Which proposal justifies middleware, and which signals support your answer?
Show answer
The second proposal justifies middleware. It involves several target systems (orchestration and fan-out), non-REST protocols (SOAP and SFTP, so protocol mediation), and a guaranteed-delivery requirement that calls for durable queues, retries, and dead-letter handling. The first proposal is a single reliable request-reply call with matching formats and one consumer, so a Named Credential with an Apex callout or a Flow HTTP callout is likely enough.