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.
String output = ctx.run(String.class, () -> doDbRequest());
Note that inside ctx.run, you cannot use the Restate context (e.g., ctx.get, ctx.sleep, or nested ctx.run).
To run an action asynchronously, use ctx.runAsync:
DurableFuture<String> myRunFuture = ctx.runAsync(String.class, () -> doSomethingSlow());
You can use this to combine your run actions with other asynchronous operations, like waiting for a timer or an awakeable.Check out Concurrent tasks.
Have a look at the serialization docs to learn how to serialize and deserialize your objects.
Failures in ctx.run are treated the same as any other handler error. Restate will retry it unless configured otherwise or unless a TerminalException is thrown.You can customize how ctx.run retries via:
try {
  RetryPolicy myRunRetryPolicy =
      RetryPolicy.defaultPolicy()
          .setInitialDelay(Duration.ofMillis(500))
          .setExponentiationFactor(2)
          .setMaxDelay(Duration.ofSeconds(10))
          .setMaxAttempts(10)
          .setMaxDuration(Duration.ofMinutes(5));
  ctx.run("my-run", myRunRetryPolicy, () -> writeToOtherSystem());
} catch (TerminalException e) {
  // Handle the terminal error after retries exhausted
  // For example, undo previous actions (see sagas guide) and
  // propagate the error back to the caller
}
  • You can limit retries by time or count
  • When the policy is exhausted, a TerminalException 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 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:
UUID uuid = ctx.random().nextUUID();
Do not use this in cryptographic contexts.

Random numbers

To generate a deterministic float between 0 and 1:
int value = ctx.random().nextInt();
This behaves like the standard library Random class but is deterministically replayable. You can use any of the methods of java.util.Random to generate random numbers: for example, nextBoolean(), nextLong(), nextFloat(), etc.
I