Skip to main content

Awakeables

Awakeables pause an invocation while waiting for another process to complete a task. You can use this pattern to let a handler execute a task somewhere else and retrieve the result. This pattern is also known as the callback (task token) pattern.

Creating awakeables

  1. The handler creates an awakeable. This contains a String identifier and a Promise.

const awakeable = ctx.awakeable<string>();
const awakeableId = awakeable.id;
await ctx.run(() => triggerTaskAndDeliverId(awakeableId));
const payload = await awakeable.promise;

  1. The handler triggers a task/process and attaches the awakeable ID (e.g. over Kafka, via HTTP,...). For example, send an HTTP request to a service that executes the task, and attach the ID to the payload. You use ctx.run to avoid re-triggering the task on retries.

const awakeable = ctx.awakeable<string>();
const awakeableId = awakeable.id;
await ctx.run(() => triggerTaskAndDeliverId(awakeableId));
const payload = await awakeable.promise;

  1. The handler waits until the other process has executed the task. The handler receives the payload and resumes.

const awakeable = ctx.awakeable<string>();
const awakeableId = awakeable.id;
await ctx.run(() => triggerTaskAndDeliverId(awakeableId));
const payload = await awakeable.promise;

  1. The handler creates an awakeable. This contains a String identifier and a Promise.
  1. The handler triggers a task/process and attaches the awakeable ID (e.g. over Kafka, via HTTP,...). For example, send an HTTP request to a service that executes the task, and attach the ID to the payload. You use ctx.run to avoid re-triggering the task on retries.
  1. The handler waits until the other process has executed the task. The handler receives the payload and resumes.

const awakeable = ctx.awakeable<string>();
const awakeableId = awakeable.id;
await ctx.run(() => triggerTaskAndDeliverId(awakeableId));
const payload = await awakeable.promise;

Completing awakeables

The external process completes the awakeable by either resolving it with an optional payload or by rejecting it with its ID and a reason for the failure. This throws a terminal error in the waiting handler.

  • Resolving over HTTP with its ID and an optional payload:

curl localhost:8080/restate/awakeables/prom_1PePOqp/resolve
-H 'content-type: application/json'
-d '{"hello": "world"}'

  • Resolving over HTTP with its ID and an optional payload:

curl localhost:8080/restate/awakeables/prom_1PePOqp/resolve
-H 'content-type: application/json'
-d '{"hello": "world"}'

  • Rejecting over HTTP with its ID and a reason:

curl localhost:8080/restate/awakeables/prom_1PePOqp/reject
-H 'content-type: text/plain' \
-d 'Very bad error!'

  • Rejecting over HTTP with its ID and a reason:

curl localhost:8080/restate/awakeables/prom_1PePOqp/reject
-H 'content-type: text/plain' \
-d 'Very bad error!'

  • Resolving via the SDK with its ID and an optional payload:

ctx.resolveAwakeable(awakeableId, "hello");

  • Resolving via the SDK with its ID and an optional payload:

ctx.resolveAwakeable(awakeableId, "hello");

  • Rejecting via the SDK with its ID and a reason:

ctx.rejectAwakeable(awakeableId, "my error reason");

  • Rejecting via the SDK with its ID and a reason:

ctx.rejectAwakeable(awakeableId, "my error reason");

Payload serialization

You can return any payload that can be serialized as a Buffer with Buffer.from(JSON.stringify(yourObject)) and deserialized with JSON.parse(result.toString()) as T.

Cost savings on FaaS

When running on Function-as-a-Service platforms, such as AWS Lambda, Restate suspends the handler while waiting for the awakeable to be completed. Since you only pay for the time that the handler is actually running, your don't pay while waiting for the external process to return.

Awaiting awakeables in Virtual Objects

Virtual Objects only process a single invocation at a time, so the Virtual Object will be blocked while waiting on the awakeable to be resolved.