Skip to the content.

Operations

SyncLane has no admin tool because the table is one. This page collects the statements that answer the usual questions, and the settings that are easy to get wrong.

All examples use the default schema, synclane.

Looking

ISyncLaneMonitor answers the usual questions without SQL: the state of each queue, whose data is old and why, each owner’s jobs counted by status, failures grouped by error code, rate budgets, instances, and volume per hour or day. See Watching it run. The queries below are for everything else.

Finding an owner by name. Give JobOwner a name in your owner source, for example the customer and its store, and GetJobsAsync, GetFreshnessAsync, GetFreshnessOwnersAsync and GetJobOwnersAsync find owners by any part of it with Search, ignoring case. The key and the name live in synclane.owners, one row per queue and owner; a search reads that small table and never the jobs. How many jobs each owner found by name has given up on is GetJobOwnersAsync with Search and Status = JobStatus.Abandoned (GET /synclane/jobs/owners?queues=shop.orders&queues=shop.stock&search=acme&status=abandoned), counted across the queues you name. The same count in SQL, per queue:

SELECT o.queue, o.owner, o.name, count(*) FILTER (WHERE j.status = 'abandoned') AS given_up
FROM synclane.owners AS o
JOIN synclane.jobs AS j ON j.queue = o.queue AND j.owner = o.owner
WHERE o.name ILIKE '%acme%'
GROUP BY o.queue, o.owner, o.name;

What is running right now, and who has it:

SELECT id, owner, lane, type, cursor, processed_count, leased_by, heartbeat_at
FROM synclane.jobs
WHERE status = 'running'
ORDER BY started_at;

What is in trouble:

SELECT id, owner, type, status, attempt_count, next_run_at, error
FROM synclane.jobs
WHERE status IN ('failed', 'deferred', 'abandoned')
ORDER BY updated_at DESC;

How far each owner’s first load is:

SELECT owner,
       count(*)                                     AS jobs,
       count(*) FILTER (WHERE status = 'completed') AS done,
       count(*) FILTER (WHERE status = 'abandoned') AS given_up
FROM synclane.jobs
WHERE mode = 'initial'
GROUP BY owner
ORDER BY owner;

Which range of an owner’s orders is covered, and where the holes are:

SELECT window_start, window_end, mode, status, processed_count
FROM synclane.jobs
WHERE owner = 'globex' AND type = 'orders' AND window_start IS NOT NULL
ORDER BY window_start;

Why a lane is not moving. One of these rows is the answer: a job that is running, or one that is waiting for next_run_at:

SELECT id, type, status, attempt_count, next_run_at, lease_expires_at, leased_by, error
FROM synclane.jobs
WHERE lane = 'globex' AND status IN ('pending', 'running', 'failed', 'deferred')
ORDER BY priority, window_start DESC NULLS LAST;

Which rate budgets are paused, and until when. generation counts the pauses of a bucket; last_pause is the most recent one’s length:

SELECT budget, key, refilled_at AS paused_until, generation, throttled_at, last_pause
FROM synclane.rate_buckets
WHERE refilled_at > now()
ORDER BY refilled_at DESC;

What each bucket has learned. rate is the current rate in requests a second, never above the ceiling; rate_max is the rate that last caused a 429; reported_rate is the limit the API last reported, which is the ceiling in place of the declared limit when it is there. A rate far below the ceiling means the API, or another app on the account, allows less than it says:

SELECT budget, key, round(rate::numeric, 3) AS rate, round(rate_max::numeric, 3) AS rate_max,
       round(reported_rate::numeric, 3) AS reported_rate, throttled_at
FROM synclane.rate_buckets
ORDER BY budget, rate;

Changing

ISyncLaneMonitor has an emergency stop and the usual repairs, for any queue in the schema:

