why health-check service is required for Load Balancer?

Let us understand, why health-check service API is required for any LB (Load Balancer)? Checking of health, whether underlying node/application/webServer is up and running or not. Health-Check is most important and  basic things, when you add your application server or web server under LB( Load Balancer) (LB can be either SLB- Software Load Balancer or HLB – Hardware Load Balancer).

1. overview of health-check service

First thing first, we need to understand what is health-check in the context of Load Balancer for distributing of your application request load to multiple nodes (machine) under your configured LB (Load Balancer). Whenever, you need to distribute your application request traffic to multiple nodes, you have to use either SLB (Software Load Balancer) or HLB (Hardware Load Balancer). Here, I’m not discussing about SLB or HLB, we will discuss about these in details in another post.

Distributing of request/traffic of your application by configuring of LB and is one of the way of scaling out of your application. Here the term application can be of 1. Web Application, 2. enterprise application, 3. distributed application, 4. distributed database or any another node which has capability of either computing, processing, or storing any data etc..

2. objectives of health-check service

Why we required health-check service API and what is its objective? Without defining any objective, neither we do anything nor we achieve anything, so let us discuss the objectives of health-check service for the Load Balancer. There are three objectives of health-check service API, which is very important to understand. Objectives of health check service are as follows:

  1. Reachable: Your endpoint API will be designed in such a way, so that requested node can be reachable. And this can be can be configured by configuring index page of your application like https://api.mysoftkey.com/cs/ . This function is only referring about your node is reachable but not talking about available and functional.
  2. Available: This functionalilty talk about your none is available but not confirming about whether your node is functional or not. if your node is only available but function then you request will be lost, not work as per your business flow.
  3. Functional: If we reach on this point , it means your node is Reachable , Available and functional too. It means, your helth-check service API is working properly and ELB will not misbehaves.

Your health-check rest API must meet all three objectives in order to add application under LB

3. different ways of checking health

  1. Can be configured for HTTP, TCP, HTTPS, SSL
  2. Ping port specifies the port for the health check
  3. Ping path specifies the path to check, e.g. /index.html
  4. Can define timeout, interval, unhealthy threshold, healthy threshold

3. supported health check protocols

There are multiple protocols available nowadays to check, whether your web application is available, reachable and functional or not. There are so many of protocols which supports this.

4. implementation of health-check service API

Controller class : HealthCheckController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@RestController
public class HealthCheckController {
 
 @Autowired
 private HealthCheckService healthCheckService;
 
/**
 * This service consumed by Nagios monitoring tool.
 * url: https//api.mysoftkey.com/cs/healthStatus
 * 
 * @return
 */
@RequestMapping(value = "/healthStatus", method = RequestMethod.GET)
public ResponseEntity<map<string, object="">&gt; healthStatus() {
  Map<string, object=""> map = new HashMap<string, object="">(2);
  try {
   String upOrDown = healthCheckService.getHealthStatus()
   if("up".equals(upOrDown)) {
     map.put("health", upOrDown);
     map.put("ok", 1);
     return new ResponseEntity<map<string, object="">&gt;(map, HttpStatus.OK);
   } else {
    map.put("health", upOrDown);
    map.put("ok", o);
    return new ResponseEntity<map<string, object="">&gt;(map, HttpStatus.SERVICE_UNAVAILABLE);
  }
} catch (Exception e) {
  map.put("error", e.getMessage());
  map.put("ok", 0);
return new ResponseEntity<map<string, object="">&gt; map,HttpStatus.SERVICE_UNAVAILABLE);
 }
}
}</map<string,></map<string,></map<string,></string,></string,></map<string,>

DAO class: HealthCheckDaoImpl.java

1
2
3
4
5
6
7
8
9
10
11
@Repository("healthCheckDao")
public class HealthCheckDaoImpl implements HealthCheckDao
 
@Override
public String getHealthStatus() throws DataAccessException {
	//jdbcTemplate.setQueryTimeout(100);
	//int count = jdbcTemplate.queryForInt("select 1 ");
	int count = jdbcTemplate.queryForObject("select 1 ", Integer.class);
	String json =  count == 1 ? "up" : "down";
	return json;
}

output of service with HTTP status code 200 Ok

1
2
3
4
{
 "health":"up",
 "ok":1
}

not included all the service and DAO class here, for clarity purpose. if service returns httpStatusCode 200 Ok , it means our api is up and running which means API is reachable, available and functional.

5. Key points of health-check

API must check all the three aspect of health-check objectives as: Reachable, Available and Functional.

  • If rest endpoint API is Reachable, it means Application is reachable but not guaranteed about Available and Functional or both.
  • If rest endpoint API is Available, it means Reachable and Available both but its not guarantee about Functional.
  • if rest endpoint API is Functional, it means application is reachable and available too.
  • for functional write a query which execute on database engine and return something for this best query is “select 1” which executes on db engine and return 1 in resultset.
  • Always return 200 ok, if db query return 1 ( up and running) else return 4XX or 5XX series http status query so that LB can identified whether application is up and running or not.
  • if your API will not execute query in db engine by “select 1” it means your application is may or may not up and running. Thus your application may or may not be in ready state to received the request and which full filled the request.
  • So implements a service API which must check about database up and running

You comments are welcome to improve this post, Happy learning 🙂

2 thoughts on “why health-check service is required for Load Balancer?

Leave a Reply

Your email address will not be published. Required fields are marked *