You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is increasingly important to build fault tolerant micro services. Fault tolerance is about leveraging different strategies to guide the execution and result of some logic. Retry policies, bulkheads, and circuit breakers are popular concepts in this area. They dictate whether and when executions should take place, and fallbacks offer an alternative result when an execution does not complete successfully.
Overview
Fault Tolerance provides developers with the following strategies for dealing with failure:
Timeout: Define a maximum duration for execution
Retry: Attempt execution again if it fails
Bulkhead: Limit concurrent execution so that failures in that area can’t overload the whole system
CircuitBreaker: Automatically fail fast when execution repeatedly fails
Fallback: Provide an alternative solution when execution fails
Fault Tolerance provides an annotation for each strategy which can be placed on the methods of CDI beans. When an annotated method is called, the call is intercepted and the corresponding fault tolerance strategies are applied to the execution of that method.
Documentation
For links to the latest maven artifacts, Javadoc and specification document, see the latest release.
Example
Apply the retry and fallback strategies to doWork(). It will be executed up to two additional times if if throws an exception. If all executions throw an exception, doWorkFallback() will be called and the result of that returned instead.
@ApplicationScopedpublicclassFaultToleranceBean {
@Retry(maxRetries = 2)
@Fallback(fallbackMethod = "doWorkFallback")
publicResultdoWork() {
returncallServiceA(); // This service usually works but sometimes// throws a RuntimeException
}
privateResultdoWorkFallback() {
returnResult.emptyResult();
}
}
From elsewhere, inject the FaultToleranceBean and call the method:
The annotation parameters can be configured via MicroProfile Config. For example, imagine you have the following code in your application:
packageorg.microprofile.readme;
@ApplicationScopedpublicclassFaultToleranceBean {
@Retry(maxRetries = 2)
publicResultdoWork() {
returncallServiceA(); // This service usually works but sometimes// throws a RuntimeException
}
}
At runtime, you can configure maxRetries to be 6 instead of 2 for this method by defining the config property org.microprofile.readme.FaultToleranceBean/doWork/Retry/maxRetries=6.
Alternatively, you can configure maxRetries to be 6 for all instances of Retry in your application by specifying the property Retry/maxRetries=6.