City Library — An advanced guide to Circuit Breakers in Kotlin

João Esperancinha
ITNEXT
Published in
15 min readOct 26, 2021

--

I’ve always been very interested in circuit-breakers and how do they work in Software Architectures. I’ve also worked on projects where one of the implementations of such design patterns was used. Namely, for online large distribution stores, many companies love to use Hystrix. However, Hystrix is reaching the end-of-the-road and a new replacement is available called resilience4j. Regardless, I’ve seen many ways in which Hystrix has been used. The possibilities are quite extensive, but in practice, in my professional life, I’ve seen that not even half of the potential of a Circuit-Breaker design pattern implementation is used. Like Resiliece4J, there are many different implementations of this software design pattern available like FailSafe, GoBreaker, Circuit-Breaker-For-Scala, and many others.

It’s always good to remind ourselves that the idea of Circuit-Breaker has existed since the 19th Century and back then it was related to how a switch would break a circuit should the electrical current increase up to unacceptable levels. Fast-forward to 2017, Michael Nygard publishes Release IT! Design and Deploy Production-Ready Software (Pragmatic Programmers) where he brings the idea of Circuit-Breakers design pattern to software development.

In this article, I’m assuming that you already have an idea of what circuit-breakers really are and what this design pattern is fundamentally about. This design pattern allows many sorts of types of configuration, and we are going to have a look at the most important ones of them as we go along. I’m also assuming that you are quite familiar with how Spring works and also that you have some idea of how the performance test tool Locust works. It is also important to have a general idea of how Reactive programming is used in Spring WebFlux. The implementation we are going to see is located on GitHub.

1. Case

For demonstration purposes, we’ll make a city library!. We are essentially going to provide one or more books to a library and just provide them to users online for reading purposes. Before we actually build our library’s software, we are right off-the-bat concerned if the library can live up to resilient expectations. As many users are going to use the library, it must be there constantly providing to…

--

--

Writer for

Kotlin, OCP JSE 11, OCP JSE 8, OCP JEE 7, VMA Spring Professional #java #kotlin #scala #jvm #kafka #flux #reactive #spring #springmvc #springwebflux #coroutines