Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions health-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: Health Check Pattern
category: Performance
language: en
tag:
- Microservices
- Resilience
- Observability
---

# Health Check Pattern

## Also known as
Health Monitoring, Service Health Check

## Intent
To ensure the stability and resilience of services in a microservices architecture by providing a way to monitor and diagnose their health.

## Explanation
In microservices architecture, it's critical to continuously check the health of individual services. The Health Check Pattern is a mechanism for microservices to expose their health status. This pattern is implemented by including a health check endpoint in microservices that returns the service's current state. This is vital for maintaining system resilience and operational readiness.

## Class Diagram
![alt text](./etc/health-check.png "Health Check")

## Applicability
Use the Health Check Pattern when:
- You have an application composed of multiple services and need to monitor the health of each service individually.
- You want to implement automatic service recovery or replacement based on health status.
- You are employing orchestration or automation tools that rely on health checks to manage service instances.

## Tutorials
- Implementing Health Checks in Java using Spring Boot Actuator.

## Known Uses
- Kubernetes Liveness and Readiness Probes
- AWS Elastic Load Balancing Health Checks
- Spring Boot Actuator

## Consequences
**Pros:**
- Enhances the fault tolerance of the system by detecting failures and enabling quick recovery.
- Improves the visibility of system health for operational monitoring and alerting.

**Cons:**
- Adds complexity to service implementation.
- Requires a strategy to handle cascading failures when dependent services are unhealthy.

## Related Patterns
- Circuit Breaker
- Retry Pattern
- Timeout Pattern

## Credits
Inspired by the Health Check API pattern from [microservices.io](https://microservices.io/patterns/observability/health-check-api.html) and the issue [#2695](https://github.com/iluwatar/java-design-patterns/issues/2695) on iluwatar's Java design patterns repository.
Binary file added health-check/etc/health-check.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions health-check/etc/health-check.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
@startuml

!theme plain
top to bottom direction
skinparam linetype ortho

class App {
+ App():
+ main(String[]): void
}
class AsynchronousHealthChecker {
+ AsynchronousHealthChecker():
+ performCheck(Supplier<Health>, long): CompletableFuture<Health>
- awaitTerminationWithTimeout(): boolean
+ shutdown(): void
}
class CpuHealthIndicator {
+ CpuHealthIndicator():
- processCpuLoadThreshold: double
- systemCpuLoadThreshold: double
- loadAverageThreshold: double
- osBean: OperatingSystemMXBean
- defaultWarningMessage: String
+ init(): void
+ health(): Health
defaultWarningMessage: String
osBean: OperatingSystemMXBean
loadAverageThreshold: double
processCpuLoadThreshold: double
systemCpuLoadThreshold: double
}
class CustomHealthIndicator {
+ CustomHealthIndicator(AsynchronousHealthChecker, CacheManager, HealthCheckRepository):
+ evictHealthCache(): void
- check(): Health
+ health(): Health
}
class DatabaseTransactionHealthIndicator {
+ DatabaseTransactionHealthIndicator(HealthCheckRepository, AsynchronousHealthChecker, RetryTemplate):
- retryTemplate: RetryTemplate
- healthCheckRepository: HealthCheckRepository
- timeoutInSeconds: long
- asynchronousHealthChecker: AsynchronousHealthChecker
+ health(): Health
timeoutInSeconds: long
retryTemplate: RetryTemplate
healthCheckRepository: HealthCheckRepository
asynchronousHealthChecker: AsynchronousHealthChecker
}
class GarbageCollectionHealthIndicator {
+ GarbageCollectionHealthIndicator():
- memoryUsageThreshold: double
+ health(): Health
memoryPoolMxBeans: List<MemoryPoolMXBean>
garbageCollectorMxBeans: List<GarbageCollectorMXBean>
memoryUsageThreshold: double
}
class HealthCheck {
+ HealthCheck():
- status: String
- id: Integer
+ equals(Object): boolean
# canEqual(Object): boolean
+ hashCode(): int
+ toString(): String
id: Integer
status: String
}
class HealthCheckRepository {
+ HealthCheckRepository():
+ performTestTransaction(): void
+ checkHealth(): Integer
}
class MemoryHealthIndicator {
+ MemoryHealthIndicator(AsynchronousHealthChecker):
+ checkMemory(): Health
+ health(): Health
}
class RetryConfig {
+ RetryConfig():
+ retryTemplate(): RetryTemplate
}

CustomHealthIndicator "1" *-[#595959,plain]-> "healthChecker\n1" AsynchronousHealthChecker
CustomHealthIndicator "1" *-[#595959,plain]-> "healthCheckRepository\n1" HealthCheckRepository
DatabaseTransactionHealthIndicator "1" *-[#595959,plain]-> "asynchronousHealthChecker\n1" AsynchronousHealthChecker
DatabaseTransactionHealthIndicator "1" *-[#595959,plain]-> "healthCheckRepository\n1" HealthCheckRepository
HealthCheckRepository -[#595959,dashed]-> HealthCheck : "«create»"
MemoryHealthIndicator "1" *-[#595959,plain]-> "asynchronousHealthChecker\n1" AsynchronousHealthChecker
@enduml
143 changes: 143 additions & 0 deletions health-check/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).

The MIT License
Copyright © 2014-2022 Ilkka Seppälä

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
</parent>

<artifactId>health-check</artifactId>

<dependencies>
<!-- Spring Boot Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Boot Actuator for health check -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Spring Boot Data JPA for database access -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Testing Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Spring Retry for retrying failed database operations -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>


<!-- JUnit Jupiter Engine for testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<!-- Mockito for mocking in tests -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<!-- H2 for mocking database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>


</dependencies>


<build>
<plugins>
<!-- Maven Assembly Plugin for creating a single distributable JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- Define the main class for the executable JAR -->
<mainClass>com.iluwatar.healthcheck.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>

<!-- Other plugins as needed -->

</plugins>
</build>
</project>
26 changes: 26 additions & 0 deletions health-check/src/main/java/com/iluwatar/health/check/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.iluwatar.health.check;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
* This application provides health check APIs for various aspects of the microservice architecture,
* including database transactions, garbage collection, and overall system health. These health
* checks are essential for monitoring the health and performance of the microservices and ensuring
* their availability and responsiveness. For more information about health checks and their role in
* microservice architectures, please refer to: [Microservices Health Checks
* API]('https://microservices.io/patterns/observability/health-check-api.html').
*
* @author ydoksanbir
*/
@EnableCaching
@EnableScheduling
@SpringBootApplication
public class App {
/** Program entry point. */
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Loading