What is a Circuit Breaker pattern and how does Resilience4j implement it in Spring Boot? सर्किट ब्रेकर पैटर्न क्या है और Resilience4j इसे स्प्रिंग बूट में कैसे इम्प्लीमेंट करता है?
The Circuit Breaker pattern prevents a failing downstream service from cascading failure across the whole system: when calls to a dependency fail repeatedly past a threshold, the circuit "opens" and further calls fail fast (or fall back to a default) without even attempting the network call, giving the failing service time to recover instead of piling up more load and threads on it.
Resilience4j is the modern standard for this in Spring Boot (replacing the now-deprecated Netflix Hystrix), used via @CircuitBreaker with a configured failure-rate threshold, wait duration in the open state, and a fallback method that's invoked when the circuit is open or the call fails.
@CircuitBreaker(name = "inventoryService", fallbackMethod = "fallback")
public Inventory checkStock(String productId) {
return inventoryClient.getStock(productId);
}
public Inventory fallback(String productId, Throwable t) {
return new Inventory(productId, 0, "Service unavailable, showing default");
}सर्किट ब्रेकर पैटर्न किसी विफल डाउनस्ट्रीम सर्विस को पूरे सिस्टम में विफलता फैलाने से रोकता है: जब किसी डिपेंडेंसी की कॉल्स एक सीमा से अधिक बार बार-बार विफल होती हैं, तो सर्किट "खुल" जाता है और आगे की कॉल्स नेटवर्क कॉल का प्रयास किए बिना ही तुरंत विफल हो जाती हैं (या डिफ़ॉल्ट पर वापस जाती हैं)।
Resilience4j इसके लिए स्प्रिंग बूट में आधुनिक मानक है (अब पुराना पड़ चुके Netflix Hystrix की जगह), जिसे @CircuitBreaker के ज़रिए उपयोग किया जाता है, जिसमें कॉन्फ़िगर्ड फेलियर-रेट थ्रेशोल्ड और सर्किट खुले होने पर इनवोक होने वाला फॉलबैक मेथड शामिल होता है।
@CircuitBreaker(name = "inventoryService", fallbackMethod = "fallback")
public Inventory checkStock(String productId) {
return inventoryClient.getStock(productId);
}
public Inventory fallback(String productId, Throwable t) {
return new Inventory(productId, 0, "Service unavailable");
}Was this answer clear?