Call What it does
PauseAsync(queue, owner?, reason, by, until?) Stops a queue, or one owner on it, at once. Running jobs of it are cut on every instance and handed back without using an attempt; nothing of it is taken until ResumeAsync or until. Planning goes on, and the next window reaches back over the pause, so nothing is lost. The page that was cut runs again on resume, which is why handlers write idempotently.
ResumeAsync(queue, owner?, by) Lifts it. Idle workers wake at once.
LimitBudgetAsync(budget, key?, limit, per, reason, by, until?) Lowers a rate budget, or one key of it, for a while: “the API answers slowly, halve our rate for an hour”. Every instance obeys from its next request. It only ever lowers: the declared limit, or the one the API reports, stays the upper bound. When it ends, the rate climbs back as it learns. Permanent changes belong in the budget’s declaration.
ClearBudgetLimitAsync(budget, key?, by) Lifts it.
RequeueJobAsync(id) Gives one failed, deferred or abandoned job fresh attempts, keeping its cursor.
SkipJobAsync(id, reason) Takes one job that is not running out of line for good, without running it, so the jobs behind it in an ordered lane go on. The job is deleted; the actions log keeps who skipped which job and why. Its dedup key is free again.
RestartJobAsync(id) Runs one job again from its first page, a completed one too, for a window whose data has to be fetched again.
RequeueByCodeAsync(queue, code) Requeues every failing, deferred and abandoned job of the queue with that error code, after you fixed the cause.
SyncNowAsync(queue, owner, type) Fetches the owner’s new data now, from where its completed data reaches, at high priority. While that job waits, later calls stretch it to now instead of adding jobs, so a webhook storm is one fetch. A type without windows is fetched whole. The planner’s own jobs are not touched.
PrioritizeOwnerAsync(queue, owner) Moves the owner’s waiting jobs ahead of every other owner’s, urgent jobs excepted, keeping its own order of levels and types. Jobs created later get the owner’s usual priority; to keep it ahead, give it a JobOwner.Priority.

Apart from the pause, none of them touches a running job. A call about one job that cannot act says why (SL3008, SL3102); the others return how many jobs they changed.

Every one of them is written to the actions table with who asked (by, which the HTTP endpoints fill in from the signed-in user), why, and how many jobs it changed. GetActionsAsync lists them, newest first, for 400 days. GetPausesAsync lists the pauses in force; GetOverviewAsync and GetFreshnessAsync show them too. GetBudgetLimitsAsync lists the budget limits in force, and GetBudgetsAsync shows on each key the limit it obeys.

An instance of a version before pauses keeps taking jobs of a paused queue, and paces at the declared limit whatever an operator or the API sets. Upgrade every instance before relying on either.

Everything SyncLane does is a plain update, so you can do the same from psql when you need something it does not offer.

Give stopped jobs of one owner fresh attempts (this is RequeueAsync). The cursor is left alone on purpose:

UPDATE synclane.jobs
SET status = 'pending', attempt_count = 0, next_run_at = now(), error = NULL, completed_at = NULL, updated_at = now()
WHERE owner = 'globex' AND status IN ('failed', 'deferred', 'abandoned');

Start one job over from its first page. Only do this if the other side’s cursor is no longer valid:

UPDATE synclane.jobs
SET cursor = NULL, processed_count = 0
WHERE id = 33 AND status <> 'running';

Hold an owner back for an hour without losing anything:

UPDATE synclane.jobs
SET next_run_at = now() + interval '1 hour'
WHERE owner = 'globex' AND status IN ('pending', 'failed');

Let one job jump the queue, as if it had been enqueued urgent:

UPDATE synclane.jobs SET priority = priority - 10000000 WHERE id = 33;

Do not set a row to running by hand, and do not change leased_by. Those two columns are how a worker proves a job is still its own.

Keeping the table small

SyncLane deletes old jobs itself. Instances that run the planner do it at start-up and then every hour, in batches of 1,000, for their own queues only:

Kept Deleted
Initial jobs. They are bounded by the horizon, GetProgressAsync counts them, and they record what history was loaded. Completed recurring and manual jobs older than KeepCompletedJobs (default 7 days).
Jobs that have not completed: pending, running, failed, deferred and abandoned.  

Set KeepCompletedJobs = null to keep every job. Deleting is safe at any age: the planner reads coverage, not the history.

An abandoned recurring job does not pile up: the next period’s job replaces it and reaches back over its window. An abandoned slice of history waits for RequeueAsync.

rate_buckets holds one small row per budget and key, updated in place. Rows of keys you no longer use can be deleted; a key that is used again starts with a full bucket.

When an account only disconnects, call CancelAsync and stop returning it from your IOwnerSource. If it connects again, call RequeueAsync for it: its abandoned slices are still planned and come back with their cursors.

When an account is removed for good, stop returning it from your IOwnerSource, then call ForgetOwnerAsync, then delete its data. A job running at that moment stops at its next page, and that page may still reach your handler. Do not delete its rows by hand: the summary would stay, and if the account came back its history would never be loaded again.

Moving from another system

If another system already loaded an account’s history, record it with MarkLoadedAsync before the planner runs for that account. Each range becomes a completed initial job. The planner then fetches only what is older, back to the horizon, and what came after, with the next recurring window.

