<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
    <version>3.1.1</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    <version>4.1.1</version>
</dependency>
 
 

@Service
public class MyService {

    @Autowired
    private RestTemplate restTemplate;

    @CircuitBreaker(name = "backendService", fallbackMethod = "fallback")
    public String backendService() {
        // Call to some external service
        return
restTemplate.getForObject("http://your-backend-service-url", String.class);
    }

    public String fallback(Throwable throwable) {
        return "Fallback response";
    }
}

 

What problems does it solve?

Network timeout

 

Service unavailable

 

Slow response

 

Too many requests

 

Repeated failures

 

Temporary outage

 

States

Closed
 
 
Open
 
 
Half Open
 
 
Closed

 

 Rate Limiter

Prevent abuse.

Suppose

One customer sends

1000 requests/sec

Allowed

10 requests/sec

Reject the rest.

 

Bulkhead

One of my favorites.

Suppose

Payment

becomes slow.

Without Bulkhead

200 Threads

 

 

Payment

 

 

All threads blocked

Entire application freezes.

 

Bulkhead

Payment

 

 

20 threads

 

Only

The remaining

180 threads

continue serving

Inventory

Customer

Orders

 

 

Time Limiter

Instead of waiting

30 seconds

Stop after

2 seconds

Return

Fallback.

 

Cache

Frequently requested data

Cache

Avoid calling slow service repeatedly.

 

 

Example

@Retry(name="redis")
@CircuitBreaker(name="redis")
@TimeLimiter(name="redis")
@Bulkhead(name="redis")

 

 

Resilience4j is inside your application.

Spring Boot
Resilience4j
Redis
Kafka

Kubernetes doesn't know.

It only sees pod