Control durability guarantees
Golem provides a set of functions components can call to control details of the durable execution engine.
General concepts
The library allows controlling four main aspects of the durable execution engine: the current persistence level, the idempotence mode, defining atomic regions and changing retry policies (discussed in the next page).
All these features are regional - they can be changed for a section of the code within a single exported function.
To make this easy to use in TypeScript, the library provides functions starting with with
, they take a closure
parameter which will be executed with the requested mode, and then they restore the previous settings.
Persistence level
The persistence level can be one of the following:
Level | Description |
---|---|
PersistNothing | Turns off persistence for a section. In case the agent is recovered or restarted, all the side-effecting functions will be reexecuted |
PersistRemoteSideEffects | Persists all the side-effects that are affecting the outside world. In case of recovery the side-effects won't be reexecuted and the persisted results will be used. |
Smart | The default setting; Let Golem decide what to persist to optimize performance |
To change the persistence level for a section of the code, use the withPersistenceLevel
function:
import {withPersistenceLevel} from "@golemcloud/golem-ts-sdk"
const result: string = withPersistenceLevel({tag: "persist-nothing"}, () => {
// this closure runs in PersistNothing mode
return "hello"
})
Idempotence mode
Golem assumes that HTTP requests are idempotent by default. This means that in case of a failure, if the system cannot determine whether the request successfully reached the target or not, it will be retried.
This behavior can be changed using the withIdempotenceMode
function:
import {withIdempotenceMode} from "@golemcloud/golem-ts-sdk"
const result: string = withIdempotenceMode(false, () => {
// this closure runs with idempotence mode disabled
return "hello"
})
With disabled idempotence mode, in case Golem cannot determine if the request was sent or not, it won't retry it, but the agent will fail.
Atomic regions
By default, side effects are persisted and retried one by one. It is possible to group them together into atomic regions, in which case the execution is retried for some reason (the agent failed or interrupted within the region), all the side effects will be reexecuted.
The golem-ts-sdk
library exports the atomically
function for using atomic regions:
import {atomically} from "@golemcloud/golem-ts-sdk"
const result: [string, string] = atomically(() => {
const firstResult: string = firstSideEffect()
const secondResult: string = secondSideEffect(firstResult)
return [firstResult, secondResult]
})
Commit oplog
The oplogCommit
function in "@golemcloud/golem-ts-sdk
waits until the oplog is committed to its persistent
storage. The function takes a single argument, replicas
, with the desired number of storage replicas the
agent's journal is replicated to. The function will block until the oplog is committed to the specified number
of replicas, or, if this number is larger than the available number of replicas, until it is written to all the
replicas.