Integrations with external systems (an ERP, an e-signature platform, a service desk tool) rarely fail in original ways. The other side gets slow, returns a 5xx, rate-limits you, or answers after your HTTP client has already given up. The question is not whether this will happen, but what your system does when it does.
At Grupo FCamara, I work on asynchronous integrations with SAP, DocuSign, and Freshservice. This article collects the decisions that mattered most in that kind of integration, without internal details from that project: the examples below are generic and use BullMQ with Redis, which is what we use to orchestrate jobs.
1. Take the integration out of the request path
If the call to the external system happens inside the user’s request, every instability on the other side becomes yours. The first step is to record the intent and respond quickly:
await integrationQueue.add('contract.signed', {
contractId,
correlationId,
});
A worker consumes the queue and talks to the external system at its own pace. The original request only depends on Redis being up.
2. Idempotency: processing twice must not have a double effect
With retries, the same message will be processed more than once: the worker can crash after calling the API and before marking the job as completed. The integration has to tolerate that.
The pattern I use is to derive a stable key from the business event, not from the job:
const idempotencyKey = `contract-signed:${contractId}`;
if (await processed.has(idempotencyKey)) return; // already delivered
await externalApi.createDocument(payload, { idempotencyKey });
await processed.save(idempotencyKey);
When the target API accepts an idempotency key, send the same key on every attempt. When it does not, the local check before the call (and, when possible, a lookup on the target) narrows the window for duplicates.
3. Retries with backoff, and only for what is worth retrying
Retrying immediately usually makes things worse for a service that is already overloaded. BullMQ lets you configure attempts with exponential backoff and jitter, which spreads the retries over time:
await integrationQueue.add('contract.signed', data, {
attempts: 5,
backoff: { type: 'exponential', delay: 2_000, jitter: 0.5 },
});
Not every error deserves a retry. A timeout or a 503 is transient; a 400 caused by an invalid payload will fail the same way five times. For those cases, BullMQ provides UnrecoverableError, which ends the job without spending the remaining attempts:
import { UnrecoverableError } from 'bullmq';
if (response.status === 400) {
throw new UnrecoverableError(`Payload rejected: ${response.body}`);
}
4. A DLQ for what did not work
After the last attempt, the message cannot just disappear. A DLQ (dead letter queue) keeps what failed for good, with enough context for someone to investigate and reprocess it:
worker.on('failed', async (job, error) => {
if (!job) return;
const permanent = error.name === 'UnrecoverableError';
const attemptsLeft = job.attemptsMade < (job.opts.attempts ?? 1);
if (!permanent && attemptsLeft) return; // will retry again
await deadLetterQueue.add(job.name, {
data: job.data,
reason: error.message,
failedAt: new Date().toISOString(),
});
});
One detail that is easy to miss: a job stopped by UnrecoverableError on its first attempt has attemptsMade equal to 1, not to the maximum. If the check only looks at the attempt count, permanent errors are exactly the ones that never reach the DLQ.
A DLQ is only useful if someone looks at it. An alert when it grows and a simple way to reprocess a message after the fix matter as much as the queue itself.
5. One correlation ID from start to finish
When an integration fails, the first question is “what happened to this contract?”. A correlationId generated at the source and carried in the message, the logs, and the external calls answers that with one search, instead of rebuilding the story from timestamps.
What I take from this
- Retries without idempotency trade one problem (a lost message) for another (a duplicated effect).
- Classifying errors as transient or permanent is as much a business decision as a technical one, and it deserves to be explicit in the code.
- The DLQ and the correlation ID look like operational details, but they are what turn a failure into a short ticket instead of a long investigation.
On that project, these practices are part of the work that contributed to a 22% reduction in integration-related tickets. They were not the only cause, but they made failures predictable, and predictable failures are the ones you can handle.
The complete code, with tests against a real Redis and a simulated external system that fails on purpose, is in reliable-integration-worker.