# Circuit breakers

### How Circuit breaker are used in distributed systems. Plus the five most important algorithms design techniques.

Hi Friends,

Welcome to the 21th issue of the Polymathic Engineer newsletter. This week I moved with my family to Sicily to visit my wife parents during the school break. I will still work from here, but at least I can enjoy the sun, the see and the warm temperatures during the breaks.

Today we will talk about:

Circuit breakers in distributed systems

Algorithms design methods

Coding challenge

Interesting tweets

### Circuit breakers

Circuit breakers are essential to any distributed system because they increase its reliability and resilience to failures.

The word circuit breaker comes from the electrical engineering and hardware world.

Indeed, a circuit breaker is a device protecting a circuit from damage caused by excess current. When the current becomes too high, the breaker interrupts the flow of electricity.

In software systems, circuit breakers play a similar role. When a component becomes unavailable or has issues, the breaker interrupts the traffic flow. This avoids failures having a cascading effect, leading to widespread outages.

Suppose an application has a service that must fetch information from a 3rd party API. The app can get unresponsive if the API experiences high latency or downtime.

A circuit breaker avoids this by monitoring the number of failed requests and stopping calling the API.

So, circuit breakers monitor the performance of a component and "trip" if it starts to have issues. The criteria for tripping depends on the component, but some common ones are high error rates, slow response times, or a vast number of timeouts.

The key to deciding the criteria is considering how an upstream service can affect the system. There are different ways to handle a trip. Some common ones are:

returning an error message

failing over to a backup component

implementing a graceful degradation

Usually, a circuit breaker remains tripped for a specific time. After this time, the breaker resets, and the traffic flows again. This period allows the affected component to recover, avoiding repeated trips in case of transient issues.

The breaker can reset according to parameters like a predefined amount of time, health checks on the upstream service, or metrics depending on the possible trip causes. Unfortunately, the last criterion is complex to implement because covering all the scenarios is hard.

Designing and implementing circuit breakers is not easy, but there are some guidelines to follow:

set the tripping thresholds considering both high and low load scenarios

thoroughly test the breakers to be sure that it trips and resets at the correct times

monitor the number of times the breaker has tripped and the time before resetting to detect any potential issues

do the regular maintenance of the circuit breakers, updating the tripping thresholds or the trip handling when necessary

### Algorithm design methods

There are many techniques you can apply to design algorithms. Here are the 5 most popular one.

##### Brute Force

It's the most basic approach:

• Enumerate all possible candidates for the solution systematically

• Checks for each candidate if the problem's statement is satisfied

• Find always the optimal solution, but it doesn't scale with the input size

##### Divide and Conquer

It is a top-down approach and requires 3 steps:

• Divide the original problem into a set of subproblems

• Solve each subproblem individually and recursively

• Combine the solution of the subproblems into a solution to the original problem

##### Greedy

It's an approach to solving optimization problems:

• The goal is to maximize or minimize a value (or objective)

• At each step, makes a choice looking best to optimize the objective

• Not always produce the optimal solution, but usually a very close one

##### Dynamic Programming

It is a bottom-up approach:

• Solve all possible small problems and combine them to get the solution for a bigger problem

• Helpful when subproblems overlap and are exponentially large in number

• Often applicable to optimization problems

##### Backtracking

It is a depth-first search on the possible solutions to a problem. If a solution doesn't work during the search:

• backtrack to the point of choice where different alternatives exist

• try the next alternative

### Coding challenge

The coding challenge for the next week is Maximum Difference Between Node and Ancestor.

Given a binary tree, the problem asks to find the maximum value v for which nodes a and b exist where v = |a.val - b.val| and a is an ancestor of b.

Consider a generic node in the tree and denote its value with v. The maximum difference for that node is the maximum between the absolute difference of v and an ancestor's minimum/maximum value.

While traversing the tree using DFS, keeping track of the minimum/maximum value of an ancestor for each node is necessary. Returning the correct answer with this information is trivial.

Here is a possible implementation of the algorithm:

The time complexity is O(N) time to traverse all nodes, and the space complexity is O(N) to store the recursion stack.

The coding challenge for the next week is Boats to save people.

### Interesting Tweets

Multitasking is often overrated. It’s hard to get the momentum continuously switching between tasks.

Everybody has their own life and their path. So, friendships and relationships may hold for a while. But it's also possible that they come back when you less expect. True friendships stay forever.