var ranges = oldJobs
    .GroupBy(job => (job.StoreId, job.Type))
    .Select(group => new LoadedRange
    {
        Owner = new JobOwner(group.Key.StoreId.ToString()),
        Type = group.Key.Type,
        Start = group.Min(job => job.WindowStart),
        End = group.Max(job => job.WindowEnd)
    })
    .ToList();

var result = await syncLane.MarkLoadedAsync("orders", ranges);
result.ThrowIfFailed();

Mark only ranges without holes: a hole inside a marked range is never fetched. If the old system’s history has gaps, mark each unbroken run as its own range.

A safe order:

  1. Deploy with RunPlanner and RunWorkers off.
  2. Mark the history.
  3. Stop the old system.
  4. Turn SyncLane on.

Choosing the numbers

slice is the setting with the most consequences. One slice is one job, and a job is the largest unit of work that cannot be split. Work out how long one slice takes for your biggest account at the other side’s rate limit.

The system this design comes from once tripled its slice from 30 to 90 days to “have fewer jobs”. For the largest account a single slice then held about 43,000 records, 430 pages, roughly twenty hours of requests, against page tokens that lived for twenty-four. Every hiccup late in a slice cost most of a day. Small slices finish, and a finished slice is progress that cannot be lost.

LeaseDuration only needs to cover the time it takes to notice a dead process, because the heartbeat renews it during long pages. The default of five minutes means a job whose worker died waits at most five minutes to be taken over. Going much lower mostly buys false alarms during garbage collection pauses or a slow database.

Concurrency is per process and per queue. Three instances with Concurrency = 4 run up to twelve jobs of that queue at once, and still at most one per lane. Set it to 1 for an API that must be called strictly one request at a time, whatever the account.

More instances or more slots make more accounts load side by side. They do not make one account load faster: its jobs share a lane, and its requests share the API’s limit for that account. A new account’s history is loaded newest slice first, so its recent data arrives first; PrioritizeOwnerAsync moves it ahead of the others in line.

Per environment. Every number here can come from configuration instead of code, with AddSyncLane(configuration, ...): see Settings from configuration. Raise Concurrency or widen every in one environment without a deploy.

MaxAttempts counts pick-ups since the job last saved a page, so it limits failures in a row, not over a job’s life. A throttle counts only when the recipe or handler has no rate budget; with one, the budget’s pause is what protects the API, and the job is postponed instead. With the defaults a job that keeps failing on one page is deferred after about fifteen minutes of ordinary failures, or more than an hour of unbudgeted throttling.

A job whose budget keeps being refused waits as long as the budget’s pause, which grows to five minutes, and is never deferred for it: it shows as pending with an SL5105 error, and synclane.budget.paused grows. That is the signal to look at the account.

every and overlap: a recurring window starts overlap before the previous one ended. Use the overlap to cover records that show up late on the other side, and rely on upserts to make the double read harmless.

Metrics and traces

All metrics belong to the meter SyncLane (SyncLaneTelemetry.MeterName). Every one carries synclane.queue and, except the planner metrics, synclane.job.type. Owners and lanes are never tags: there can be thousands of them and they often identify customers. Use the table for questions about one owner.

Metric Unit What it counts
synclane.job.taken {job} Jobs a worker took.
synclane.job.wait s Time between a job becoming due and a worker taking it. Rising wait with idle CPU means too few worker slots, or lanes that are always busy.
synclane.job.finished {job} Runs that ended, with synclane.job.outcome: completed, failed, deferred, abandoned, released (worker stopping), postponed (waiting, attempt not counted), paused (cut by a pause, attempt not counted) or lease_lost.
synclane.job.duration s Time from taking a job to the end of the run, with the same outcome tag.
synclane.page.completed {page} Pages a handler returned.
synclane.page.duration s Time a handler took for a page it returned.
synclane.record.processed {record} Records the handlers reported.
synclane.job.throttled {job} Runs that ended with a JobThrottledException: throttles without a rate budget, which use an attempt. They are also counted as failed, deferred or abandoned. With a budget, a throttle postpones the job and shows in synclane.budget.paused.
synclane.planner.planned {job} Jobs the planner added.
synclane.planner.failed {run} Planner runs that failed.
synclane.budget.wait s Time a request waited for a token of a rate budget. Tagged synclane.budget only; the key is an owner and never a tag.
synclane.budget.paused {pause} Times a rate budget was paused because the API answered 429. Tagged synclane.budget only.
synclane.budget.exhausted {pause} Times a rate budget waited for a reset because the API’s headers said nothing was left. Tagged synclane.budget only. Expected when a quota is used to the full.

