Service Communication
A handler can call another handler and wait for the response (request-response), or it can send a message without waiting for the response.
Request-response calls
Request-response calls are requests where the client waits for the response.
To a Service:
response = await ctx.service_call(my_service_handler, arg="Hi")
To a Service:
response = await ctx.service_call(my_service_handler, arg="Hi")
To a Virtual Object:
response = await ctx.object_call(my_object_handler, key="Mary", arg="Hi")
To a Virtual Object:
response = await ctx.object_call(my_object_handler, key="Mary", arg="Hi")
To a Workflow:
# Call the `run` handler of the workflow(only works once).await ctx.workflow_call(run, key="my_workflow_id", arg="Hi")# Call some other `interact_with_workflow` handler of the workflow.await ctx.workflow_call(interact_with_workflow, key="my_workflow_id", arg=None)
To a Workflow:
# Call the `run` handler of the workflow(only works once).await ctx.workflow_call(run, key="my_workflow_id", arg="Hi")# Call some other `interact_with_workflow` handler of the workflow.await ctx.workflow_call(interact_with_workflow, key="my_workflow_id", arg=None)
- Use
ctx.service_call
to call Services, andctx.object_call
to call Virtual Objects. - Specify the handler you want to call and supply the request.
For Virtual Objects, you also need to supply the key of the Virtual Object you want to call, here
"Mary"
. For Workflows, you need to supply a workflow ID that is unique per workflow execution. - Await the call to retrieve the response.
These calls are proxied by Restate, and get logged in the journal. In case of failures, Restate takes care of retries.
Once the run
handler of the workflow has finished, the other handlers can still be called up to the retention time of the workflow, by default 24 hours.
This can be configured via the Admin API per Workflow definition by setting workflow_completion_retention
.
Request-response calls to Virtual Objects can lead to deadlocks, in which the Virtual Object remains locked and can't process any more requests. Some example cases:
- Cross deadlock between Virtual Object A and B: A calls B, and B calls A, both using same keys.
- Cyclical deadlock: A calls B, and B calls C, and C calls A again.
In this situation, you can use the CLI to unblock the Virtual Object manually by cancelling invocations.
Sending messages
Handlers can send messages (a.k.a. one-way calls, or fire-and-forget calls), as follows:
To a Service:
ctx.service_send(my_service_handler, arg="Hi")
To a Service:
ctx.service_send(my_service_handler, arg="Hi")
To a Virtual Object:
ctx.object_send(my_object_handler, key="Mary", arg="Hi")
To a Virtual Object:
ctx.object_send(my_object_handler, key="Mary", arg="Hi")
To a Workflow:
# Call the `run` handler of the workflow (only works once).ctx.workflow_send(run, key="my_workflow_id", arg="Hi")# Call some other `interact_with_workflow` handler of the workflow.ctx.workflow_send(interact_with_workflow, key="my_workflow_id", arg=None)
To a Workflow:
# Call the `run` handler of the workflow (only works once).ctx.workflow_send(run, key="my_workflow_id", arg="Hi")# Call some other `interact_with_workflow` handler of the workflow.ctx.workflow_send(interact_with_workflow, key="my_workflow_id", arg=None)
Without Restate, you would usually put a message queue in between the two services, to guarantee the message delivery. Restate eliminates the need for a message queue because Restate durably logs the request and makes sure it gets executed.
Delayed calls
A delayed call is a one-way call that gets executed after a specified delay.
To schedule a delayed call, send a message with a delay parameter, as follows:
To a Service:
ctx.service_send(my_service_handler, arg="Hi", send_delay=timedelta(seconds=5))
To a Service:
ctx.service_send(my_service_handler, arg="Hi", send_delay=timedelta(seconds=5))
To a Virtual Object:
ctx.object_send(my_object_handler, key="Mary", arg="Hi", send_delay=timedelta(seconds=5))
To a Virtual Object:
ctx.object_send(my_object_handler, key="Mary", arg="Hi", send_delay=timedelta(seconds=5))
You can also use this functionality to schedule async tasks. Restate will make sure the task gets executed at the desired time.
Invocations to a Virtual Object are executed serially. Invocations will execute in the same order in which they arrive at Restate. For example, assume a handler calls the same Virtual Object twice:
ctx.object_send(my_object_handler, key="Mary", arg="I'm call A")ctx.object_send(my_object_handler, key="Mary", arg="I'm call B")
It is guaranteed that call A will execute before call B. It is not guaranteed though that call B will be executed immediately after call A, as invocations coming from other handlers/sources, could interleave these two calls.