Skip to content

Concepts

Idempotency

Retries are safe in Bok Joy when each layer has a key. A trigger key stops a second run from starting, and the run's own key stops a second side effect.

On this page

Two kinds of retry can repeat work:

  • Your app retries a trigger, for example after a timeout. An idempotency key on the trigger makes the retry return the run it already started.
  • Bok Joy retries an attempt, after a failure or a lost worker. The run's ctx.idempotencyKey, passed to the services your task calls, makes the second attempt's side effect a no-op.

Trigger keys#

Every trigger carries an idempotency key. Pass your own, named after the thing that should happen once:

await tasks.trigger('email.send', payload, {  idempotencyKey: `order-${order.id}:receipt`,});
A trigger with a key that...Returns
Is new in this environmentA new run.
Started a run with the same task, payload, placement and machineThat run, whatever its status. No second run starts.
Started a run with anything differentA ConflictError (HTTP 409): idempotency key reused with changed request.
  • A key is 1 to 128 bytes. Hash a longer one, for example to a 64-character SHA-256 hex digest.
  • Keys are scoped to an environment. The same key in dev and prod names two different runs.
  • A key lasts as long as its run. Reusing a key months later still returns the old run, so put a date in the key when you want a new run each day. The key ends only when run retention deletes the run's whole tree, and then the same key starts a new run. Runs started from @bokjoy/sdk/trigger hold their keys as Trigger.dev does instead (below).
  • The key still finds its run after the environment's deployment changes. The retry is compared with the run as it was accepted.

Without a key, the client makes a fresh UUID for each call. It sends that same key on its own retries, so a network failure or a 5xx answer never starts a second run. A key you choose also covers your own retries: a crashed web request, a redelivered webhook, a job that runs twice.

Batch keys#

A batch takes one key for all its entries. Entry i gets the key <key>:<i> unless it sets its own, so retrying the whole batch returns the same runs. A batch key is at most 124 bytes, so every derived key fits in 128. See Trigger runs.

Keys inside a run#

Side effects#

ctx.idempotencyKey is the run's ID. It is the same on every attempt, so pass it to every service that deduplicates: payment providers, email APIs, your own endpoints.

JavaScript
await stripe.paymentIntents.create(params, {  idempotencyKey: ctx.idempotencyKey,});

If a task makes several calls that must each happen once, derive one key per call, such as `${ctx.idempotencyKey}:charge` and `${ctx.idempotencyKey}:refund`.

Child runs#

Children are keyed by their parent, so a parent that runs again never starts a second child:

  • ctx.callTask in a workflow uses the step's key, by default call:<index>. A replay returns the recorded child.
  • ctx.triggerAndWait and ctx.batchTriggerAndWait use the step's key too, by default call:<index> and batch:<index>. A replay returns the recorded results and starts no child again, even if the input or items changed.
  • ctx.trigger uses the key you pass, by default trigger:<task>:<n> for the nth trigger of that task in this execution. The child's run ID is derived from the key and the parent's run ID, or in a retry from failure its first run's ID. So a retried parent gets the same ID back, and so does a retry from failure. Keys you choose may not start with trigger:.
  • A default key names a position among one task's triggers. If a fix adds a trigger of a task before its others, the added trigger takes the first one's child and the later ones shift. The run records a trigger.reused event when that happens. Give a trigger that must happen once a key of its own.

Keys from the Trigger.dev dialect#

A run that @bokjoy/sdk/trigger starts with a key holds it as Trigger.dev does, inside a run and from a backend:

A key...Then
Whose run failedStarts a new run. The failed run records the new one as released_to.
Whose run completed or was cancelledReturns that run until its idempotencyKeyTTL passes.
Past its idempotencyKeyTTLStarts a new run, whatever the old one's status. The TTL is 30 days unless you pass one, from "1s" to "365d", written as Trigger.dev writes it: "60s", "10m", "2h", "7d", "1w".
Reset with idempotencyKeys.reset(taskId, key, options)Starts a new run. The runs that held it are unchanged.
Reused with a different payload or options, from a backendReturns the run it names, marked isCached, as Trigger.dev does: the run keeps its own payload. A key that names a run of another task is still a ConflictError.
JavaScript
import { idempotencyKeys, tasks } from '@bokjoy/sdk/trigger'; await tasks.trigger('simphony.ingest', drop, {  idempotencyKey: await idempotencyKeys.create(drop.filename, { scope: 'global' }),  idempotencyKeyTTL: '24h',});

In a workflow, a wait whose child failed runs again on the workflow's next attempt, as Trigger.dev's retry starts that child again. The steps before it replay, and it and every later step run again. A child under a key never runs twice because of that: a later wait under a key whose child completed waits on that child again, even if its input changed, as Trigger.dev returns a key's run whatever the payload. A later wait without a key starts a new child, as on Trigger.dev.

Inside a run, a wait's key is made in the run's scope unless it is 64 characters long, as Trigger.dev makes it. idempotencyKey: 'charge-7' in triggerAndWait keys the child to this run, the same key in every attempt and in a retry from failure. A batch's items get keys made from the batch key and their index, in the run's scope whatever the batch key, in a wait or not. So a batch key made with scope: 'global' still starts each run's own items, as on Trigger.dev. A trigger that does not wait keeps its own plain key to its parent run.

idempotencyKeys.reset takes Trigger.dev's options:

ResetLets go of
reset(taskId, key) inside a run, or with {scope: 'run', parentRunId}The key in that run's scope: the children its triggers and waits started with it. Inside a run the run is this one.
reset(taskId, key, {scope: 'global'})The key a backend's tasks.trigger sent, and the key idempotencyKeys.create(key, {scope: 'global'}) makes.
reset(taskId, key, {scope: 'attempt', parentRunId, attemptNumber})The key idempotencyKeys.create(key, {scope: 'attempt'}) made in that attempt.
reset(taskId, key) from a backendThe key as a backend's tasks.trigger sent it, or a 64-character key as it is. A key that only triggers inside runs hold is refused with a 422 that asks for parentRunId.

Once a failed run gave its key up and a later trigger started a new run, the handle the first trigger returned still names the failed run. runs.retrieve and runs.poll from @bokjoy/sdk/trigger read it as the run the key moved to, the one doing the work, as Trigger.dev's handle would.

Bok Joy's own API, tasks.trigger from @bokjoy/sdk and the Python SDK keep every key as long as its run, as above.

Event IDs#

An event sent to a run has an event_id. Sending the same event again is a no-op. Sending a different event under the same ID is a 409. Use an ID that names the fact, such as activated-ada. See Send events.