DEV Community

ibrohim syarif
ibrohim syarif

Posted on

Circuit Breaker Pattern

Integrating with partners often got unexpected behavior due some isssue on their server that impact to our service performance. lets say the integration flow look like this

simple partner integration

If the partner responds successfully, our service forwards the response data to the client. Otherwise, if the partner returns an error, our service will relay the error message to the client. Similar to our server, the partner have maintenance or unexpected issue that make it inaccessible. When their server fails to respond, every request to their server will get not responding error and giving unnecessary waiting time, with huge traffic this issue very possible will cause our server to crash. So what should we do to prevent that happen?

Solution

The issue in this article isn't about the persistent errors from the partner but rather the additional response time caused by these errors, which could lead to our server crash (see this article https://dev.to/ibrohhm/crash-and-timeout-simulation-jbp). To solve this, we need add an another layer to manage the partner connection, acting as circuit breaker if the connection goes bad it will break the connection and return the request immediately without waiting for the partner response

the circuit breaker pattern have three states

  1. closed means the service allow to make connections
  2. half-open means the service allow to make connections with limited number
  3. open means the service not allow to make connections, it will return error immediately

this is detail curcuit breaker flow
circuit breaker flow

curcuit breaker allows us to control the partner connection effectively. By implement circuit breaker in our integration flow, we have no worries about the unexpected partner failure, it will cut the connection automatically and prevent our service from potential crashes due the unnecessary waiting times

Top comments (0)