Home > Castle Project, Patterns > CircuitBreaker Interceptor

CircuitBreaker Interceptor

An interesting pattern discussed in Release It!, Design and Deploy Production-Ready Software, is the CircuitBreaker pattern. I was thinking about implementing it in C# but well, as often, someone took care of it already (thus less work for me). :)

Tim Ross has a first post on his blog where he describes the CircuitBreaker and his implementation. In the second part, he introduces several improvements such as a ServiceLevel variable and the possibility to ignore certain exception types.

Although a very nice implementation, I wasn't thinking about a class for my CircuitBreaker implementation … but about an Interceptor.

Indeed, when designing a system built of loosely-coupled components using an IoC container, you will often add interceptors at strategic places (integration points with external systems) to catch failures, log statistics, … So, it makes sense to make the CircuitBreaker an interceptor that can be applied to those critical parts of your application in order to fail-fast in the event of repeated failures & relieve the external system that is under stress.

Here a quick implementation of Tim's class as an interceptor (using Castle Windsor IoC container) … the test code is dumb, but I guess you'll get the point. ;)

CircuitBreakerInterceptor.rar

Note: Instead of porting the whole code into the Interceptor class, it's also possible to simply get a CircuitBreaker object injected by he container into the Interceptor (interceptors are just a special kind of components), and the Interceptor would delegate the invocation call to the CircuitBreaker.

Btw, for those who don’t know Castle Windsor container well yet (never too late to learn something), this small sample also demonstrate the ease of using & configuring the container (as well as creating interceptors).

  1. Rinat Abdullin
    December 1st, 2008 at 21:09 | #1

    Hi, I’ve just posted on yet another implementation of this pattern, if you are interested.

    It is thread-safe, allows for simple code inlining and follows the idea from the StorageClient (that’s the library from the Windows Azure).

    Her’s the post if your are interested http://rabdullin.com/journal/2008/12/1/net-exception-handling-action-policies-application-block.html

    Best regards,
    Rinat Abdullin

  1. No trackbacks yet.