Traces come from the activity source SyncLane (SyncLaneTelemetry.ActivitySourceName). A synclane.job span covers one run of a job and carries synclane.job.id, synclane.job.mode, synclane.job.attempt and, at the end, synclane.job.outcome; a failed run records the exception and sets the span status to error. Each page is a synclane.page span inside it with synclane.page.processed. The cursor is not recorded, because some APIs put tokens in it.

Four signals worth an alert:

Ordered queues

Some deliveries must arrive in the order they were made: a stock movement, then the next one for the same company. Declare their queue ordered:

options.AddQueue("movements", queue =>
{
    queue.Ordered = true;
    queue.Handle<MovementHandler>("movement");
});

Health checks

Register SyncLaneHealthCheck with your health checks and point your orchestrator’s probe at the endpoint:

builder.Services.AddHealthChecks().AddCheck<SyncLaneHealthCheck>("synclane", tags: ["ready"]);
app.MapHealthChecks("/health/ready", new() { Predicate = check => check.Tags.Contains("ready") });
Status When What to do
Unhealthy The database cannot be reached Check PostgreSQL and ConnectionString. The description names the error’s type only.
Unhealthy The schema lacks migrations Apply SyncLaneSchema.Script(), or set CreateSchema to true.
Unhealthy No heartbeat for two minutes Other instances count this one as gone, and another application may take its queues. The log says why the heartbeat fails.
Degraded Starting Nothing: it has not registered yet.
Degraded The latest planning round of a queue failed No new jobs are planned for it until a round succeeds. The log says why, usually the owner source.
Degraded A switch turns workers or the planner on, but another application runs one of the queues They stay off. Rename the queue in one of them, or give both the same Application. See Switching an application on and off.
Degraded Another application registers under the same name Give one of them a name of its own with options.Application. See Several applications on one schema.
Degraded It does not hear job changes Workers poll every PollInterval. See Behind a connection pooler.

ASP.NET Core answers 200 for degraded and 503 for unhealthy by default, so a readiness probe keeps a degraded instance in service. Use it for readiness rather than liveness: a database outage makes every instance unhealthy at once, and restarting them all helps nothing.

Several instances

Run as many instances as you like against the same table. Planning is safe to do from all of them, and so is taking jobs.

If your web instances should not do background work, give the worker a role and switch the web instances off from the monitor (Switching an application on and off). ISyncLane works the same either way.

Switching an application on and off

What a process is comes from its settings; whether it works comes from the database. The settings name the process, and nothing else about this:

What Where Example
The application’s name, its own on the schema SyncLane:Application in appsettings.json, committed "Application": "shop"
The role, only when one build runs as web and worker the worker’s environment SyncLane__Role=worker
Whether it runs workers and the planner a switch per application and role, in the database set from the monitor, below

RunWorkers and RunPlanner stay at their default, true, and a switch set from the monitor overrides them, without a deploy, a configuration change or a visit to the server. With ten applications on one schema, that is one place to start and stop all of them:

await monitor.SetSwitchAsync("shop", role: "worker", runWorkers: true, runPlanner: true, reason: "launch", by: user);

Over HTTP: POST /synclane/switch?application=shop&role=worker&workers=true&planner=true&reason=launch, and GET /synclane/switches lists every application and role with its switch and what its live instances run.

Rate budgets are shared through rate_buckets: every instance takes its tokens from the same row, and a 429 heard by one instance pauses the key on all of them. Declare the same budgets on every instance that runs workers. Taking a token is one statement that locks the bucket’s row for a moment; measured against a local PostgreSQL 18, one key served about 12,000 tokens a second to eight callers, far more than any API we know allows.

Keep the clocks of your instances in sync. Leases, due times and token refills are computed with the clock of the instance that asks. For leases a skew of seconds does not matter; for a budget, an instance whose clock runs one second ahead can let one second’s worth of extra tokens through.

Behind a connection pooler

Everything SyncLane writes and reads works through a pooler in transaction mode, such as PgBouncer with pool_mode = transaction: every statement stands alone or runs in its own transaction, nothing is kept in the session, and its one advisory lock ends with its transaction. CI runs the whole test suite through PgBouncer in that mode.

The exception is the one connection each process keeps open to hear job changes, which wake idle workers at once and feed WatchAsync and the /stream endpoint. It uses LISTEN, and a pooler in transaction mode accepts LISTEN without an error and never delivers what it hears, because the server connection that listened goes back to the pool. Worse, that server connection stays registered: every change sent to the channel afterwards reaches whichever client the pooler lends it to next, and those clients, any application’s, time out now and again.

