02 / Admin and operations API
One enrollment, one order, no matter how many jobs run
Two background jobs could provision the same course purchase at the same time and create duplicate orders on the learning platform. I made each job claim its record in a single database statement, so only one can ever act on it.
- Problem
- A check and a write ran as separate steps, so overlapping retries could both pass the check and both create an order.
- Fix
- One conditional update claims the enrollment. If it touches zero rows, another job owns it and this one stops.
- Result
- Caught before release. Three integration tests prove one order per enrollment, even when jobs overlap or crash.
Context
The platform sells continuing education courses to veterinary teams. When someone buys a course, a background job gives them access on the external learning platform by creating an order there.
When that fails, staff can retry from the admin panel, one record at a time or up to 100 at once. A scheduled sweep also picks up anything that got stuck. I was building the admin API in a platform rebuild alongside an external agency, and the bulk retry was mine.
The problem
Before queuing a job, the bulk retry checked that the enrollment wasn't already done or in progress. But the check and the queueing were two separate steps.
If two retries overlapped, or a retry ran at the same moment as the sweep, both could pass the check. Both jobs would then call the learning platform, and the learner would end up with two orders for one purchase.
The fix
I moved the guard into the job itself and made it atomic. Instead of reading the status and then writing it, the job claims the enrollment with one conditional update. It only succeeds if the enrollment isn't completed and isn't already held by another job.
UPDATE enrollments
SET status = 'in_progress', updated_at = now()
WHERE id = @id
AND status <> 'completed'
AND (status <> 'in_progress'
OR updated_at < now() - interval '15 minutes');
-- 1 row: this job owns it. 0 rows: another job does, so stop.
A simplified version of the idea, not the production code.
If the update touches zero rows, the job logs the skip and exits without calling the learning platform. Two details mattered:
- Crashed workers. If a job dies after claiming a record, the claim would otherwise last forever. A claim older than 15 minutes can be taken over, which matches the window the sweep already uses.
- The ORM's own concurrency check. A bulk update skips Entity Framework's change tracking, so the job's in memory copy would look out of date and its next save would fail with a false conflict. Reloading the record right after the claim prevents that.
How I proved it
I changed the test stand in for the learning platform so it records every order it receives, then wrote three integration tests:
- 1A job does nothing while another job holds a fresh claim.
- 2A claim left behind by a crashed worker is taken over, and exactly one order is created.
- 3A failed enrollment queued twice still produces exactly one order.
Outcome
- Duplicate orders from overlapping retries can no longer happen, and it was caught before release.
- In a bulk retry, records that are missing, already done or in progress are skipped with a reason instead of failing the whole batch.
Another one caught before release
A nightly cleanup job for unverified accounts could have deleted members who had already paid. It now skips anyone with orders, enrollments or carts, and removes each account locally before tearing down its login, so one blocked delete can't break the whole run.
Any check that runs separately from the write it protects will eventually lose a race. The safe place for that rule is the write itself.