Skip to the content.

Planning

Planning decides which jobs exist. It is the part of a sync system that looks easiest and goes wrong most quietly, so it has one rule and this page explains where the rule comes from.

The rule

Planning compares the jobs that should exist with the jobs that exist, and adds only what is missing. It can be called at any time, any number of times.

Nothing in the planner remembers that it ran. There is no “already planned” flag, no early exit, no last-run timestamp. The tables are the only memory: the jobs, and coverage, a summary of what has been planned per owner and type. The statement that inserts jobs also writes the summary, so the two never disagree, and the planner reads one row per owner and type instead of every job ever created. Old jobs can then be deleted without changing what is planned.

Two questions, two owners

Most planning bugs come from mixing these up:

Question Who answers it What happens
A job that should exist has no row The planner The row is inserted.
A row exists but the job stopped (failed, deferred, abandoned) Retry and requeue The row goes back to pending.

This is why coverage does not look at status. A slice of history that was abandoned is still “planned”. Bringing it back is the job of a requeue, which keeps its cursor. If the planner counted only healthy rows, it would insert a second row for the same slice while the first one is waiting for its retry, and the two would then fetch the same range.

One exception: a recurring job that was abandoned is replaced by the next one. When the next period comes, the planner deletes the abandoned recurring jobs of that owner and type in the same statement that inserts the new one, and the new window starts where the oldest of them started. An account whose credentials were revoked would otherwise add an abandoned row every period, and the windows they missed would stay missed after the credentials are fixed. The newest job always carries the current error. A requeue that comes first still wins: only rows that are still abandoned are deleted.

What should exist

You declare each job type as one of two kinds.

Snapshot: a type that reads everything on each run, like a product list.

Windowed: a type that reads a range of time, like orders.

today    = midnight UTC of the current day
covered  = the oldest window_start among the owner's initial jobs, or today if there are none
horizon  = today - Horizon

for each slice from covered back to horizon:      # stops exactly at the horizon
    insert initial job (slice.start, slice.end)

if no recurring job of this type is outstanding and none was created in the current period:
    from  = the newest window_end among all the owner's jobs of this type
    start = from - Overlap, or the oldest start of abandoned recurring jobs if that is earlier
    insert recurring job (start, now), deleting those abandoned recurring jobs

covered, from and the creation times come from the summary; whether a job is outstanding or abandoned comes from the open rows, which clean-up never deletes.

The inserts go through INSERT ... ON CONFLICT DO NOTHING on the unique index (queue, owner, type, dedup_key). The key is initial for a snapshot, initial:<window start> for a slice of history and recurring:<period start> for a recurring job. So even if two instances compute the same plan at the same moment, each job is created once.

What that gives you

These are the numbers from PlanningTests, which runs the planner against PostgreSQL with a clock it controls:

Situation Jobs created
New owner, 60 days of history in 30-day slices 2
Planned again the same day 0
Planned again five hours later 0
Horizon raised from 60 to 730 days 23, only the gap
Planned again after raising 0
Planned the next day 0
In total 25 slices, 730 days, no hole and no overlap

And for what is new:

Three mistakes this design exists to prevent

All three were made in the system this design comes from. Each one looked reasonable when it was written.

1. The “already planned” latch

if (await jobs.AnyAsync(job => job.Owner == owner && job.Mode == Initial))
    return;

One initial row blocked all further planning for that owner, forever. It was harmless until the history horizon was raised from 60 days to two years: new accounts got two years, and every account that was already connected silently stayed at 60 days. Nothing failed. Reports that needed last year’s numbers were simply empty for the older accounts.

The planner has no early exit now. If you find yourself adding one, the generator underneath is probably not repeatable, which is the next mistake.

2. Slices anchored to the time of the call

foreach (var slice in Slices(end: DateTime.UtcNow, days: 60))

Slices that start from “now” are different every time: called an hour later, every window_start has moved by an hour. Duplicates are recognised by window_start, so planning twice produced a second, overlapping set of slices. The latch above was there to hide exactly this.

The two were locked together: the latch existed because the generator was not repeatable. The order of the fix matters. First anchor the slices to a fixed point, then remove the latch. In SyncLane slices are anchored to midnight UTC, and the generator takes an explicit end point: it continues from the edge of what is covered and stops exactly at the horizon.

3. Deduplicating recurring jobs by their window

A recurring job also carries a window, so it is tempting to recognise a duplicate the same way as for history: same type, same window_start.

In that system the start of the recurring window was derived from the data: the latest “updated at” already stored, minus a few minutes of lag. Now consider a day on which a job brings nothing new. The latest “updated at” does not move. The next day the same window start is computed, it is found to be a duplicate, and nothing is planned. From that day on the account never gets a recurring job again, because the only thing that could move the data forward is the job that is no longer being planned. The lag made it worse: the last run had re-read a few boundary records, so its processed count was not zero and the account looked alive.

Six accounts stopped this way, one after the other, over several weeks, before anyone connected the dots.

So the two modes are deduplicated by different things, on purpose:

And the window of a recurring job is derived from the jobs table (where coverage ends), not from the data, so it moves forward even when a run finds nothing.

Before you change something