Reliable HTTP cron and delayed-job scheduling with automatic retries and full run logs — delay.run
Every background job eventually has to change something: mark invoices as overdue, expire trials, recalculate a leaderboard, send reminders. There are two ways to do it.
Option A: the job connects to the database and runs the queries itself, using the same models or a few lines of SQL.
Option B: the job calls your own application over HTTP (POST /internal/invoices/mark-overdue), and the application does the work, through the same code paths as any other request.
Both are common. Teams switch from one to the other after an incident, and then sometimes switch back after a different incident. Let's argue about it.
The case for going straight to the database
It's simple and fast. No HTTP, no serialization, no auth tokens, no timeouts. A job that updates 50,000 rows does it in one statement, in a transaction, in seconds.
Fewer moving parts. If the web app is down or slow, the job still works. Your nightly cleanup doesn't depend on the load balancer, the API gateway and the web workers all being healthy at 3 a.m.
Bulk operations fit SQL. "Mark every invoice older than 30 days as overdue" is one UPDATE. Doing it through an API that handles one invoice per request is 50,000 requests and a lot of overhead.
It's how most frameworks do it. Rails jobs, Django management commands, Laravel scheduled commands all load the app's models directly. It's the well-trodden path.
The case for calling your own API
Business rules live in one place. The API enforces validation, permissions, state transitions, audit logs, webhooks to other systems, cache invalidation and emails. A job that writes to the database directly bypasses all of that unless someone remembers to reimplement it. "Why didn't the customer get a cancellation email?" "Because the expiry job sets status = 'cancelled' in SQL."
Schema changes don't break jobs. When the subscriptions table is split in two, every job that queries it directly breaks. An API endpoint keeps the same contract while the storage underneath changes.
Jobs become ordinary, observable requests. They show up in the same logs, traces, metrics and error tracking as user traffic. Debugging a job means looking at a request, which every engineer already knows how to do.
It decouples where jobs run. A job that only needs HTTP can run anywhere: a different server, a serverless function, an external scheduler. A job that needs database credentials has to run inside your network with access to production data, which is a security trade-off too.
Where I've landed
I don't think one answer covers all jobs, but I've ended up with a rule of thumb I'd defend:
- If the job changes business state, go through the application's domain logic. Expiring a trial, cancelling a subscription, charging a card, sending a notification: anything that a user action could also do should use the same code path as the user action. In a monolith, that can mean calling the same service function directly. Across a process boundary, it means calling an internal endpoint.
- If the job is maintenance on data, go straight to the database. Deleting old sessions, vacuuming, rebuilding a search index, archiving logs. These have no business rules, and doing them through an API adds nothing.
- For bulk business operations, the API should accept batches. Not one request per invoice. An internal endpoint like
POST /internal/invoices/mark-overdue?before=2026-10-17that does the bulk update inside the application, with all side effects, is the best of both. - Internal endpoints need real auth and idempotency. Never "internal, so no auth". A signed request or a scoped token, and an idempotency key or a natural key, so a retried call is harmless.
- Set timeouts deliberately. A job that calls an endpoint which runs for 10 minutes will hit every proxy timeout on the way. Either make the endpoint start the work and return immediately, or keep the work small per call.
Once a job is just an HTTP call to your own endpoint, the schedule can live anywhere. delay.run calls that endpoint on time, retries it if your app is mid-deploy or returns an error, and keeps a log of each attempt.
The parts I'm still unsure about
- Performance-critical batch jobs. Some reports and aggregations are just too heavy for the application tier. Do you build a separate "batch API" with relaxed timeouts, or accept that these jobs touch the database directly and duplicate a bit of logic?
- Where the line is in a monolith. If the job runs in the same codebase, "calling the service function" and "calling the API" are nearly the same thing. Is HTTP worth it there just for observability and decoupling?
- Failure modes during incidents. When the web tier is overloaded, jobs that go through it add load at the worst time. Jobs that go straight to the database keep working, which is good for cleanups and bad for "send 50,000 emails while the site is down".
A quick test for your jobs
List your scheduled jobs and, for each, ask: if a user did this action through the UI, would anything else happen (an email, a webhook, an audit log entry, a cache flush) that the job doesn't do? Every "yes" is a job that bypasses business logic, and a bug report waiting to happen.
Related reading
- Moving cron jobs to a new server? A 12-point checklist so nothing runs twice, and nothing stops running
- Should background workers ship with the web app or deploy separately? Let's argue about it
- Our nightly cleanup job deleted the wrong things. Here's how I'd build it today
- What I'd do differently designing background job payloads (now that some of them sit in a queue for months)
- 6 myths about calling rate-limited APIs from background jobs
How do your jobs work today: straight to the database, through the API, or a mix? And has the choice ever caused a surprising bug?