Resilient Microservice Design With Spring Boot — Bulkhead Pattern

Need For Resiliency:

Why Bulkhead?

Sample Application:

  • /product/{id} → an endpoint which gives more details about the specific product along with ratings and stuff. It depends on the results from rating-service. Users updating their rating, leaving comments, replying to the comments everything goes via this endpoint.
  • /products → and endpoint which gives list of products we have in our catalog based on some search criteria. It does not depend on any other services. Users can directly order product (add to cart) from the list.

Product Controller:

@RestController
@RequestMapping("v1")
public class ProductController {

@Autowired
private ProductService productService;

@GetMapping("/product/{id}")
public ProductDTO getProduct(@PathVariable int id){
return this.productService.getProduct(id);
}

@GetMapping("/products")
public List<ProductDTO> getProducts(){
return this.productService.getProducts();
}

}
server:
tomcat:
max-threads: 15

Bulkhead Implementation:

  • We allow max 10 concurrent requests to the rating service even when we have 15 threads.
  • max wait duration is for when we get any additional requests for rating service when the existing 10 threads are busy, we wait for only 10 ms and fail the request immediately.
server:
tomcat:
max-threads: 15
port: 8082
rating:
service:
url: http://localhost:8081/v1/ratings
resilience4j.bulkhead:
instances:
ratingService:
maxConcurrentCalls: 10
maxWaitDuration: 10ms
  • @Bulkhead uses the instance we have defined in the application.yaml.
  • fallBackMethod is optional. It will be used when we have more than 10 concurrent requests
  • products requests average response time is 106 milli seconds compared to 3.6 seconds without bulkhead implementation. This is because we do not exhaust the resources of product-service.
  • By using the fallback method any additional requests for the product/1 are responded with default response.

Summary:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinoth Selvaraj

Vinoth Selvaraj

Principal Software Engineer — passionate about software architectural design, microservices.