<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Somenath Das</title>
    <description>The latest articles on DEV Community by Somenath Das (@somenath_das_kol_86).</description>
    <link>https://dev.to/somenath_das_kol_86</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3777154%2Fc21cb33a-4bfb-4de8-98f9-4900937e5779.jpg</url>
      <title>DEV Community: Somenath Das</title>
      <link>https://dev.to/somenath_das_kol_86</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/somenath_das_kol_86"/>
    <language>en</language>
    <item>
      <title>Why We Use circuit breaker and How Do we Implements this.</title>
      <dc:creator>Somenath Das</dc:creator>
      <pubDate>Sat, 03 Oct 2026 08:51:03 +0000</pubDate>
      <link>https://dev.to/somenath_das_kol_86/why-we-use-circuit-breaker-and-how-do-we-implements-this-5gf9</link>
      <guid>https://dev.to/somenath_das_kol_86/why-we-use-circuit-breaker-and-how-do-we-implements-this-5gf9</guid>
      <description>&lt;p&gt;The Circuit Breaker pattern is a crucial design pattern for ensuring system stability and fault tolerance in a microservices architecture. Below is a comprehensive overview of the Circuit Breaker pattern and its implementation in Spring Boot using Resilience4j.&lt;/p&gt;

&lt;p&gt;What is a Circuit Breaker?&lt;/p&gt;

&lt;h2&gt;
  
  
  In a microservices environment, if a specific service goes down or becomes slow, the Circuit Breaker temporarily halts calls to that service. This prevents the entire system from crashing or suffering from a cascading failure.
&lt;/h2&gt;

&lt;p&gt;The 3 main states of a Circuit Breaker:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Closed: This is the normal operating state. All requests pass successfully from one service to another.&lt;/li&gt;
&lt;li&gt;Open: If a service fails repeatedly or exceeds a specific threshold, the circuit trips to the 'Open' state. Requests are no longer sent to the main service; instead, an alternative or fallback response is returned immediately.&lt;/li&gt;
&lt;li&gt;Half-Open: After a set period, the circuit automatically transitions to the 'Half-Open' state. In this state, a limited number of requests are sent to verify whether the remote service has recovered. If they succeed, the circuit returns to the 'Closed' state; if they fail, it reverts to the 'Open' state.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Circuit Breaker Implementation in Spring Boot
&lt;/h2&gt;

&lt;p&gt;Step 1: Adding Dependencies&lt;/p&gt;

&lt;h2&gt;
  
  
  Spring Boot project's pom.xml
&lt;/h2&gt;

&lt;p&gt; &amp;lt;!-- Resilience4j Spring Boot Starter --&amp;gt;  io.github.resilience4j &lt;br&gt;
resilience4j-spring-boot3&lt;br&gt;
 &amp;lt;!-- For Spring Boot 3 --&amp;gt;&lt;br&gt;
 2.1.0 &lt;br&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  Application.property file configuration.
&lt;/h2&gt;

&lt;h1&gt;
  
  
  Resilience4j Circuit Breaker configuration for userServiceBreaker
&lt;/h1&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.registerHealthIndicator=true&lt;/p&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.slidingWindowType=COUNT_BASED&lt;/p&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.slidingWindowSize=10&lt;/p&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.failureRateThreshold=50&lt;/p&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.minimumNumberOfCalls=5&lt;/p&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.automaticTransitionFromOpenToHalfOpenEnabled=true&lt;/p&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.waitDurationInOpenState=10s&lt;/p&gt;

&lt;p&gt;resilience4j.circuitbreaker.instances.userServiceBreaker.permittedNumberOfCallsInHalfOpenState=3&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Implementation (Service &amp;amp; Controller):
&lt;/h2&gt;

&lt;h2&gt;
  
  
  In Service Class:--------------
&lt;/h2&gt;




&lt;h2&gt;
  
  
  We will use the @CircuitBreaker annotation and create a fallback method.
&lt;/h2&gt;

&lt;p&gt;package com.example.service;&lt;/p&gt;

