Circuit Breaker Design Pattern

Your overall application’s performance might be strongly dependent on how the third-party services you integrated with are performing.

If some remote resource is currently experiencing performance troubles, your calls to this service could be hanged out waiting for a response until a timeout is occurred. Because of that issue the throughput of your service will be drastically decreased, which definitely will impact your end users’ experience.

In this situation a Circuit Breaker mechanism could prevent your system from such cascade performance impact.

Circuit Breaker Design Pattern Overview

The main idea here is to make your calls fail-fast if the remote resource’s calling ended up with a critical amount of failed attempts.
In this case the Circuit Breaker mechanism will be switched to open state, which means outage is in progress. Since that there is no sense to keep calling the problematic resource, and an alternative flow could be used while outage is happening:

Circuit Breaker Design Pattern Overview
Circuit Breaker Design Pattern Overview

The Circuit Breaker will switch back to closed state after defined timeout. So, new attempts to reach out the resource will be taken.

Circuit Breaker Design Pattern Flow Diagram
Circuit Breaker Design Pattern Flow Diagram

Implementation Details

There is a simple implementation of the described Circuit Breaker design pattern. Basically, we’re keeping track the following information about failed attempts:

The following basic API makes the usage of our Circuit Breaker really simple and straightforward:

Here is an example with 5 failed calls threshold allowed within a one minute time frame. As soon as the threshold is reached, an alternative flow is being processed for the next 5 minutes:

SimpleCircuitBreaker circuitBreaker = new SimpleCircuitBreaker(5, 300000L, 60000L);
if (!circuitBreaker.isOutageInProgress()) {
    try {
        // make a call to the resource

    } catch (RuntimeException e) {
        // process alternatives
} else {
    // process alternatives

Full implementation of the could be found in my GitHub repository.


The Circuit Breaker mechanism could help to protect your application from cascading failures and to provide a fallback behavior for potentially failing calls to external resources.

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora