Friday, 19 December 2014

Circuit Breaker - 3

Here I will try to describe how easy is to implement a circuit breaker (CB) in your system to have a more stable software environment. First we have to choose the architecture we want to apply the CB on it. What about have it as a separate module either as a single binary or just as a piece of code which works in a binary with other parts.

Sample software circuit breaker.
Look at the diagram, the CB we are going to build is going to be something like it. The component we are going to build has, at least, these parts:

  • Input: An interface module to receive data from one component.
  • Output: An interface module to transmit data to the next component.
  • Control: A central control unit which manages passing or dropping or disconnecting the flow and/or flow path.
Now let us see how we can have this CB to do what we want, suppose we want it to drop more than 1000 data packet per second and we want the CB in a simple single thread mode. The following shows a pseudo code for what we have in mind:



CB cb;
cb.init();
cb.control.counter.set(0);
long lastSecond = getUnixTimestamp(); 

while (true) {
  DataItem dataItem = cb.input.pop();

  if (lastSecond != getUnixTimestamp()) {
    lastSecond = getUnixTimestamp();
    cb.control.counter.set(0);
  }
  cb.control.counter.increase();

  if (cb.control.counter.getCurrent() < 1000) {
    cb.output.push(dataItem);
  }  
}


The above code is clear and is exactly what we wanted to do. We read data from the input, if the current period of packet counting is passed we just reset the counter then we increase the counter. Now it is time to forward the data, we just forward it if the counter shows less than the threshold. 

No comments:

Post a comment