// DaveBuilds

Order Confirmation Jobs

A background job system where Postgres is the queue, checked with screenshots of every failure path.

AI-assistedRuns locally, not deployed
GitHub
Terminal showing the same order posted twice and the same job id returned
Idempotency check: a repeated orderId returns the same job

Problem

This was a task from the Product Engineering Bootcamp: build a background job system for one kind of work, sending an order confirmation email. Next.js API routes enqueue jobs and report status. A separate worker script claims and processes them. There's no queue service and no deployment. Everything runs and is verified on localhost, and the emails go through Mailtrap's sandbox, so nothing reaches a real inbox.

My role

I designed the job flow and directed an AI agent to build it. I wrote AGENTS.md, which set the evidence I'd require, such as a concurrency log (using Claude Code inside Antigravity). I reviewed each failure path against screenshots instead of taking "it works" on trust.

Process

The stack is Next.js, TypeScript, Prisma, PostgreSQL on Neon, and Nodemailer over SMTP.

POST /api/orders writes a pending job and returns 202. A repeated orderId returns the existing job instead of creating a second one. GET /api/jobs/:id returns status, attempts and last error. /dead-letters lists every dead job with a Retry button.

The worker runs three claim-and-process lanes in parallel. Each claim is a single atomic UPDATE ... FOR UPDATE SKIP LOCKED statement. A failed send is rescheduled with exponential backoff (a 2-second base doubled per attempt) plus up to a second of random jitter, then marked dead after the maximum attempts. A sweep runs on startup and every 30 seconds. It resets any job stuck in processing for more than 60 seconds back to pending.

To test failures on demand, I added a FORCE_SEND_FAILURE switch that makes every send fail without contacting Mailtrap. It's testing-only.

Key decisions

  • Atomic claim, not read-then-write. Selecting a job and then updating it leaves a gap where two lanes can claim the same row and send the same email. One locking statement removes the gap, and other lanes skip locked rows instead of waiting.
  • failed and pending are separate statuses. The claim query treats them the same, but a fresh job and a job on its third failure tell different stories, and the table should show that without cross-checking columns.
  • The guard checks sentAt, not whether a job row exists. The row records bookkeeping. sentAt is set only after the send succeeds, so it's the one field that answers whether the email actually went out.

Result

I kept screenshots of each path: every status in one table, backoff delays growing until a job goes dead, the dead-letter page, a job stuck after I killed the worker and then recovered, the same orderId posted twice returning the same job, and the worker holding at 3 of 3 jobs in flight, including real Mailtrap rate-limit failures mid-batch. These are screenshots from my local runs, not a fresh test run.

Terminal log of one job failing five times, with each retry scheduled further out, until it is marked dead
A single job failing repeatedly, with each retry's scheduled delay growing before it is marked dead
Terminal log of the worker starting, sweeping stuck jobs back to pending, then claiming them and marking one succeeded
The startup sweep resetting a stuck job to pending, and the worker reclaiming and completing it

What I'd do differently

The email is sent before sentAt is written. If the worker dies in that gap, the job is swept back to pending and the email can go out twice. The sentAt guard stops duplicates after it's recorded, but it can't close that window. So delivery is at-least-once, not exactly-once. With more time, I'd look for an email provider whose API supports idempotency keys and use the orderId as the key, so a retried send isn't delivered twice.

A 50-job burst test and a two-worker race screenshot are still pending. I'd also deploy it and swap the sandbox for a real provider, which would show how it behaves outside localhost.

Back to DaveBuilds