Pandemic! Human race is now at stake. Flatten the curve. Whole world is running after this epidemic curve. Why it so important?

20200403 Flatten the curve animated GIF.gif
By RCraig09 - Own work, CC BY-SA 4.0, Link

Fight or Flight. Neither medicine nor vaccine for COVID-19  is out there. Naturally, second one would be the wisest choice.

Wash hand, disinfection, wear mask, keep distance, stay at home, social bubbles, lockdown - so many temporary strategies. But for what?

Idea here is don't fall sick. Delay the  peak number of infected people that health care system can handle at a time. Don't put more pressure on already stressed out healthcare system. Help administration to buy time to increase health care capacity ("raise the line") .

Why am I talking all these in the context of microservices? Probably, you got me . Just think microservice application as healthcare system and each person (who requires health care support) as request to that application then you'll find amazing similarities.

We may have experienced 1-2 pandemics in 100 years. But we see it frequently in IT application environment.  Therefore, it is very important to know how to handle pandemic in application.

Broadly, there are three different states of dealing with this epidemic. This is almost same across all countries (except Big Bro :-)).

  1. Open (Pre Covid-19 / Hopefully Post Covid-19)
  2. Locked Down (Everything absolutely closed)
  3. Slowly opening / Semi-open / half-open (Social bubbles, Stage 1, Stage 2, Stage 3 etc.)

Third state is very tricky. Wait, watch and take action. If situation continuously improves, then open  particular area / city / business otherwise close it immediately.

For our applications, we use to have Input Sanitization (Wash Hand),  Virus / Malware / Vulnerability scanner (Disinfection), Firewall / VPC / Security Group / ACL / Private Subnet etc. (Face Mask, Home Isolation, Social bubbles etc.). We also need something that will help us to continuously monitor and decide when not to pass request if underlying system is already stressed out.

Roughly, Circuit Breaker pattern helps to address this and make an application more resilient. It is partially works like an electric circuit breaker that protects electrical appliances from unexpected level of current flow at our home. It is absolutely essential and most important safety mechanism for our home. Similarly, Circuit Breaker pattern is also absolutely necessary for our modern software applications.  

Circuit Breaker pattern is implemented as finite state machine with 3 main states - "Closed", "Open" and "Half-Open". It monitors and aggregate outcome of calls in a rolling / sliding window. Once,  failures rate crosses the configured threshold, Circuit Breaker trips ("Closed" --> "Open") and all subsequent calls get rejected with immediate error or fallback response. After configured wait time, it goes to "Half-Open" state and allow configured number of calls to verify the backend health. If all calls are successful then it moves to "Closed" state, otherwise again goes back to "Open" state and restart the wait timer.

Project Concept Diagram

Two java implementations of Circuit Breaker pattern are very popular.
1. Netflix Hystrix:

Hystrix is no longer in active development, and is currently in maintenance mode.

Hystrix (at version 1.5.18) is stable enough to meet the needs of Netflix for our existing applications. Meanwhile, our focus has shifted towards more adaptive implementations that react to an application’s real time performance rather than pre-configured settings (for example, through adaptive concurrency limits). For the cases where something like Hystrix makes sense, we intend to continue using Hystrix for existing applications, and to leverage open and active projects like resilience4j for new internal projects.
Source: https://github.com/Netflix/Hystrix

2. Resilience4j

Resilience4j is a lightweight fault tolerance library inspired by Netflix Hystrix, but designed for Java 8 and functional programming. Lightweight, because the library only uses Vavr, which does not have any other external library dependencies. Netflix Hystrix, in contrast, has a compile dependency to Archaius which has many more external library dependencies such as Guava and Apache Commons Configuration.
Source: https://github.com/resilience4j/resilience4j

Personally, I prefer Resilience4j for my projects. Reason is obvious from above quotes. Please let me know in the comment what are you using in your projects?

Thanks for reading! :)

References