Skip to main content
Restate uses an execution log to replay operations after failures and suspensions. Non-deterministic operations (database calls, HTTP requests, UUID generation) must be wrapped to ensure deterministic replay.

Run

Use ctx.run to safely wrap any non-deterministic operation, like HTTP calls or database responses, and have Restate store its result in the execution log.
const result = await ctx.run<string>(async () => doDbRequest());
Note that inside ctx.run, you cannot use the Restate context (e.g., ctx.get, ctx.sleep, or nested ctx.run).
By default, results are serialized using JSON. See the serialization docs to customize this.
Failures in ctx.run are treated the same as any other handler error. Restate will retry it unless configured otherwise or unless a TerminalError is thrown.You can customize how ctx.run retries via:
try {
  const myRunRetryPolicy = {
    initialRetryInterval: { milliseconds: 500 },
    retryIntervalFactor: 2,
    maxRetryInterval: { seconds: 1 },
    maxRetryAttempts: 5,
    maxRetryDuration: { seconds: 1 },
  };
  await ctx.run("write", () => writeToOtherSystem(), myRunRetryPolicy);
} catch (e) {
  if (e instanceof restate.TerminalError) {
    // Undo or compensate here (see Sagas guide)
  }
  throw e;
}
  • You can limit retries by time or count
  • When the policy is exhausted, a TerminalError is thrown
  • See the Error Handling Guide and the Sagas Guide for patterns like compensation
If Restate doesn’t receive new journal entries from a service for more than one minute (by default), it will automatically abort the invocation and retry it.However, some business logic can take longer to complete—for example, an LLM call that takes up to 3 minutes to respond.In such cases, you can adjust the service’s abort timeout and inactivity timeout settings to accommodate longer execution times.For more information, see the error handling guide.

Deterministic randoms

The SDK provides deterministic helpers for random values — seeded by the invocation ID — so they return the same result on retries.

UUIDs

To generate stable UUIDs for things like idempotency keys:
const uuid = ctx.rand.uuidv4();
Do not use this in cryptographic contexts.

Random numbers

To generate a deterministic float between 0 and 1:
const randomNumber = ctx.rand.random();
This behaves like Math.random() but is deterministically replayable.

Deterministic time

To get the current millis since midnight, January 1, 1970, that is consistent across retries:
const now = await ctx.date.now();
I