Using Dropwizard HealthCheck for Service Liveness and Readiness
Learn how to add a lightweight Dropwizard HealthCheck to monitor DB connectivity and expose it via the admin port for Kubernetes probes.
25 Aug 2026, 04:23 UTC

Problem: Needing a lightweight status endpoint
Many microservices running in Kubernetes or similar platforms need a quick way to tell the orchestrator whether the process is alive and ready to serve traffic. Adding a separate monitoring library can increase the dependency surface and startup time.
How Dropwizard HealthCheck works
Dropwizard includes a HealthCheck interface in the io.dropwizard.lifecycle package. By implementing the check() method and returning either Result.healthy() or Result.unhealthy(String message), the framework automatically registers the check under the admin port's /admin/healthcheck endpoint. All registered checks are invoked and their results aggregated into a JSON object.
Worked example: Database connectivity check
import io.dropwizard.lifecycle.HealthCheck;
import io.dropwizard.setup.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseHealthCheck extends HealthCheck {
private static final Logger LOG = LoggerFactory.getLogger(DatabaseHealthCheck.class);
private final String jdbcUrl;
private final String user;
private final String password;
public DatabaseHealthCheck(String jdbcUrl, String user, String password) {
this.jdbcUrl = jdbcUrl;
this.user = user;
this.password = password;
}
@Override
protected Result check() throws Exception {
try (Connection conn = DriverManager.getConnection(jdbcUrl, user, password)) {
// A lightweight query; adjust to your dialect if needed
try (var stmt = conn.createStatement()) {
stmt.executeQuery("SELECT 1");
}
return Result.healthy();
} catch (SQLException e) {
LOG.warn("Database health check failed", e);
return Result.unhealthy("Cannot connect to database: " + e.getMessage());
}
}
}
// In your Application subclass
@Override
public void run(Configuration config, Environment env) {
env.healthChecks().register("database", new DatabaseHealthCheck(
config.getDataSourceFactory().getUrl(),
config.getDataSourceFactory().getUser(),
config.getDataSourceFactory().getPassword()));
}
Trade-offs and limitations
- The health check runs on the admin thread pool. A long-running or blocking check can delay other admin endpoints such as metrics or thread dumps.
- Keep the check lightweight; avoid heavyweight operations like large data pulls or complex business logic.
- The endpoint returns a JSON map where each key is the check name and the value is an object with
healthy(boolean) and optionallymessage. This format is stable across Dropwizard 1.x and 2.x series.
Actionable closing
- Add a class that extends
HealthCheckand implements a fast, idempotent probe (e.g., a simple SQLSELECT 1). - Register the check in the
runmethod of your Dropwizard application viaenvironment.healthChecks().register. - Start the application and verify the endpoint:
curl http://localhost:8081/admin/healthcheck(admin port defaults to 8081 unless overridden). You should see JSON like{"database":{"healthy":true}}. - Point your Kubernetes liveness or readiness probe to this URL; adjust the
initialDelaySecondsandtimeoutSecondsto suit your environment.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.