a9script

Docs / Scripts

How runs share capacity

Every event that starts a script becomes a run. Runs wait in a queue and run in slots. Two settings on a script decide how many of its runs may be active at once; everything else is the platform’s fairness. The settings are described in Triggers and concurrency; what bounds the queue is in Limits and safety.

SettingWhereWhat it decidesWithout it
maxConcurrencyscriptHow many runs of this script may be active at onceone at a time
concurrencyKeyscriptA name several scripts share, so they share one limitthe script’s own name

A run keeps the limit it started under, even if the script is changed while it is parked.

Slots and the queue

Every run takes a slot
  1. queueslotsdonerun Arun Bfree

    1A runtime has a fixed number of slots. A slot is reserved before the next event is taken from the queue, so waiting work stays in the queue, not in memory.

  2. queueslotsdonerun Arun Brun C

    2When every slot is busy, new events wait in the queue. They are accepted, not refused, while the queue has room.

  3. queueslotsdonerun Arun Drun CB

    3When a run finishes, its slot takes the next event at once.

One script’s limit

maxConcurrency: how many runs of one script at once
  1. waitingrunningdonemaxConcurrency: 2

    1Two runs of the script are active, which is its limit. Without a setting the limit is one run at a time per script and environment.

  2. waitingrunningdonemaxConcurrency: 2

    2A third event arrives at the limit. It is set aside in the queue: it keeps its place and uses no delivery attempt. It is never refused for this reason.

  3. waitingrunningdonemaxConcurrency: 2finished

    3One run finishes; the waiting event starts as the second active run.

A limit shared by key

concurrencyKey: one limit shared by several scripts
  1. Sync ordersSync invoicesconcurrencyKey: erplimit: 1orders run

    1Two scripts that touch the same system name the same concurrencyKey. They now share one limit — here, one run at a time.

  2. Sync ordersSync invoicesconcurrencyKey: erplimit: 1orders run

    2An invoices run waits while the orders run holds the key, although it belongs to another script.

  3. Sync ordersSync invoicesconcurrencyKey: erplimit: 1invoices run

    3When the orders run ends, the invoices run starts. The limit that applies is the one of the script that is starting.

Fair turns

Fair turns between tenants and between sources
  1. tenant Atenant B

    1One tenant has a large backlog, another a little work.

  2. tenant Atenant Border of turns

    2Turns go round the tenants: each gets a few, then the next one. The small tenant's work does not wait for the large backlog.

  3. inside tenant Aresumed runswebhook /ordersschedule

    3Inside a tenant every source has its own queue. Resumed runs go first, and every webhook endpoint and the schedule still get a turn in each round.

Long runs take turns

A long run takes turns
  1. queueslotnextlong run

    1A run computes for a long time while other work waits.

  2. queueslotlong runsavednext

    2After its share of time the run is saved and queued again. The next run gets the slot; nothing is lost and nothing is repeated.

  3. queueslotlong runcontinues where it stopped

    3On its next turn the run continues exactly where it stopped.

Inside a run, one call at a time

One call after another inside a run; many runs side by side
  1. timeruncall Acall Bcall C

    1Inside a run, calls happen one after another, and each answer is recorded before the next call starts. A script has no parallel branches.

  2. timerun 1call Acall Bcall Crun 2call Acall Bcall Crun 3call Acall Bcall C

    2Work happens at the same time across runs: each event starts its own run, each in its own slot and within the script's maxConcurrency. Bulk work over many records belongs in the dataset functions.

Written for this site. What it says is held true by apps/runtime/test/dispatcher-leases.test.ts, apps/runtime/test/acceptance-lane-fairness.test.ts, apps/runtime/test/acceptance-slices.test.ts, apps/runtime/test/dispatcher-semantics.test.ts in the product's own repository. Found a problem on this page? Write to the address in the footer.