DEV Community

delay.run
delay.run

Posted on Edited on

Case study (hypothetical): a unique constraint on the job queue stalled every nightly email for three days, and only the web app was monitored

Reliable HTTP cron and delayed-job scheduling with automatic retries and full run logs — delay.run

This is a hypothetical case study. The company and numbers are invented; the failure is a quiet one: a background queue that stops accepting work while the site in front of it looks fine.

The setup

A learning product sends a nightly "pick up where you left off" email to users who had activity that day and have not been emailed in 24 hours. The web app, on a user's last action of the evening, enqueues a job:

INSERT INTO jobs (name, user_id, run_after, status)
VALUES ('nightly_digest', $1, now() + interval '30 minutes', 'pending');
Enter fullscreen mode Exit fullscreen mode

There is a unique index, added a year earlier to prevent double-sends during a bug:

CREATE UNIQUE INDEX jobs_one_pending_digest
  ON jobs (user_id)
  WHERE name = 'nightly_digest' AND status = 'pending';
Enter fullscreen mode Exit fullscreen mode

A worker process, on a separate host, picks pending jobs whose run_after has passed, sends the email, and sets status = 'sent'. The web app has uptime checks on / and /health. The worker host has none, and nobody graphs jobs table depth.

The incident

Thursday, 22:14. A deploy of the worker dies mid-send. The process is restarted by systemd. The job it had been holding stays pending, because the status update never committed. That is usually fine: the worker will pick it up again. This particular job, though, had already been sent by a previous retry path in the mailer (the provider accepted it, the worker then crashed before the status update). Still pending.

Thursday night. For most users, new inserts work: they have no pending row. For the user whose row is stuck, the next enqueue hits the unique index and the web app logs a traceback, swallowed by the enqueue helper as "failed to schedule digest". One user misses an email. Nobody notices.

Friday. A second bug in the new worker build: under an exception, it no longer marks jobs as failed or sent. It leaves them pending and moves on, or, in some cases, exits the loop early. By Friday afternoon, a few thousand pending rows are sitting in the table, one per user who was enqueued and not completed. Those users cannot be enqueued again. As they represent a large share of the evening's "had activity" set, Friday's digest almost doesn't happen. A few emails that were already pending from earlier in the day do go out, slowly, when a process happens to finish one.

Saturday and Sunday. The stuck rows remain. Weekend activity still tries to enqueue, hits the unique index, and gives up. Almost no digests go out. The web app is fine. /health is 200. The marketing dashboard, looked at on Monday, shows a cliff in "emails sent" from Friday.

Monday, 10:20. An engineer investigating the cliff looks at SELECT status, count(*) FROM jobs WHERE name = 'nightly_digest' GROUP BY 1; and sees tens of thousands of pending rows, some days old, and a flood of unique-violation errors in the web app logs starting Thursday night.

Fixing the immediate damage

They stopped the broken worker, marked obviously-sent and obviously-stale pending rows by hand (anything pending and older than a day whose user already received a digest according to the mail provider), deployed a worker that always terminal-states a job, and drained the rest. They did not re-send blindly to everyone with a stuck row, because some had already received the email. About three nights of digests were partly or fully missed; they sent a single catch-up on Monday afternoon to users who were owed one and had not received it.

A queue that stops accepting work will not show up on a homepage check. delay.run runs the job outside your app process, retries a failed run, and keeps a log of every attempt, so a nightly email that stopped leaving the queue is visible as a missed or failed run, not only as a quieter campaign chart on Monday.

What actually went wrong

The unique index turned a stuck row into a site-wide enqueue failure. "One pending digest per user" is a reasonable invariant, but only if pending rows cannot get stuck. Combined with a worker that left them pending, it became a lockout.

Only the web app was monitored. The worker's host, the depth of pending jobs, and the unique-violation rate were all silent. The product looked up because the product, as defined by the monitors, was the HTTP site.

Enqueue errors were swallowed. The web app treated "could not schedule digest" as non-fatal, which it is for a single request, and which it is not when it happens for every user on a Friday.

There was no 'already sent' ledger independent of the queue. The queue row was both the schedule and the record of the send. A crash between the two left the system in a state it could not describe.

The fixes

1. A send ledger, unique on (user_id, local_date), separate from the jobs table. The job inserts into the ledger when the mail provider accepts the message, and the queue row is just a pointer. Re-running is a no-op if the ledger says sent.

2. Terminal states are mandatory. A worker may leave a job pending only while it still holds a lock on that row. Exceptions set failed with the error, and a retry is a new attempt (or a new row), not an immortal pending.

3. Alerts on queue age and enqueue failures. Page if any pending digest is older than two hours, or if unique-violations (or enqueue error rate) exceed a few per minute. Graph sent-email count against a baseline.

4. Monitor the worker the way you monitor the app. A health endpoint is not enough, but a missed-run check and a host uptime check would have been a start.

5. Keep the unique index, once stuck rows cannot survive. The invariant is good; it just needed the rest of the design.

What I'd take from it

A uniqueness rule on "pending" is only safe if "pending" is always short-lived. And a product that sends email has a second surface area, the worker, that a homepage monitor will never see. If the business event is "email left the building", that is what you should be looking at on Monday and on Friday night.

Related reading

Have you had a queue stall behind a unique constraint or a stuck 'pending' row? How did you notice — a customer, a graph, or a Monday-morning cliff?

Top comments (0)