Coder Social home page Coder Social logo

circuit-breaker's Introduction

Create a Circuit Breaker

Circuit Breaker Diagram From Martin Fowler:

The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all. Usually you'll also want some kind of monitor alert if the circuit breaker trips.

This simple circuit breaker avoids making the protected call when the circuit is open, but would need an external intervention to reset it when things are well again. This is a reasonable approach with electrical circuit breakers in buildings, but for software circuit breakers we can have the breaker itself detect if the underlying calls are working again. We can implement this self-resetting behavior by trying the protected call again after a suitable interval, and resetting the breaker should it succeed.

Creating this kind of breaker means adding a threshold for trying the reset and setting up a variable to hold the time of the last error. There is now a third state present - half open - meaning the circuit is ready to make a real call as trial to see if the problem is fixed. Asked to call in the half-open state results in a trial call, which will either reset the breaker if successful or restart the timeout if not. So, essentially:

  1. Make a thing that wraps an action.
  2. If that action fails n times, prevent further calls to that action.
  3. Wait a period of time (or manually close it).
  4. While open - calls immediately rejected.
  5. Perform the action again - if it succeeds - continued calls are permitted, otherwise - reset the clock.
  6. Optimise by adding the "half-open" state to tentatively try a single call.

Why is the Circuit Breaker Important?

Now - more than ever - many of our calls are remote calls to (often) HTTP services. There's many reasons why these can fail (infrastructure as well as software issues). Generally, most systems work well - with perhaps a few sprinkles of errors - but within tolerances. However, when they go down - they can go down hard. Particularly at scale this causes two main problems:

  1. Lots of calls to a clearly failing service - wasting resources, time and reputation.
  2. Servers may struggle to get back online and users contunally refresh/retry operations.

The Kata

This is a suggested progression. Feel free to deviate if you wish! ๐Ÿ˜Š

  1. Create a Circuit Breaker that simply has open/closed states, that must be manually closed once open. The tolerance should be configurable.
  2. Add automation of closing by adding a timer.
  3. Add the 'half-open' state to send only a single request, if that succeeds - we completely reset and close the breaker.
  4. Add real-world usage with a mock HTTP server, such as Sandbox.

Patterns

๐Ÿšง

  • Esp. BFFs!

circuit-breaker's People

Watchers

James Cloos avatar Polly avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.