Synchronous calls done right

Microservices · lesson 3 of 33 · 4 min read

Call another service over HTTP with a client that has timeouts, and see the risk you just took on.

Open this lesson in the learning hub

Key points

  • Synchronous means the caller waits. Easy to reason about, but your availability is now capped by the service you call.
  • Blocking HTTP: RestClient (Spring 6.1+). RestTemplate is in maintenance mode; WebClient is reactive.
  • Always set a connect timeout and a read timeout. Without them a thread parks until the OS gives up, and one slow service stalls five.
  • Chained calls multiply failure. Three hops at 99.9% availability each gives the user 99.7%.
  • gRPC is worth it when you need low latency and a strict schema. JSON over HTTP wins on reach and on being debuggable with curl.

Example

// Server side: a small, boring HTTP endpoint.
@RestController
@RequestMapping("/api/orders")
class OrderController {

    private final RestClient inventory;

    // Spring Boot auto-configures RestClient.Builder for you.
    OrderController(RestClient.Builder builder) {
        this.inventory = builder.baseUrl("http://inventory").build();
    }

    @GetMapping("/{id}")
    OrderView get(@PathVariable String id) {
        // Client side: one blocking call to another service.
        Stock stock = inventory.get()
                .uri("/api/stock/{sku}", id)
                .retrieve()
                .body(Stock.class);

        return new OrderView(id, stock.available());
    }
}

// Never ship without timeouts. In application.yml (Spring Boot 3.4+):
//   spring.http.client.connect-timeout: 1s
//   spring.http.client.read-timeout: 2s

Every synchronous call is a dependency; give it a timeout or it will own you.

This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the Microservices course, and every lesson in it is listed on the Microservices contents page.