&lt;p&gt;import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;&lt;br&gt;
import org.springframework.stereotype.Service;&lt;br&gt;
import org.springframework.web.client.RestTemplate; @Service&lt;br&gt;
public class OrderService {&lt;/p&gt;

&lt;p&gt;private final RestTemplate restTemplate = new RestTemplate(); &lt;br&gt;
private static final String USER_SERVICE_URL = "&lt;a href="http://localhost:8081/users/" rel="noopener noreferrer"&gt;http://localhost:8081/users/&lt;/a&gt;"; &lt;/p&gt;

&lt;p&gt;// The name here must match the instance name defined in application.yml&lt;br&gt;
@CircuitBreaker(name = "userServiceBreaker", fallbackMethod = "getUserFallback")&lt;br&gt;
public String getUserDetails(Long userId) {&lt;br&gt;
// Assume this calls another microservice that might be down&lt;br&gt;
return restTemplate.getForObject(USER_SERVICE_URL + userId, String.class); &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// The fallback method signature (parameters and return type) must match the original method,&lt;br&gt;
// with the addition of a Throwable or Exception parameter. &lt;br&gt;
public String getUserFallback(Long userId, Throwable throwable) {&lt;br&gt;
return "Sorry, the user service is currently down. Showing fallback data. Reason: " + throwable.getMessage(); &lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;




&lt;h2&gt;
  
  
  In the Controller class:
&lt;/h2&gt;

&lt;p&gt;package com.example.controller;&lt;/p&gt;

&lt;p&gt;import com.example.service.OrderService;&lt;br&gt;
import org.springframework.web.bind.annotation.GetMapping;&lt;br&gt;
import org.springframework.web.bind.annotation.PathVariable;&lt;br&gt;
import org.springframework.web.bind.annotation.RestController;&lt;/p&gt;

&lt;p&gt;@RestController&lt;br&gt;
public class OrderController {&lt;/p&gt;

&lt;p&gt;private final OrderService orderService; &lt;/p&gt;

&lt;p&gt;public OrderController(OrderService orderService) {&lt;br&gt;
this.orderService = orderService; &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;@GetMapping("/order/user/{id}")&lt;br&gt;
public String getOrderUser(@PathVariable Long id) {&lt;br&gt;
return orderService.getUserDetails(id); &lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;







&lt;p&gt;Testing:----&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;Normal State (Closed): When the user service (port: 8081) is active, the request will directly fetch the data.&lt;/li&gt;
&lt;li&gt;Service Down (Open): If the user service is down, errors will occur during the initial attempts. As soon as the failure rate exceeds 50% within 10 requests, the circuit breaker will trip and switch to the 'Open' state. Subsequently, it will execute the &lt;code&gt;getUserFallback&lt;/code&gt; method directly, without making the service call.&lt;/li&gt;
&lt;li&gt;Recovery (Half-Open): 10 seconds&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Now , we Consolidated all these:---------&lt;/p&gt;

&lt;p&gt;Guidelines for combining Retry or Rate Limiter patterns with the Circuit Breaker:---------&lt;/p&gt;

&lt;p&gt;Resilience4j allows you to chain or combine multiple patterns (such as Circuit Breaker, Retry, and Rate Limiter) on the same method.&lt;/p&gt;




&lt;p&gt;Retry Pattern:------&lt;br&gt;
This is used to automatically retry an operation a few times if a request fails due to temporary network issues or other reasons, rather than immediately returning an error.&lt;br&gt;
We include this in the &lt;code&gt;application.properties&lt;/code&gt; file:----------&lt;/p&gt;

&lt;h1&gt;
  
  
  Resilience4j Retry configuration for userServiceRetry
&lt;/h1&gt;

&lt;p&gt;resilience4j.retry.instances.userServiceRetry.maxAttempts=3&lt;br&gt;
resilience4j.retry.instances.userServiceRetry.waitDuration=2s&lt;/p&gt;

&lt;p&gt;Rate Limiter Pattern:&lt;br&gt;
This is used to control the maximum number of API calls a user or client can make.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;application.properties&lt;/code&gt; file and settings for Rate Limiter:&lt;/p&gt;

&lt;h1&gt;
  
  
  Resilience4j Rate Limiter configuration for userServiceRateLimiter
&lt;/h1&gt;

&lt;p&gt;resilience4j.ratelimiter.instances.userServiceRateLimiter.limitForPeriod=5&lt;br&gt;
resilience4j.ratelimiter.instances.userServiceRateLimiter.limitRefreshPeriod=10s&lt;br&gt;
resilience4j.ratelimiter.instances.userServiceRateLimiter.timeoutDuration=0&lt;/p&gt;




&lt;p&gt;An important rule (Order) regarding the simultaneous use of multiple annotations in the code:------&lt;/p&gt;

&lt;p&gt;Execution sequence of annotations:&lt;/p&gt;

&lt;h2&gt;
  
  
  Retry ➡️ CircuitBreaker ➡️ RateLimiter
&lt;/h2&gt;

&lt;p&gt;The combined code for all these features looks like this:-----------&lt;/p&gt;

&lt;p&gt;package com.example.service;&lt;/p&gt;

&lt;p&gt;import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;&lt;br&gt;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;&lt;br&gt;
import io.github.resilience4j.retry.annotation.Retry; import org.springframework.stereotype.Service;&lt;br&gt;
import org.springframework.web.client.RestTemplate;&lt;/p&gt;

&lt;p&gt;@Service&lt;br&gt;
public class OrderService {&lt;/p&gt;

&lt;p&gt;private final RestTemplate restTemplate = new RestTemplate(); &lt;/p&gt;

&lt;p&gt;// Using three annotations together; all share the same fallback method&lt;br&gt;
@Retry(name = "userServiceRetry", fallbackMethod = "commonFallback")&lt;br&gt;
@CircuitBreaker(name = "userServiceBreaker", fallbackMethod = "commonFallback")&lt;br&gt;
@RateLimiter(name = "userServiceRateLimiter", fallbackMethod = "commonFallback")&lt;br&gt;
public String getUserDetails(Long userId) {&lt;br&gt;
return restTemplate.getForObject("&lt;a href="http://localhost:8081/users/" rel="noopener noreferrer"&gt;http://localhost:8081/users/&lt;/a&gt;" + userId, String.class); &lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;// A common fallback method to handle errors for all patterns&lt;br&gt;
public String commonFallback(Long userId, Throwable throwable) {&lt;br&gt;
if (throwable instanceof io.github.resilience4j.ratelimiter.RequestNotPermitted) {&lt;br&gt;
return "You are sending requests too fast! Please wait a moment."; &lt;br&gt;
}&lt;br&gt;
return "The User Service is currently unavailable. Alternative data has been loaded."; &lt;br&gt;
}&lt;/p&gt;

&lt;h2&gt;
  
  
  }
&lt;/h2&gt;

&lt;p&gt;Hope it will make you understandable.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>java</category>
      <category>microservices</category>
      <category>spring</category>
    </item>
    <item>
      <title>What a Spring Boot Annotation Actually Do</title>
      <dc:creator>Somenath Das</dc:creator>
      <pubDate>Fri, 02 Oct 2026 15:30:45 +0000</pubDate>
      <link>https://dev.to/somenath_das_kol_86/what-a-spring-boot-annotation-actually-do-4e8i</link>
      <guid>https://dev.to/somenath_das_kol_86/what-a-spring-boot-annotation-actually-do-4e8i</guid>
      <description>&lt;h2&gt;
  
  
  A Spring Boot annotation is essentially a label or a form of metadata placed above a Java class to instruct the Spring Framework on what tasks to perform in the background.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  In the past, configuring the Spring Framework required writing large, complex XML files. Spring Boot has eliminated that hassle through the use of annotations. We simply place an annotation—prefixed with an '@' symbol—above a class, and the Spring Framework automatically handles tasks like object creation and management in the background.
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Here is a simple explanation of how these annotations actually work behind the scenes:
&lt;/h2&gt;

&lt;p&gt;How Annotations Work in the Background (Internal Working)&lt;br&gt;
When your Spring Boot application runs, three main events occur:-------&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Package Scanning (Component Scanning): Spring first scans all the classes in your project. It looks for classes that have Spring annotations applied to them.&lt;/li&gt;
&lt;li&gt;Reflection (Java Reflection): Using Java's Reflection API, Spring reads the annotation details within those classes at runtime.&lt;/li&gt;
&lt;li&gt;Bean Creation and Lifecycle Management: For any class where it finds an annotation, Spring automatically creates an object of that class (referred to as a "Bean" in Spring terminology) and stores it in its memory (the Application Context). It then manages the bean as needed.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;A real-world examples&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s look at examples using a few commonly used annotations to make the concept clearer:
&lt;/h2&gt;

&lt;p&gt;• @Component or @Service:&lt;br&gt;
• What you did: You placed @Service above a class. &lt;/p&gt;

&lt;h2&gt;
  
  
  • What Spring understood: "Okay, I need to create an object (Bean) of this class myself and store it in memory; the developer won't need to create the object using the 'new' keyword anymore."
&lt;/h2&gt;

&lt;p&gt;• @Autowired:&lt;br&gt;
• What you did: You placed this above a variable. &lt;/p&gt;

&lt;h2&gt;
  
  
  • What Spring understood: "The developer needs an object of another class inside this class. I will locate that object in memory and inject (connect) it here myself (Dependency Injection)."
&lt;/h2&gt;

&lt;p&gt;• @RestController:&lt;br&gt;
• What you did: Applied this to the controller class. &lt;/p&gt;

&lt;h2&gt;
  
  
  • What Spring understood: "This class will handle HTTP requests (such as GET, POST) and return JSON data directly to the browser or app."
&lt;/h2&gt;

&lt;p&gt;• @SpringBootApplication:&lt;br&gt;
• This is typically placed on the project's main class. It tells Spring: "Start the application from here, scan all surrounding packages, and automatically configure the necessary libraries."&lt;/p&gt;

&lt;p&gt;So, these are some examples of annotation .&lt;/p&gt;

&lt;p&gt;Hpoe your concept will get clear.............&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>java</category>
      <category>springboot</category>
    </item>
  </channel>
</rss>
