“use workflow” vs the job queue we built by hand
HyperSync runs long Shopify copies on a Postgres queue we wrote ourselves. We went through Vercel’s Workflow SDK piece by piece: what “use workflow” would replace, and what it wouldn’t.
HyperSync, our Shopify app, copies store data between stores. A big copy runs for a long time, touches thousands of resources, and has to survive crashes, deploys and Shopify telling it to slow down. We covered the data side in an earlier post. This one is about the machinery that keeps a long run alive, which we built ourselves on Postgres.
Vercel’s Workflow SDK promises most of that machinery through two directives: "use workflow" and "use step". We haven’t moved HyperSync onto it. We went through it piece by piece to see what it would replace, what it wouldn’t, and whether we would use it for the next system like this.
What we built
A run is split into units, rows in a Postgres table. Each unit is a batch of up to 25 resources of one kind, in dependency order. Separate worker processes take units with a single UPDATE … WHERE id = (SELECT … FOR UPDATE SKIP LOCKED), so two workers never get the same one. Around that sits everything a durable queue needs:
- Leases. A claimed unit is held for 60 seconds and renewed by a heartbeat every 20. A reaper returns expired leases to the queue every 30 seconds, and a unit that kills its worker five times is set aside as poison.
- Checkpoints. After every resource, the unit saves where it is and what it has reported so far. A retry continues from there.
- Retries. Shopify’s throttling is retried with exponential backoff and full jitter, capped at a minute, five attempts in all.
- Fairness. Two lanes per worker, and at most four units in flight per store, so one large shop can’t starve the others.
- Pause and cancel. Both are requests on the run that workers check between resources. A paused unit goes back to the queue with its checkpoint.
- Dead letters and replay. A unit that fails for good is marked dead. An operator script replays dead units, dry-run by default.
- Deploy safety. Workers only claim unit types they have handlers for, so a rolling deploy doesn’t hand new work to old code.
None of this is exotic, and all of it took real bugs to get right. Pause once marked units as done and dropped their checkpoint. A throttled response was once treated as a permanent failure. A cancel that landed during planning once left a run stuck forever.
What “use workflow” is
A workflow is an async function marked "use workflow" that orchestrates. A step is an async function marked "use step" that does the work, with full Node.js and any npm package. You start a run with start() from workflow/api, which returns immediately with a run you can check on later. In a Next.js app, setup is wrapping the config in withWorkflow from workflow/next.
Here is roughly how a HyperSync copy would look. We wrote this against workflow 5.8 on Next.js 16 and it builds and type-checks. We haven’t run it against a store:
import { RetryableError, FatalError } from "workflow";
export async function copyStore(batches: string[][]) {
"use workflow";
const report = { written: 0, failed: [] as string[] };
for (const ids of batches) {
const result = await copyBatch(ids);
report.written += result.written;
report.failed.push(...result.failed);
}
return report;
}
async function copyBatch(ids: string[]) {
"use step";
// Read, remap and write up to 25 resources, recording each mapping.
// Shopify said slow down: throw new RetryableError("Throttled", { retryAfter: "30s" });
// Shopify said never: throw new FatalError("Input rejected");
return { written: ids.length, failed: [] as string[] };
}Durability comes from an event log. Every step’s result is recorded, and when a run resumes, the workflow function runs again from the top with the log supplying the results of steps that already finished, so they don’t run twice. A step that was in flight when something crashed is retried, which means a step can run more than once and its side effects need to be safe to repeat.
What it would replace
Matched against our list, a lot of the plumbing disappears:
- Leases, heartbeats, the reaper, `SKIP LOCKED`. Gone. Claiming work and recovering from a dead process is the runtime’s job.
- Checkpoints. A step’s result is its checkpoint. With one step per batch, a crash loses at most the batch in flight, the same as today.
- Retries. Steps retry by default, three times after the first attempt, configurable per step with
maxRetries. Retries are queued immediately unless you say otherwise, so throttling still needs aRetryableErrorwith aretryAfter.FatalErrorstops the retries, and the workflow can catch it and carry on with the next batch. - Deploy safety. A run stays on the deployment that started it; new code only affects new runs. That is stronger than our unit-type filter, and it means a fix doesn’t reach runs already in progress unless you restart them on the latest deployment.
- Waiting.
sleep()costs no compute while it waits, and hooks let a run wait for an outside event, such as a merchant confirming a plan, without polling. - Inspection.
npx workflow inspect runsand a web view show every run and step. We built our own run page and a health check on a timer.
Two things we have don’t map one to one. The docs describe cancelling a run, but not pausing one; a pause would be a hook the workflow waits on between batches. And there is no dead-letter state as such: a run that fails is a failed run you inspect and restart.
What it wouldn’t replace
The hard parts of HyperSync were never the queue.
- Idempotency against Shopify. A step can run twice, and so can ours today. Workflow gives each step a stable ID to use as an idempotency key, which helps with APIs that accept one. Shopify writes mostly don’t, so the mapping table and the natural keys (handles, SKUs) stay exactly as they are.
- Per-store fairness. We found no built-in cap on concurrent work per key. There is an experimental rate limit on how often runs start, which is a different thing. “At most four units in flight per store” would still be ours to build.
- Run size. A run has limits: by default 10,000 steps and 25,000 events, and a successful step writes three events. Our largest test copy had 5,498 resources. One step per resource would use about 16,500 events, two-thirds of the limit before a single retry. One step per batch of 25 uses under a thousand. Batching stops being a tuning choice and becomes a requirement.
- Determinism. Workflow code reruns on every resume, so it has to make the same decisions every time. The runtime fixes
Math.randomandDateper run to help, but anything that talks to the network belongs in a step. That is a discipline your team has to keep, not one the framework can enforce for you.
Where it would run
This is what decides it for HyperSync today. The app runs on a server in Frankfurt with its database in the same city, and EU data residency is part of how we sell it. On Vercel, Workflow runs on Vercel’s managed backend, and start() accepts a region. Off Vercel, the self-hosted option is a Postgres backend that needs a long-running process. Its own docs say it is “not optimized for scale, speed, or security” and ships with no authentication on its HTTP routes.
So on our own server, adopting it would swap a queue we know well for a younger one on the same Postgres, and we would still write the fairness and the Shopify logic ourselves. On Vercel, the trade is different: billing is per event, at $0.02 per thousand on demand. By our arithmetic, a copy the size of that test run, in batches of 25, comes to around 670 events, a little over one cent. The rest of the bill is ordinary function time.
Would we use it next time?
For a new system that runs on Vercel, yes. Leases, heartbeats, reapers and checkpoints are code nobody should have to write twice, and every one of them cost us at least one bug. We would still design around batches, keep side effects idempotent, and build our own fairness.
For HyperSync, not yet. Moving would mean moving hosting, and the queue isn’t what goes wrong. The bugs that matter happen where store data meets Shopify’s API, and "use workflow" doesn’t change that part.