Think about your scariest background job. What would happen if it crashed between two of its lines? Would your customer get charged twice? Would a webhook silently not go out? How much time do you already lose manually cleaning up issues like this?
Here's an example we'll use for the rest of this post:
class ChargeOrderJob < ApplicationJob
def perform(order_id)
order = Order.find(order_id)
return if order.paid? # double-clicks enqueue this twice
payment = Stripe::PaymentIntent.create(
amount: order.total_cents,
currency: "usd",
customer: order.customer.stripe_id,
payment_method: order.customer.default_payment_method_id,
off_session: true,
confirm: true
)
# ๐ฅ crash point 1: the retry charges the card again
order.update!(status: :paid, stripe_payment_intent_id: payment.id)
# ๐ฅ crash point 2: the retry sees paid?, returns early, and the warehouse never hears about the order
SendFulfillmentWebhookJob.perform_later(order.id)
end
end
The Problem
There are three problems hiding in this job, and you very likely have at least one of them in your codebase.
More than one side effect in a job
When a job calls an API then writes to the database or calls two different APIs, there is no way to make those actions happen together or not at all. This creates a window between the two where a failure causes a retry that repeats the first side effect. (This is assuming your jobs retry on failure. Sidekiq does that out of the box. With Solid Queue you opt in with Active Job's retry_on.)
"Failure" means anything here. Sure, a hard crash counts: OOM kills, SIGKILL from a long deploy, etc. But, a plain old exception does the same damage. update! raising a validation error or a database connection blip right after the Stripe call, and you're in the window. Those are way more common than OOM kills.
The result is double work: duplicate charges, duplicate emails, duplicate webhooks. This is illustrated by crash point 1 in ChargeOrderJob.
Jobs enqueueing other jobs
The second pattern is background job chaining. When a job enqueues the next job it's doing a dual write: the job's work commits to your app database, and the enqueue commits to wherever your queue keeps its jobs. That's another window. Fail inside this one and the only thing that gets the next job enqueued is a retry that makes it all the way back to that line. If the retries run out, or the job gets dropped entirely (Sidekiq's free version loses in-progress jobs on a SIGKILL), your chain silently stops.
You're only safe if your queue is database-backed, shares your application database, and you enqueue inside the same transaction as the write. But, it's generally recommended to keep the two separate. With Rails 8, Solid Queue defaults to its own database.
This time the result is lost work: orders that get charged but never ship, receipts that never send. Crash point 2 in our example ChargeOrderJob sits in this window, and the next section shows why its retry never makes it back to the enqueue.
The guard makes it worse
Now look at the return if order.paid? guard clause. That's the guard you'd add so a job that runs twice doesn't charge twice. It doesn't actually help where it matters.
At crash point 1 the order isn't marked paid yet, so the guard never fires and the retry charges again anyway. And at crash point 2 the guard is exactly what makes the retry return early and skip the enqueue. What felt like an obvious patch here fixes nothing in the scary window and opens up a new one.
The Atomicity Gap
Double work and lost work look like two different bugs, but they come from the same place. Your job reads like one unit of work, but it's really a handful of separate commits to separate systems: Stripe, your database, your queue. Nothing ties those commits together, so every boundary between them is a spot where a failure can leave the work half done.
I call this the Atomicity Gap: the gap between the atomic units in your background jobs.
The Solution
Crashes are just gonna happen, and a job that touches more than one system is never truly going to be atomic. Here's what you can do to address these issues:
- Make the seams between units of work durable, so a crash won't drop or repeat it.
- Shrink each unit of work until it's atomic on its own. That's a step.
Ductwork is built around these two ideas. You write each unit of work as a step, and Ductwork closes the gap between them with a Durable Seam. It's durable workflows for Ruby built in Ruby that run inside your app. Ductwork only requires a single database (PostgreSQL, MySQL, or SQLite).
Rewriting the scariest job
Let's check out ChargeOrderJob again. Before the single job had three side effects and two crash points. Using Ductwork, it becomes a workflow with one step per side effect.
class CheckoutWorkflow < Ductwork::Workflow
define do |workflow|
workflow.start(ChargeCard)
.chain(to: MarkOrderPaid)
.chain(to: NotifyFulfillment)
end
end
Kicking it off is a one-line change. ChargeOrderJob.perform_later(order.id) becomes CheckoutWorkflow.trigger(order.id).
Looking closer at the steps, you don't have to manually kick off the next step. You make the workflow definition and all orchestration is handled for you. Here is the ChargeCard step:
class ChargeCard < Ductwork::Step
def initialize(order_id)
@order = Order.find(order_id)
end
def execute
payment = Stripe::PaymentIntent.create(
{
amount: @order.total_cents,
currency: "usd",
customer: @order.customer.stripe_id,
#...
},
{ idempotency_key: idempotency_key }
)
{ order_id: @order.id, payment_intent_id: payment.id } # saved and passed to MarkOrderPaid step
end
end
The return value of execute is how the PaymentIntent ID gets to MarkOrderPaid. Ductwork saves it in the same transaction that marks the step done and hands it to the next step, so you never have to go back to Stripe for it.
Let's walk the same two crash points through the new version with Ductwork.
Crash point 1: after the charge
I want to be honest here: Ductwork does not promise exactly-once execution. Nothing can when the side effect is an external API. Steps run at least once, so if the process dies after Stripe charges the card but before ChargeCard finishes, the step runs again.
What keeps that from double charging is the idempotency key. ChargeCard runs again, sends Stripe the same key it sent the first time, and Stripe hands back the original PaymentIntent instead of charging the card again.
Every Ductwork step has an idempotency_key that stays the same across retries and crash re-runs, and for most steps that's the one to use. For the charge I key off the order instead. A double-click triggers two pipelines, each with its own steps and its own keys, so a step key would let both of them charge. An order key means the second pipeline just gets the first PaymentIntent back.
None of this needs Ductwork. The same key works just as well in a plain job, and if you're calling Stripe from jobs today you should add one. What Ductwork changes is how much a re-run repeats. The step only does one thing, so a crash repeats the charge, not the charge plus everything after it.
Webhooks are messier. You can send the key in a header so the receiver can dedupe, but that only works if they actually check it, and you don't control the receiver. If they don't, a crash in NotifyFulfillment means the warehouse gets the same order.paid event twice. So does a double-click, since the second pipeline sends its own. Still a lot better than charging someone twice.
Crash point 2: after marking the order paid
This is where the plain job lost the order. The retry hit the paid? guard, returned early, and the enqueue never happened.
In the pipeline, if the process dies after update! but before MarkOrderPaid finishes, Ductwork runs the step again. It sets status: :paid on an order that's already paid, which doesn't hurt anything, and finishes.
The handoff can't get lost either. This is a Durable Seam doing its job. Ductwork creates NotifyFulfillment in the same transaction that marks MarkOrderPaid done, so there's no window between them for a crash to fall into. And only one process can advance a step, so the chain can't fork into two either. The warehouse hears about the order.
And notice there's no paid? guard in any of the steps. The guard was trying to answer "has this already been done?" for three side effects at once, and one check can't tell "done" from "half done." Ductwork answers that question per step. It records when each step finishes, so a re-run starts at the step that didn't, and the steps that did never run again.
Ductwork didn't make the crash go away. It turned lost work into double work, and double work is the one you can actually make safe.
"Can't I just fix the job?"
Yeah, kinda. Add an idempotency key to the Stripe call, delete the paid? guard, and walk both crash points again. The retry sends Stripe the same key, gets the same PaymentIntent back, marks the order paid, and enqueues the notification. Both crash points covered.
For three side effects in a row, a careful plain job gets you most of the way there. Here's where it stops.
A plain job has no seams, so every retry re-runs the whole job. Not the line that failed, all of them. Say a bad deploy makes update! raise for two days. Every retry calls Stripe again. Stripe only promises to remember an idempotency key for 24 hours, and Sidekiq's default retries keep going for about three weeks. Once the key is gone, the next retry is a brand new charge.
In the pipeline, ChargeCard finished two days ago. MarkOrderPaid can fail as many times as it wants, Stripe never hears from you again.
Build vs. Buy
So why not set an LLM loose on this problem? To do it correctly you need claiming, fencing, heartbeats, reaping dead resources, etc. And even if you one-shot vibe-coded your way into it, are you going to spend the time to properly crash test every possible boundary?
What Rails 8.1 ships with, ActiveJob::Continuable, and what exists in the ecosystem, like acidic_jobs, both run a workflow as steps inside one job. The moment you need to hand off to another job you're back in the Atomicity Gap. (acidic_jobs 0.9 had awaits for this, but the current 1.0 rewrite dropped it.) Neither one brings a job back after a hard crash either, so that's still up to your queue. A continuation saves its progress when it's interrupted or raises, but a SIGKILL right after the Stripe call doesn't give it the chance. The job starts over at the charge, if it comes back at all.
Temporal and Camunda solve all of this, but they're a separate cluster to run (or pay for), plus a new programming model the whole team has to learn. DBOS and Oban solve it well too, just not in Ruby.
Find Your Gaps
So where do the gaps live in your codebase? I wrote a prompt that you can hand to your coding agent. It reads your jobs and reports every Atomicity Gap it finds, labeled as double work or lost work, with the file and lines it found it on. It's read-only, makes no network calls, and installs nothing.