GithubHelp home page GithubHelp logo

philoinc / cb2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from prognostikos/cb2

0.0 5.0 0.0 28 KB

Implementation of the circuit breaker pattern in Ruby, backed by Redis

License: MIT License

Ruby 100.00%

cb2's Introduction

CB2

Gem version Build Status

Implementation of the circuit breaker pattern in Ruby, backed by Redis.

Setup circuit breakers wrapping external service calls, be it HTTP, TCP, etc. When a service becomes unavailable the circuit breaker will open and quickly refuse any additional requests to it. After a specific window the breaker closes again, allowing calls to go through.

This pattern makes your application more resilient to third party failures, because it won't exhaust resources trying to make calls to an unresponsive service. This is particularly relevant to apps running on servers susceptible to request queuing, like Unicorn or Puma. It can also help the services you depend on to recover faster, by reducing the load on them.

CB2 tracks errors over a rolling window of time (size configurable), and opens after the error rate hits a certain percentage. The circuit stays open (rejecting calls) for another specified period, and then from there it goes to the half-open state: in which a succesful request will make it close again, but a failure will put it immediatelly back at the open state. Martin Fowler has a nice diagram to further explain these states.

Usage

Instantiate a circuit breaker:

breaker = CB2::Breaker.new(
  service: "aws"       # identify each circuit breaker individually
  duration: 60,        # keep track of errors over a 1 min window
  threshold: 5,        # open the circuit breaker when error rate is at 5%
  reenable_after: 600, # keep it open for 10 mins
  redis: Redis.new)    # redis connection it should use to keep state

Then wrap service calls with it:

breaker.run do
  some_api_request()
end

The breaker will open when that block raises enough exceptions to trigger the threshold. Handle these exceptions to react accordingly:

begin
  breaker.run do
    some_api_request()
  end
rescue CB2::BreakerOpen
  alternate_response() # fallback to cached data, or raise a user-friendly exception
end

Circuit breaker stub

CB2 can also run as a stub. Use it to aid testing, simulations and gradual rollouts:

breaker = CB2::Breaker.new(
  strategy: "stub",
  allow: true) # set it to false to always block requests

See also

This might be the only percent-based breaker in Ruby so far. For count-based breakers (eg: open after X exceptions) you can also check breaker_box and circuit_breaker.

Meta

Created by Pedro Belo.

cb2's People

Contributors

richardatphilo avatar

Watchers

 avatar  avatar  avatar  avatar  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.