Run
Usectx.run to safely wrap any non-deterministic operation, like HTTP calls or database responses, and have Restate store its result in the execution log.
ctx.run, you cannot use the Restate context (e.g., ctx.get, ctx.sleep, or nested ctx.run).
Serialization
Serialization
By default, results are serialized using JSON. See the serialization docs to customize this.
Error handling and retry policies
Error handling and retry policies
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:- You can limit retries by time or count
- When the policy is exhausted, a
TerminalErroris thrown - See the Error Handling Guide and the Sagas Guide for patterns like compensation
Increasing timeouts
Increasing timeouts
If Restate doesn’t receive new journal entries from a service for more than one minute (by default), it will automatically suspend 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 inactivity timeout and abort timeout settings to accommodate longer execution times.For more information, see the error handling guide.
Stopping work when an attempt ends
Stopping work when an attempt ends
If the connection between Restate and your service is lost while a Aborting stops work that has not happened yet. It cannot take back a request that already reached the other system, so calls that are not idempotent still need an idempotency key, like the invocation ID above.
ctx.run is still executing, Restate retries the invocation in a new attempt. The old attempt keeps running in your service until its code finishes. Its result is discarded, but its side effects still happen: the HTTP request still goes out and the LLM call still runs to the end.ctx.request().attemptCompletedSignal is aborted when the current attempt ends. Pass it to anything that accepts an AbortSignal, so the old attempt stops instead of running alongside the new one:With Durable Coroutines, use
yield* run(...) inside a coroutine to record a side effect. To compose multiple Restate operations, use gen(...), since run callbacks cannot contain Restate context operations.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:Random numbers
To generate a deterministic float between0 and 1:
Math.random() but is deterministically replayable.