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 class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
java
public class SortedListMerger { public static int[] merge(int[] a, int[] b) { int[] result = new int[a.length + b.length];
Merging two sorted arrays in Java
two-pointers
merging
arrays
Beginner
6 steps
java
import java.util.ArrayDeque; import java.util.Deque; public final class RollingAverage {
A rolling average over a sliding window
sliding-window
running-sum
deque
Intermediate
7 steps
java
@Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = StrongPasswordValidator.class) @Documented
Building a custom @StrongPassword validator in Spring
bean-validation
annotations
regex
Intermediate
7 steps
java
@Component public class JwtAuthenticationFilter extends OncePerRequestFilter { private final JwtTokenProvider tokenProvider;
How a JWT auth filter works in Spring
authentication
jwt
servlet-filter
Intermediate
8 steps
java
@Entity @Table(name = "accounts") public class Account {
Optimistic locking with @Version in Spring
optimistic-locking
jpa
concurrency
Advanced
7 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.