Short answer: use a small cron trigger to find expired reservations, then hand bounded batches to a queue only when the cleanup can run longer than the trigger window or needs independent retries. For a developer tool holding a reservation for 15 minutes, the cheapest reliable design is usually one Postgres transaction plus an idempotent worker. A queue is a delivery mechanism, not a substitute for a database rule.
The failure timeline of a 15-minute hold
The job is easy to describe: release reservations whose hold_until is in the past. The dangerous part is delivery. A scheduler can start twice, a process can die after updating one row, and a queue can redeliver a message after a timeout. “Exactly once” is not a realistic promise across these boundaries. I design for at-least-once execution and make every state transition safe to repeat.
The 15-minute window gives us a useful operational bound. Being a few seconds late is acceptable; silently leaving a reservation locked for an hour is not. That points to a frequent, short trigger and a database query that owns the truth. The trigger should wake work up, not carry the reservation state around in a payload.
There are three separate guarantees to write down before choosing cron or a message queue:
- Eligibility: only rows still in
heldstate and pasthold_untilmay be released. - Idempotency: a retry must produce the same final state, with no second refund, email, or inventory decrement.
- Visibility: an operator can see the age of the oldest eligible row and the number of failed attempts.
That list is more useful than a price comparison. A cron tick that runs every minute but cannot tell you which rows it skipped is cheap only on paper.
Smallest working implementation
The cleanup claim and state change belong in one transaction. Claim a bounded batch with row locks, commit quickly, and let the worker perform any slow side effect after the claim. The partial index keeps the hot query focused on rows that can expire.
import { Pool } from "pg"; const pool = new Pool({ connectionString: process.env.DATABASE_URL }); const BATCH_SIZE = 200; export async function expireReservations(now = new Date()): Promise<number> { const client = await pool.connect(); try { await client.query("BEGIN"); const result = await client.query( `UPDATE reservations SET state = 'expired', expired_at = $1, version = version + 1 WHERE id IN ( SELECT id FROM reservations WHERE state = 'held' AND hold_until <= $1 ORDER BY hold_until, id FOR UPDATE SKIP LOCKED LIMIT $2 ) RETURNING id`, [now, BATCH_SIZE], ); await client.query("COMMIT"); return result.rowCount ?? 0; } catch (error) { await client.query("ROLLBACK"); throw error; } finally { client.release(); } } // Run this function from a one-minute scheduler. It is safe to invoke twice. expireReservations().catch((error) => { console.error("reservation expiry failed", error); process.exitCode = 1; }); The matching index is intentionally boring:
await pool.query( "CREATE INDEX CONCURRENTLY IF NOT EXISTS reservations_hold_expiry ON reservations (hold_until, id) WHERE state = 'held'", ); SKIP LOCKED prevents two overlapping ticks from waiting on the same rows. It does not remove the need for idempotency. If a later step sends a webhook, store an event key such as reservation-expired:{id}:{version} and enforce uniqueness before sending. A worker may receive that event twice; the recipient should still observe one effect.
How do Node.js Postgres cleanup jobs measure cron queue delivery?
A queue becomes useful when one tick can discover more work than a single process should handle, or when each item needs a separate retry and visibility timeout. Keep the scheduler responsible for discovering IDs, publish one message per bounded batch, and have consumers re-check the row state before doing side effects. A message that has exceeded its visibility timeout can be delivered again, so the consumer must acknowledge only after the transaction and side effect policy are complete. AWS documents this timeout-and-redelivery model for SQS-style queues, and the same reasoning applies to other at-least-once systems.
Rate limits add another failure mode. If a downstream API answers with HTTP 429, honor its Retry-After guidance when present and delay the message; do not spin in a tight retry loop. MDN describes 429 as a rate-limit response, not proof that the reservation itself is invalid.
The catch is operational weight. A queue adds credentials, monitoring, dead-letter handling, and a second place to reason about ordering. It is not suitable when the cleanup is a short SQL update, the batch is naturally bounded, and a minute of lateness is fine. Stick with the scheduler-plus-transaction path in that case.
At millions of reservations, partition by time or tenant and expose two metrics: oldest hold_until still in held, and expiry attempts that reached the retry limit. Alert on age, not only on process exit. A green scheduler with a blocked database connection is still a failed cleanup.
Recovery deserves its own rehearsal. Stop a worker after it has claimed 200 rows, then start it again with the same batch key. The second pass should find zero held rows from the committed transaction and should safely ignore any event keys already recorded. If the transaction rolled back, those rows remain eligible and the next tick claims them. This is the boring behavior I want: no manual SQL, no guessing which half of a batch ran.
I would also separate “release the reservation” from “notify another system.” The first is a local, transactional state change. The second is an integration workflow and belongs behind an outbox or queue, with a clear replay policy. That split keeps a transient downstream outage from extending a customer’s hold indefinitely.
There is no universal cheapest option. Measure the unit that matters: database work per expired row, queue operations per batch, and operator minutes per incident. Your mileage may vary with tenancy, burst size, and the cost of a late release; write those assumptions beside the schedule so the next engineer can challenge them.

