java
50 lines · 7 steps
A custom replica health check in Spring
A Spring Actuator HealthIndicator that probes a read replica and reports UP, DEGRADED, or DOWN based on latency.
Explained by
highlit
1package com.example.monitoring;
2
3import org.springframework.boot.actuate.health.Health;
4import org.springframework.boot.actuate.health.HealthIndicator;
5import org.springframework.stereotype.Component;
6
7import javax.sql.DataSource;
8import java.sql.Connection;
9import java.time.Duration;
10import java.time.Instant;
11
12@Component("replicaDatabase")
13public class ReplicaDatabaseHealthIndicator implements HealthIndicator {
14
15 private static final Duration SLOW_THRESHOLD = Duration.ofMillis(250);
16
17 private final DataSource replicaDataSource;
18
19 public ReplicaDatabaseHealthIndicator(DataSource replicaDataSource) {
20 this.replicaDataSource = replicaDataSource;
21 }
22
23 @Override
24 public Health health() {
25 Instant start = Instant.now();
26 try (Connection connection = replicaDataSource.getConnection()) {
27 boolean valid = connection.isValid((int) SLOW_THRESHOLD.toSeconds() + 1);
28 Duration elapsed = Duration.between(start, Instant.now());
29
30 if (!valid) {
31 return Health.down()
32 .withDetail("reason", "replica connection failed validation")
33 .build();
34 }
35
36 Health.Builder builder = elapsed.compareTo(SLOW_THRESHOLD) > 0
37 ? Health.status("DEGRADED")
38 : Health.up();
39
40 return builder
41 .withDetail("latencyMs", elapsed.toMillis())
42 .withDetail("catalog", connection.getCatalog())
43 .build();
44 } catch (Exception ex) {
45 return Health.down(ex)
46 .withDetail("reason", "unable to reach replica")
47 .build();
48 }
49 }
50}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Implementing HealthIndicator lets you fold custom probes into Actuator's aggregated health endpoint.
- 2A health check can report graded status, not just up or down, by distinguishing slow from failed.
- 3Try-with-resources guarantees the probe connection is returned even when validation or the query throws.
Related explainers
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
java
import java.util.ArrayDeque; import java.util.Deque; import java.util.Map;
Evaluating math expressions with two stacks
stacks
parsing
operator-precedence
Intermediate
9 steps
java
@Entity @Table(name = "orders") @SQLDelete(sql = "UPDATE orders SET deleted = true, deleted_at = now() WHERE id = ?") @Where(clause = "deleted = false")
Soft deletes with Hibernate in Spring
soft-delete
jpa
hibernate
Intermediate
9 steps
java
@GetMapping("/files/{id}") public ResponseEntity<StreamingResponseBody> download( @PathVariable String id, @RequestHeader(value = HttpHeaders.RANGE, required = false) String rangeHeader) throws IOException {
HTTP range requests in Spring
http-range
streaming
file-io
Advanced
10 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-custom-replica-health-check-in-spring-explained-java-8056/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.