Think about your scariest background job. What would happen if it crashed between two of its lines? Would an order ship twice? Would a customer never get charged? 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 ShipOrderJob < ApplicationJob
def perform(order_id)
order = Order.find(order_id)
return if order.shipped? # don't ship twice
shipment = Warehouse.create_shipment(order)
# ๐ฅ crash point 1: the retry ships a second package
order.update!(status: :shipped, shipment_id: shipment.id)
# ๐ฅ crash point 2: the retry sees shipped?, returns early, and the customer never gets charged
ChargeOrderJob.perform_later(order.id) # we charge when the order ships
end
end
The Problem
There are two 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 warehouse call, and you're in the window. Those are way more common than OOM kills.
And no, an idempotency key won't always save you. That only works when the API on the other end accepts one, and you don't control the warehouse.
The result is double work: duplicate shipments, duplicate charges, duplicate emails. This is illustrated by crash point 1 in ShipOrderJob.
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.
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.
Fail inside this window and the only thing that gets the next job enqueued is a retry that makes it all the way back to that line. Plenty of things can stop it. The retries run out. The job gets dropped entirely (Sidekiq's free version loses in-progress jobs on a SIGKILL). Or your own code gets in the way, which is what happens in ShipOrderJob.
Look at the return if order.shipped? guard clause. That's the guard you'd add so a job that runs twice doesn't ship twice. At crash point 2 the order is already marked shipped, so the retry hits the guard, returns early, and never reaches the enqueue. Your chain silently stops.
This time the result is lost work: orders that ship but never get charged, receipts that never send.
The guard is a trade
So do you just delete the guard? Then the retry at crash point 2 does make it back to the enqueue, and it ships a second package on the way there. With the guard you lose the charge. Without it you ship twice.
And at crash point 1 the guard doesn't matter either way. The order isn't marked shipped yet, so it never fires and the retry ships again anyway.
What felt like an obvious patch only trades double work for lost work.
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: the warehouse, 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:
- Shrink each unit of work down to one side effect. That's a step. A crash can still make a step run twice, but now it only repeats that one thing.
- Make the seams between steps durable, so a finished step never runs again and the handoff to the next one can't get dropped.
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. Ductwork only requires a single database (PostgreSQL, MySQL, or SQLite).
Rewriting the scariest job
Let's check out ShipOrderJob 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 ShipOrderWorkflow < Ductwork::Workflow
define do |workflow|
workflow.start(CreateShipment)
.chain(to: MarkOrderShipped)
.chain(to: ChargeOrder)
end
end
Kicking it off is a one-line change. ShipOrderJob.perform_later(order.id) becomes ShipOrderWorkflow.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 CreateShipment step:
class CreateShipment < Ductwork::Step
def initialize(order_id)
@order = Order.find(order_id)
end
def execute
shipment = Warehouse.create_shipment(@order)
{ order_id: @order.id, shipment_id: shipment.id } # saved and passed to MarkOrderShipped step
end
end
MarkOrderShipped picks up from there. The return value is saved as JSON, so the hash arrives with string keys:
class MarkOrderShipped < Ductwork::Step
def initialize(payload)
@order = Order.find(payload["order_id"])
@shipment_id = payload["shipment_id"]
end
def execute
@order.update!(status: :shipped, shipment_id: @shipment_id)
@order.id # saved and passed to ChargeOrder step
end
end
Let's walk the same two crash points through the new version with Ductwork.
Crash point 1: after the shipment
I want to be honest here: Ductwork does not promise exactly-once execution. Nothing can when the side effect is an external API. It's the Two Generals problem, proven impossible back in 1975. If the process dies right after the warehouse creates the shipment, it comes back with no record of whether the shipment went through, so on its own it can only retry and risk a duplicate, or give up and risk losing it. Ductwork retries: steps run at least once.
So what's different? How often you end up there. In the plain job, any failure after the warehouse call sent the retry back through it. A validation error on update!, a database blip, a failed enqueue. Every one of them ships another package, on every retry.
In the workflow, once CreateShipment finishes Ductwork records it and never runs it again. MarkOrderShipped can fail as many times as it wants, the warehouse never hears from you again. The only way left to ship twice is a failure inside CreateShipment itself, after the warehouse answers and before the step is recorded. That window used to be the rest of the job. Now it's one line.
If your warehouse does take an idempotency key, send one and that last window closes too. Every Ductwork step has an idempotency_key that stays the same across retries and crash re-runs. ChargeOrder uses it further down.
But look at what a key buys you. It makes double work safe. It does nothing for lost work. A key can't do anything about an enqueue that never happens, and that's crash point 2.
Crash point 2: after marking the order shipped
This is where the plain job lost the charge. The retry hit the shipped? guard, returned early, and the enqueue never happened.
In the workflow, if the process dies after update! but before MarkOrderShipped finishes, Ductwork runs the step again. It sets status: :shipped on an order that's already shipped, which doesn't hurt anything, and finishes.
The handoff can't get lost either. This is a Durable Seam doing its job. Ductwork creates ChargeOrder in the same transaction that marks MarkOrderShipped 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 customer gets charged.
And notice there's no shipped? 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.
The last step: charging once
So the customer gets charged. But only once? ChargeOrder is a step like the other two, which means it runs at least once, and a double charge is a lot worse than a second box. This is the step where you want that idempotency key:
class ChargeOrder < Ductwork::Step
def initialize(order_id)
@order = Order.find(order_id)
end
def execute
charge = PaymentGateway.charge(@order, idempotency_key: idempotency_key)
charge.id
end
end
If the step dies after the charge goes through, the re-run sends the same key and the payment gateway hands back the original charge instead of making a new one.
Ductwork didn't make the crash go away. It made the opposite trade from the guard. Lost work becomes double work, and double work is the one you can actually make safe.
What about Continuable?
Rails 8.1's ActiveJob::Continuable and gems like acidic_job split a job into steps too. For a plain exception they do the right thing, and the retry picks up at the step that failed.
The difference is where the progress lives. Continuable keeps it in the job's own payload and only writes it back when the job re-enqueues itself. A SIGKILL never gives it that chance. Whether the job comes back at all is up to your queue, and if it does, it starts over from the first step. acidic_job does record each step in your database, but there's still a plain job underneath, and bringing that job back after a SIGKILL is still up to your queue.
Steps in both also run one after another inside a single job. Ductwork's seams don't have to be one-to-one. divide and expand fan a workflow out, combine and collapse join it back up, and each of those is the same Durable Seam you saw between MarkOrderShipped and ChargeOrder.
Temporal does solve all of this. It's also a separate cluster to run and a new programming model for the whole team. Ductwork is a gem and some tables in the database you already have.
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.