We know what a circuit breaker is in theory and how it behaves. Let us try to implement it in our codebase now. There are two popular libraries out there that provide this functionality at the time of writing—gobreaker and hystrix-go. We will use the latter because Hystrix has a dashboard functionality for us to explore on our own, which is very useful for monitoring overall health. Currently, we are only calling fakerApi, but when our service has more dependencies, this dashboard utility will truly shine.

Adding the circuit breaker

To successfully add Hystrix in our code, we will first need to ensure we set up a new Hystrix configuration for every network client we use and then use its functionality when we make our API call.

Create a Hystrix command

We already have the mechanism to create one client for each of our dependencies with unique configurations. We will leverage that same code also to create a Hystrix configuration of the same name.

Get hands-on with 1400+ tech skills courses.