Skip to content
For Devs

Queues and Jobs for Fail-Safe Integrations

By Ana da Forcoder6 min read

Every integration with the outside world fails from time to time. SEFAZ goes offline, the bank takes a while to respond, the webhook arrives twice, the supplier's API returns an error during business hours. What separates a reliable system from one that causes rework isn't that the integration never fails: it's what happens when it does. And the main tool for this is queues and jobs.

At Forcoder, queues and jobs are at the core of systems that interact most with third parties, such as LogFrota—with SEFAZ, ANTT, banks and insurers—and the AI-powered support on our own website. In this article we explain the patterns we use to ensure an integration that cannot fail keeps working when the other side does.

  • Laravel
  • Redis
  • SEFAZ
  • ANTT
Mentioned in this article

Why integration shouldn't run on the user's request

When the user clicks "issue" and the system calls SEFAZ inside that same request, three bad things can happen. The screen freezes while waiting for the other side to respond. If the response takes longer than the server's limit, the request drops, but the call might have succeeded on the other side. And the user, left with no response, clicks again.

Putting the call in a job solves all three: the screen instantly replies with "in progress", the job can take as long as it needs, and the second click finds the first request still running instead of creating a new one.

The patterns we use

Write before processing

The received webhook is saved in full to a table, and only then does the sender get a confirmation. Processing comes later, in a job.

Idempotency

Each operation has a unique key. If the same key arrives again, nothing new is created.

Common failure vs. uncertain result

An error before the request leaves can be retried. If a failure happens after the request leaves, you must check before retrying.

Scheduled reconciliation

A periodic routine checks for what didn't get notified and retries what failed.

Common failure and uncertain result

This is where ignoring the point is most costly. Think about a toll-card purchase or the registration of a payment slip. If the connection drops before the request leaves, nothing happened externally and it's safe to retry. If it drops after, the supplier may have registered the purchase but failed to notify. Blindly retrying will purchase again.

How the job handles each type of failure
SituationRetry?What the job does
Failure to obtain token or query priceYesRetries with exponential backoff
Refusal with reasonNoRecords the reason and informs whoever needs to fix it
Drop after requestNot blindlyMarks as uncertain and checks before any new attempt
Accepted response but no identifierNot blindlyAlso uncertain: queries the supplier or asks an operator for confirmation

It's the same logic we apply to CT-e: communication failure keeps the document on hold for resending, but SEFAZ rejection is not retried, since the error is in what was sent.

Debounce: wait to execute just once

Not every job should run as soon as the event arrives. For AI-based chat support on our website, people messaging through WhatsApp often send several short messages in a row. If the AI replied to each individually, the conversation would become chaotic. The reply job waits a few seconds with no new messages, then responds just once, considering everything sent. The same principle applies to recalculating a report or syncing a record that changes multiple times quickly.

The cost of integration failures without a queue

A simple estimate of the team's time cost spent manually fixing integration failures in a month. Does not include fines, duplicate purchases, or lost clients.

Observability: knowing what happened with every job

A queue with no visibility just hides the problem. Here are the patterns we follow:

  • every external call is logged with request and response, sensitive data masked;
  • the state of each operation is shown in the requester's interface (in progress, completed, refused with reason, uncertain);
  • failed jobs are listed for reprocessing, not just hidden;
  • technical staff is alerted when the system is at fault, and the customer when the issue is credentials or registration.

The right question is not "does the integration work?" but "what does the system do when the other side doesn't respond?" This answer needs to be in the code before the first real failure.

Order and concurrency

Two jobs working on the same record at the same time is another source of silent errors. A payment webhook and scheduled reconciliation can both try to clear the same bill at the same second. To handle this, the job "locks" the record before processing, atomically, so the second job finds it already being processed and leaves without acting.

Order matters too. Closing the CIOT before the trip's final manifest, or processing a refund before the payment itself leads to impossible states. When order is part of business rules, it is coded as an explicit lock, never left to event arrival sequence. Late events that arrive after a final state do not reopen what is already closed.

Infrastructure

In our Laravel projects, queues run on Redis, with workers in containers separate from the web application and a scheduler for periodic checks. This allows for restarting the app without losing work in progress and scaling workers independently. For bank and gateway webhooks, event logging happens before any processing, as described in banking integration in the system and boleto and Pix with banks via API.

Checklist for an integration that cannot fail

  1. The call runs in a job, not in the user request.
  2. The second click joins the first request.
  3. Webhook saved before processing, with a unique key.
  4. Common failures and uncertain results are handled differently.
  5. Scheduled reconciliation for what wasn't notified.
  6. Log every call, masking sensitive data.
  7. Status visible on screen and a failure list for reprocessing.

Hire Forcoder

Forcoder builds systems and integrations that keep working when the other side fails. See our services, contact us or reach us on WhatsApp.

Frequently asked questions

Do queues slow down the system?

For the user, no: the screen responds instantly and shows progress. Heavy work happens in the background.

What happens if the server restarts with jobs in the queue?

With Redis-based queues and separate workers, jobs pick up where they left off when workers return.

Can any job be retried?

No. Only those that are idempotent, or that failed before the request left. Uncertain cases are checked before any retry.

Do you review integrations in existing systems?

Yes. We assess how each integration handles failures and propose changes based on risk priority.

How long should I wait before retrying?

It depends on the supplier. We use exponential backoff and respect each API's rate limits.

Keep reading

Practical Guide: Understanding Routers, Switches, Firewalls and Access Points

The foundation of any network infrastructure, whether home or corporate, lies in the devices that connect, control and protect data traffic. Routers, switches, firewalls and access points are the four pillars of that structure, yet it's still common to see questions about what each one does, how they…

Products by Forcoder and our clients

Software Forcoder built and maintains, for itself and for its clients.

Client case

Torela Real Estate

Real estate portal and system for Torela, in Gramado, Canela and the Rio Grande do Sul coast, integrated with Loft: listings reach the website without double entry, with search by region, city and neighborhood, property pages with photo galleries, favorites, comparison, vacation...

Client case

Nexus Finanças

Banking as a Service (BaaS) platform for Nexus Finanças: a digital account for each partner, with Pix, transfers, bill payments, charges with automatic split and cards, admin and partner dashboards, plus the company's corporate website.

Have a project in mind? Let's make it happen.

Custom websites, systems and integrations, focused on results. Tell us what you need and the Forcoder team will get in touch.