Win $20,000. Help build the future of education. Answer the call. Learn more

IBM Developer Blog

Follow the latest happenings with IBM Developer and stay in the know.

How to get started using the Swift CircuitBreaker library.

What is the Circuit Breaker design pattern?

The CircuitBreaker pattern embodies the idea of a traditional circuit. If the circuit is closed all electricity is flowing as expected, but when a breaker is tripped, the electricity will cease to flow.

In the CircuitBreaker pattern there are three states:

  • Closed
  • Open
  • HalfOpen


If the state is Closed, the breaker allows all incoming requests to flow as normal. The developer specifies various configurations, such as, max number of failures, how long before a request is marked as a timeout, and how long to wait to reset the timeout state.

Once the max failures are reached, the breaker is then moved to Open state, where all incoming requests are set to fail fast or not process, at the same time the reset timer is kicked off. This continues until the reset timeout is reached and the breaker is moved to HalfOpen state. The first request while in HalfOpen state is attempted, depending on if the request is successful or not, the breaker will be moved to Closed or Open state, resetting the cycle.

It is also common for Circuit Breaker libraries to collect statistics of the application endpoints you are circuit breaking. Tracking information including total requests, failed responses, successful requests, average response time, and more. This allows for the logging of statistical snapshots of your overall application reliability.

Why would I use the CircuitBreaker library in my application?

The goal of the Circuit Breaker design pattern is to increase application reliability, improve response time, and prevent the application from making constant failing requests. Developers tune the application logic to proactively react to endpoint failures and timeouts. Application users should see increased flow and response time of the application, especially during endpoint failures or timeouts. Keeping the application from getting bogged down by timeouts, or unforeseen failures, improves significantly the user experience and prevents the system from consuming resources for executing a function or a request that more than likely will fail.

The CircuitBreaker library is great for use in micro-service based applications, highly distributed systems, and RESTful applications. Another optional feature of the framework is that it allows for bulkheading, which limits the number of concurrent requests that can be made at one time, further increasing stability.

How do I use the CircuitBreaker package in my application?

The CircuitBreaker library for Swift is now available for use in your application. Configure a breaker for each desired endpoint or function in your application you wish to circuit break and track the statistics. Use the Swift Package Manager to easily install and build the package within your application. For specific details on basic and advanced usage and configuration of the CircuitBreaker library visit the GitHub project at: IBM-Swift/CircuitBreaker.