FerriteDocs

Transactions

Commit one atomic decision across up to 64 keys with the APE service.

Ferrite Transactions, served by the APE (atomic envelope) service, apply up to 64 key updates in one atomic commit. All writes in the envelope commit together or none takes effect, and readers never observe a partially applied envelope.

When to use transactions

Use a transaction whenever one business rule spans several records: reserving inventory while creating an order, holding a booking slot while recording its owner, or updating an account balance alongside its idempotency ledger.

inventory/product-42  stock: 8 → 7
order/order-91        absent → pending
idempotency/checkout  absent → order-91

All three changes commit together or none do.

Declare a transaction service

Set both the service primitive and the binding kind to ape:

{
  "services": [{ "name": "orders", "primitive": "ape" }],
  "applications": [{
    "name": "checkout",
    "artifact_key": "apps/checkout@v1",
    "artifact_sha256": "0000000000000000000000000000000000000000000000000000000000000000",
    "bindings": [{ "handle": "orders", "kind": "ape", "service": "orders" }]
  }]
}

Fetch the handle from AppContext and commit the three updates as one guarded envelope:

let mut orders = ctx.ape("orders")?;
let entries: [(&[u8], &[u8]); 3] = [
    (b"inventory/product-42", b"7"),
    (b"order/order-91", b"pending"),
    (b"idempotency/checkout", b"order-91"),
];
match orders.commit_n_if(b"idempotency/checkout", None, &entries).await? {
    Ok(()) => { /* all three keys changed together */ }
    Err(found) => { /* the guard held `found`: reload and decide again */ }
}

Keep the envelope bounded

One envelope covers 1 to 64 keys. That ceiling scopes each commit to a single domain decision. When a decision triggers wider fan-out, commit the authoritative state and an outbox record inside the envelope, then drive downstream work through a Queue.

An APE service never shards: every key lives in one group so cross-key commits stay atomic. Ferrite derives shard counts from declared rates and volume; manifests cannot set a shard count directly. Any APE declaration whose workload requires more than one group is refused with SERVICE_APE_UNSHARDED. Lower the declared rates or volume_mib, or split independent sets of keys across separate APE services.

Handle conflicts

Guard an envelope on one key with ctx.ape("name")?.commit_n_if(guard, expected, entries). Every entry commits only if guard currently holds expected (None requires the key to be absent); otherwise the call returns the value it found. Use commit_n for an unguarded commit, or commit(key_a, value_a, key_b, value_b) for the two-key shorthand. On conflict, reload the current state and re-evaluate your business rule instead of retrying an old decision indefinitely.

The hello template uses this shape for once-per-key request admission: it commits the computed result and an idempotency marker together, guarded on the marker being absent, so a retried delivery returns the stored result. Side effects outside the envelope, such as calls to an external service, can still happen more than once; see Queues for making external work safe to repeat.

A guard mismatch can also come from your own retry. If a reply is lost after the first commit landed, your retry finds the guard already updated by your own envelope. Re-read your own marker or record before assuming another writer won.

Read envelope state

get(key) reads a single key, and multi_get(&keys) reads up to 256 keys from one consistent point so readers never observe half of an envelope.

In the platform build, the handle also offers get_wait(key, seen, wait_ms). It returns the current value alongside a progress number; pass the progress number you last saw to wait up to wait_ms (capped at 5 seconds regardless of what you pass) for the key to change. A tailer that needs to wait longer calls get_wait again with the progress number returned on the previous call. The local SDK used by ferrite check and ferrite dev does not declare get_wait, so code calling it compiles only in the platform build.

APE handles have no delete, prefix scan, or expiry. You can overwrite a key, never remove or list it. Keep records that need listing, deletion, or expiry in the Key-value Store, and reserve APE for the keys that must change together.

ferrite-demo-shop and ferrite-booking each declare an APE service so Alloy commits an action’s result record, cache-tag changes, and outbox entries as one envelope, while keeping single-key inventory and seat counts in the Key-value Store behind compare-and-swap. Both applications are described on Apps.

See Key-value Store for individual records and Workflows for long-running transitions.

Ferrite counts page views on this site. Privacy · Terms