Enterprise Java

Circuit Breaker Pattern in Apache Camel

Camel is very often used in distributed environments for accessing remote resources. Remote services may fail for various reasons and periods. For services that are temporarily unavailable and recoverable after short period of time, a retry strategy may help. But some services can fail or hang for longer period of time making the calling application unresponsive and slow. A good strategy to prevent from cascading failures and exhaustion of critical resources is the Circuit Breaker pattern described by Michael Nygard in the Release It! book.
Circuit Breaker is a stateful pattern that wraps the failure-prone resource and monitors for errors. Initially the Circuit Breaker is in closed state and passes all calls to the wrapped resource. When the failures reaches a certain threshold, the circuit moves to open state where it returns error to the caller without actually calling the wrapped resource. This prevents from overloading the already failing resource. While at this state, we need a mechanism to detect whether the failures are over and start calling the protected resource. This is where the third state called half-open comes into play. This state is reached after a certain time following the last failure. At this state, the calls are passed through to the protected resource, but the result of the call is important. If the call is successful, it is assumed that the protected resource has recovered and the circuit is moved into closed state, and if the call fails, the timeout is reset, and the circuit is moved back to open state where all calls are rejected. Here is the state diagram of Circuit Breaker from Martin Fowler’s post:

state

How Circuit Breaker is implemented in Camel?

Circuit Breaker is available in the latest snapshot version of Camel as a Load balancer policy.
Camel Load Balancer already has policies for Round Robin, Random, Failover, etc. and now also CircuiBreaker policy.

Here is an example load balancer that uses Circuit Breaker policy with threshold of 2 errors and halfOpenAfter timeout of 1 second. Notice also that this policy applies only to errors caused by MyCustomException

new RouteBuilder() {
    public void configure() {
        from("direct:start").loadBalance()
            .circuitBreaker(2, 1000L, MyCustomException.class)
                .to("mock:result");
    }
};

And here is the same example using Spring XML DSL:

<route>
    <from uri="direct:start"/>
    <loadBalance>
        <circuitBreaker threshold="2" halfOpenAfter="1000">
            <exception>MyCustomException</exception>
        </circuitBreaker>
        <to uri="mock:result"/>
    </loadBalance>
</route>
Reference: Circuit Breaker Pattern in Apache Camel from our JCG partner Bilgin Ibryam at the OFBIZian blog.

Bilgin Ibryam

Bilgin is a software craftsman based in London, integration architect at Red Hat, Apache Camel and Apache OFBiz committer. He is an open source fanatic, passionate about distributed systems, messaging, enterprise integration patterns, and application integration. He is also the author of Camel Design Patterns and Instant Apache Camel Message Routing books.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Roy
Roy
9 years ago

“Resource is recovered and the circuit moves into ”
Closed state, is it?

Bilgin
9 years ago

Yes, that’s correct. There is a typo in this version of the article, but it is fixed in the original.

Byron Kiourzoglou
9 years ago

Hello Roy,

Thanks for the info. We have corrected our version of the article also!

Best regards
Byron

Back to top button