So SyncLane checks before it listens. Each time that connection opens, it listens on a channel of its own that nothing else uses, sends a probe there through ConnectionString, and waits five seconds to hear it. Only then does it listen on the schema’s channel. If it hears nothing, it logs a warning that names ListenConnectionString, closes the connection and does not listen again until the next start. Workers then look for jobs every PollInterval, and live views stay quiet. At most the probe’s own channel stays registered behind the pooler, and nothing ever sends to it.

To keep live changes, give the listening connection a way around the pooler:

{
  "SyncLane": {
    "ConnectionString": "Host=pgbouncer;Port=6432;Database=app;Username=app",
    "ListenConnectionString": "Host=db;Port=5432;Database=app;Username=app"
  }
}

It costs one server connection per process. A pooler in session mode works too, and so does a proxy that pins a session to one server connection when it sees LISTEN. Without a pooler, leave ListenConnectionString out.

Where nothing can get around the pooler, switch listening off from the monitor before the application first starts: SetListeningAsync(application, role, listen: false, reason), or POST /synclane/switch/listen?application=…&listen=false&reason=… (Switching an application on and off). Each instance reads the switch before it listens, so no connection is opened and no probe is sent; ListenForChanges = false in configuration does the same where nobody manages switches. workers look for jobs every PollInterval, which you may want shorter, and the health check does not count it as a problem. Do this rather than leaving the warning: the probe itself is one notification, sent at each start, and behind PgBouncer in transaction mode it can reach whichever client the pooler lends that server connection to just then.

Versions before 0.4.1 listened on the schema’s channel through the pooler. PgBouncer then logs got packet 'A' from server when not linked for each change, and its other clients can see read timeouts. After upgrading, run RECONNECT on PgBouncer’s admin console, or restart it, so the server connections that still hold that LISTEN are closed; otherwise they close at the latest after server_lifetime (an hour by default).

Several applications on one schema

Applications can share one database and one SyncLane schema. Each declares its own queues, so jobs, planning and clean-up stay separate, and a budget declared by several applications is shared by all of them. One table then shows every sync of every application.

Each process registers in instances at start-up and every half minute after. Its row holds:

Its id is the host, the process id and a random part, so two containers that share a hostname and both run as process 1 are still two rows.

The application is options.Application. It defaults to the host’s application name, which is the entry assembly’s unless the host sets another. Copies of one application share their queues.

A queue belongs to one application at a time. A worker takes any job of a queue it declares, so two applications on one queue would take each other’s jobs and abandon the types they do not know (SL4001). An instance that would run a queue a live instance of another application runs does not start, and says which one (SL1011). Rename the queue in one of them, or give both the same Application if they are one application.

Upgrading the schema

The schema is a numbered list of migrations. synclane.migrations records which ones a database has. With CreateSchema on, each instance applies what is missing at start-up, in one transaction, behind a lock; instances that start together wait for each other, and the second one finds nothing to do.

Migrations are only ever added, and each one only adds. An instance still running the previous version keeps working against the upgraded table, so you can roll a deploy one instance at a time.

A table created by SyncLane 0.1, before the migrations table existed, is adopted as it is: migration 1 finds the table and indexes in place and only records itself.

Migrations 5 and 14 build indexes on jobs, and a plain CREATE INDEX blocks writes to the table while it runs. On a large table, create them first without blocking. The migration then finds them and skips them:

CREATE INDEX CONCURRENTLY IF NOT EXISTS jobs_open
    ON synclane.jobs (queue, owner, type) WHERE status <> 'completed';
CREATE INDEX CONCURRENTLY IF NOT EXISTS jobs_finished
    ON synclane.jobs (queue, completed_at) WHERE status = 'completed' AND mode <> 'initial';
CREATE INDEX CONCURRENTLY IF NOT EXISTS jobs_lease_order
    ON synclane.jobs (queue, (status = 'deferred'), priority, window_start DESC NULLS LAST, next_run_at, id)
    WHERE status <> 'completed';

jobs_lease_order covers only jobs that have not completed, so even on a table with a long history it is usually built in a moment.

Running the schema yourself

Set CreateSchema = false and put the script into your own migrations. SyncLaneSchema.Script() returns it for any schema name; with EF Core that is one line:

migrationBuilder.Sql(SyncLaneSchema.Script("synclane"));

When a SyncLane upgrade brings a new migration, add a new migration of your own with the same line. The script is safe to run on a database that already has some or all of it.

schema.sql is the same script for the default schema, checked against the library by a test.

With CreateSchema off SyncLane never changes the database. It checks at start-up that every migration it knows is recorded, and if one is missing it stops the host with a message that names the migration and the call that produces it.