Circuit Breaker Example
Heart Beat system in your applications - Why do you need this?
What is a Heart Beat system and why do you need it?
Heart Beat system track the health of some external/internal components/services that your application talks to.
If your application talks to some external service A and that service A happens to be down for X number of minutes/hours, then your application will be building up a backlog of connection because each connection now stay open longer because the timeout value is typically set to a value higher than an average response from this service. Over time your application could create a huge backlog to the point where the clients connecting to your application would experience slowness or failures. For a website with high traffic this could mean loss of thousands of dollars in minutes.
This is a where a Heart Beat system helps. Your Heart Beat system would track the health of this service A. Your application would check with the Heart Beat system if it's worth spending resources and even making a call to this service A. If the service A is down, your application with the help of Heart Beat system would not make unnecessary calls and consequently it would NOT build up a backlog of connections; that means there is not impact on your end customers which is what the business is all about.
There are two types of Heart Beat systems in my book:
Heart Beat systems that works independently of your application and pings external/internal services that your application talks to. It periodically checks the health of these services and informs the application.
Heart Beat systems that require your applications to feed it information/failures and then the Heart Beat system feeds back the decisions to your application when your application needs it. I like to compare this system to hybrid systems in hybrid cars which use the braking energy to feed the battery. It might be a perfect comparison but I like it :)
To demonstrate the concept of the 2nd type of Heart Beat system, I prepared a little web page with all the logic built into the Javascript of the page. The Javascript modules are separated into a module that emulates a client, a module that emulates a controller on web-server side, and a module that emulates an external service that your application on web server talks to.Â