Skip to main content

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. The Java SDK generates a client for each service, which you can use to make these calls.

Generating the clients

The clients are generated when you build the project. If you don't see the generated clients, make sure you have built the project with ./gradlew build.

Use the generated client's of the SDK to do the request-response call:

To a Service:


String response = MyServiceClient.fromContext(ctx)
.myHandler(request)
.await();

To a Service:


String response = MyServiceClient.fromContext(ctx)
.myHandler(request)
.await();

To a Virtual Object:


String response = MyVirtualObjectClient.fromContext(ctx, objectKey)
.myHandler(request)
.await();

To a Virtual Object:


String response = MyVirtualObjectClient.fromContext(ctx, objectKey)
.myHandler(request)
.await();

To a Workflow:


// Call the `run` handler of the workflow
String response = MyWorkflowClient.fromContext(ctx, workflowId)
.run(request)
.await();
// Calling some other `interactWithWorkflow` handler of the workflow
MyWorkflowClient.fromContext(ctx, workflowId)
.interactWithWorkflow(request)
.await();

To a Workflow:


// Call the `run` handler of the workflow
String response = MyWorkflowClient.fromContext(ctx, workflowId)
.run(request)
.await();
// Calling some other `interactWithWorkflow` handler of the workflow
MyWorkflowClient.fromContext(ctx, workflowId)
.interactWithWorkflow(request)
.await();

No need for manual retry logic

These calls are proxied by Restate, and get logged in the journal. In case of failures, Restate takes care of retries.

Workflow retention time

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.

Deadlocks with Virtual Objects

Request-response calls to Virtual Object can lead to deadlocks. When this happens, the Virtual Object remains locked and the system can't process any more requests. In this situation you'll have to unblock the Virtual Object manually by cancelling invocations. 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.

Sending messages

Handlers can send messages (a.k.a. one-way calls, or fire-and-forget calls). Use the client's .send() method to do this as follows:


MyServiceClient.fromContext(ctx)
.send()
.myHandler(request);

No need for message queues

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.

Use Restate's generated clients .send(Duration) method to send a delayed requests:


MyServiceClient.fromContext(ctx)
.send(Duration.ofSeconds(1))
.myHandler(request);

Scheduling async tasks

You can also use this functionality to schedule async tasks. Restate will make sure the task gets executed at the desired time.

Ordering guarantees in Virtual Objects

Invocations to a Virtual Object are executed serially. Invocations will execute in the same order in which they arrive at Restate. For example, assume the following code in ServiceA:


_10
MyVirtualObjectClient.fromContext(ctx, objectKey).send().myHandler("Hi!");
_10
MyVirtualObjectClient.fromContext(ctx, objectKey).send().myHandler("Hi again!");

It is guaranteed that the invocation on line 1 will execute before the invocation on line 2. It is not guaranteed though that invocation 2 will be executed immediately after invocation 1, as invocations coming from other handlers/sources, could interleave these two calls.