Repository navigation
Description
Feature description
Add a new jobKeyMode that combines debounce with a maximum delay — i.e., each new call pushes run_at forward (like replace), but never beyond a configurable upper bound relative to when the job was first created.
Proposed API:
addJob(payload, {
jobKey: "my-key",
runAt: addSeconds(new Date(), 2), // debounce window
jobKeyMode: "debounce",
maxDebounceSeconds: 10, // upper bound since first creation
});Behavior:
| Event | run_at |
Notes |
|---|---|---|
| T+0s — first call | T+2s | Job created, created_at = T+0s |
| T+1s — second call | T+3s | run_at pushed forward (debounce), still ≤ created_at + 10s |
| T+3s — third call | T+5s | Debounce continues |
| T+9s — fourth call | T+10s | Would be T+11s, but clamped to T+10s |
| T+10s | — | Job executes. Next call starts a fresh cycle |
Implementation idea:
The existing _private_jobs table already has a created_at column that is set once when the job row is inserted and is not updated by subsequent addJob calls with the same jobKey. This is exactly the "first enqueued at" timestamp needed for clamping.
When jobKeyMode = "debounce": update run_at to the new value (like replace), but clamp it:
run_at = LEAST(new_run_at, created_at + (maxDebounceSeconds || ' seconds')::interval)No schema changes required — the feature can be implemented purely as logic in the add_job function.
Alternative API shapes (open to discussion):
// Option A: explicit new mode
jobKeyMode: "debounce",
maxDebounceSeconds: 10,
// Option B: extend preserve_run_at with a tolerance
jobKeyMode: "preserve_run_at",
maxRunAtDrift: 10,
// Option C: generic clamp on replace
jobKeyMode: "replace",
maxDelaySeconds: 10,No strong preference — whatever fits the library's design philosophy best.
Motivating example
Debounce-with-max-delay is an extremely common pattern in event-driven systems. A concrete case: recalculating derived data after user edits.
// Users vote for ideas in rapid succession — we want to debounce
// the expensive recalculation, but not starve it indefinitely.
addJob("rubrics.recalculateRating", { rubricId }, {
jobKey: `rubrics.recalculateIdeasRating:${rubricId}`,
runAt: addSeconds(new Date(), 2),
jobKeyMode: "preserve_run_at", // compromise: throttle, not debounce
});The current options force a choice between two extremes:
replace— true debounce, but if events arrive every second, the job is postponed forever.preserve_run_at— guaranteed execution at the original time, but ignores recency entirely (throttle, not debounce).
Neither captures the desired semantics: "wait for a pause in activity, but guarantee execution within N seconds."
Other use cases where this applies: webhook coalescing, search index rebuilding, cache invalidation batching, notification grouping, analytics aggregation after user activity bursts.
Breaking changes
None. The existing created_at column on _private_jobs already provides the necessary "first enqueued" anchor. The feature can be implemented as a new jobKeyMode value ("debounce") with an additive option (maxDebounceSeconds), requiring no schema migration and no changes to existing modes.
Supporting development
I [tick all that apply]:
- am interested in building this feature myself
- am interested in collaborating on building this feature
- am willing to help testing this feature before it's released
- am willing to write a test-driven test suite for this feature (before it exists)
- am a Graphile sponsor ❤️
- have an active support or consultancy contract with Graphile
Activity
Krrishkumar125 commentedon Oct 22, 2026
Hi @benjie, I'd like to work on this and wanted to agree the design before writing any code.
I read through
add_jobin the current schema. Here's what I'd propose:API: Option A from the issue, a new
jobKeyMode: "debounce"plus amaxDebounceSecondsoption (alternative name:maxDelay). It behaves exactly likereplaceexcept for howrun_atis computed on conflict:On insert it's just
run_at, like today, so the first call starts the window.Things I noticed / edge cases:
add_jobwould get a new parameter, so it still needs a migration that replaces the function (andadd_jobs, which currently takesjob_key_preserve_run_at boolean, would need an equivalent so batch adds stay consistent).created_atis never updated on conflict, which is what makes the clamp work. But if the existing row has already failed and is waiting to retry (attempts > 0), itscreated_atcan be very old. My suggestion is to only clamp whileattempts = 0, mirroringpreserve_run_at, and otherwise fall back toreplacebehaviour.replace. The key gets detached and a new job is inserted, which starts a fresh window. That matches "next call starts a fresh cycle" from the issue.debouncewithoutmaxDebounceSeconds: I'd make that an error (it's justreplace), rather than silently behaving likereplace.runAtis already past the clamp on the first call, should the job run atrunAt, or atcreated_at + max? I'd keep it simple and only clamp on conflict.Tests I'd add: the timeline from the issue (pushed forward, then clamped), the retrying-job case, the locked-job case, the missing-option error, and
add_jobsbatch parity.Questions for you:
replace(Option C)?add_job/add_jobsparameter, or is there a preferred way to pass extra job-key options?Happy to adjust before I start on a PR.