Without it:

Order Service → Payment Service (down)
              → timeout → thread blocked
              → cascade failure

With Resilience4j:

Payment fails → circuit opens
              → fast fallback
              → system survives

Code to implement:

Add dependency at each service

<dependency>
  <groupId>io.github.resilience4j</groupId>
  <artifactId>resilience4j-spring-boot3</artifactId>
</dependency>

At each service application.yml

resilience4j:
  circuitbreaker:
    instances:
	    # this name should consist with later use one
      paymentService: 
        failureRateThreshold: 50
        slidingWindowSize: 10
        waitDurationInOpenState: 10s

Order Service calling Payment Service

@CircuitBreaker(name = "paymentService", fallbackMethod = "paymentFallback")
public PaymentResponse callPayment(PaymentRequest request) {
    return webClient.post()
						        .uri("<http://payment-service/pay>")
						        .bodyValue(request)
						        .retrieve()
						        .bodyToMono(PaymentResponse.class)
						        .block();
}

Create fallback method in calling service (Order Service)

public PaymentResponse paymentFallback(
        PaymentRequest request,
        Throwable ex
) {
    return new PaymentResponse(
        "FAILED",
        "Payment service unavailable"
    );
}