liveBook · Manning

Synchronous call

As explained in Microservices Patterns, Second Edition

Synchronous code performs operations in a sequence that waits for each operation to complete before continuing.

A synchronous call waits for an operation to complete before the caller continues. Across service boundaries, synchronous request/response mechanisms such as HTTP-based REST and gRPC let a client request work and receive a response. The choice affects runtime coupling, availability, and performance. section 8.0 section 8.1

Synchronous requests do not require every internal step to be synchronous. An HTTP endpoint can coordinate work through asynchronous messages, but it must decide when to respond. Waiting for all participants gives the client the final outcome; responding after local work and the first message avoids waiting for the other services. section 8.3.9

Creating an order

A POST /orders endpoint can wait while the Order Service exchanges messages with other services, then return the final order-creation outcome. This makes the endpoint runtime-coupled to those services: if they are unavailable, it cannot respond, increasing latency and reducing availability. Alternatively, it can validate locally, update its database, send messages, and respond before the other services finish. section 8.3.9

Synchronous request/response is useful in several service collaboration patterns:

  • API composition can query services that hold different parts of the data. This is often straightforward to implement and lets clients read their own writes, unlike queries against eventually consistent replicas that can lag. section 12.2.3
  • A saga orchestrator can call participant REST endpoints and use their HTTP responses to trigger state transitions. On a communication failure with no response, it remains in the same state and retries after an interval. section 10.2.5

A synchronous saga orchestrator cannot rely on a message broker’s at-least-once delivery guarantee, so it must handle transient participant failures with retries. Participant endpoints can use an idempotency key to detect duplicate requests and resend the original response. section 10.2.5

Figure 10.16 The state machine for a saga orchestrator that invokes the participants using HTTP. HTTP responses trigger state machines. If there’s a communication failure, the state is unchanged and a transition to the same state occurs after a timeout.
orchestration http state machine

The state machine shows how responses advance a synchronous saga and how a timeout leads to a retry after a communication failure. section 10.2.5

Choose response timing and failure handling deliberately. Waiting for a final result gives the client a direct outcome but couples availability to the work required to produce it. Where an early response is acceptable, local processing followed by asynchronous messaging avoids waiting for other services. section 8.3.9

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains response timing, runtime coupling, retries, and idempotency through service flows and a saga state machine.
Examples
Illustrates order creation through a REST endpoint and a saga orchestrator invoking participant endpoints.
What you can do after
Evaluate synchronous response timing and apply retries and idempotency to service interactions.
How it connects
Connects synchronous calls to asynchronous messaging, saga orchestration, API composition, and runtime coupling.

Also explained in

Compare how 12 books explain synchronous call →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.