Health Check Endpoints Setup
Health check endpoints verify application health. Load balancers, Kubernetes, monitoring systems query them to exclude unhealthy instances from rotation.
Two check types
Liveness — is the process alive. If not, container restarts. Should respond even with degraded dependencies.
Readiness — is it ready to accept traffic. If not, balancer doesn't route requests. Checks DB, cache, external services.
Laravel: health check endpoints
// routes/api.php
Route::get('/health/live', fn() => response()->json(['status' => 'ok']));
Route::get('/health/ready', function () {
$checks = [];
// Database
try {
DB::connection()->getPdo();
$checks['database'] = 'ok';
} catch (\Throwable $e) {
$checks['database'] = 'error: ' . $e->getMessage();
}
// Redis
try {
Cache::store('redis')->set('health-check', 1, 5);
$checks['cache'] = 'ok';
} catch (\Throwable $e) {
$checks['cache'] = 'error: ' . $e->getMessage();
}
$healthy = !str_contains(implode('', $checks), 'error');
$status = $healthy ? 200 : 503;
return response()->json([
'status' => $healthy ? 'healthy' : 'unhealthy',
'checks' => $checks,
], $status);
});
Node.js Express
app.get('/health/live', (_req, res) => {
res.json({ status: 'ok', uptime: process.uptime() });
});
app.get('/health/ready', async (_req, res) => {
const checks: Record<string, string> = {};
try {
await db.query('SELECT 1');
checks.database = 'ok';
} catch (e) {
checks.database = `error: ${e}`;
}
try {
await redis.ping();
checks.redis = 'ok';
} catch (e) {
checks.redis = `error: ${e}`;
}
const healthy = Object.values(checks).every(v => v === 'ok');
res.status(healthy ? 200 : 503).json({ status: healthy ? 'healthy' : 'unhealthy', checks });
});
Kubernetes probes
containers:
- name: app
livenessProbe:
httpGet:
path: /health/live
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
failureThreshold: 3
readinessProbe:
httpGet:
path: /health/ready
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
failureThreshold: 3
Implementation timeline
Basic liveness + readiness endpoints with DB and Redis checks: 0.5–1 day. With Kubernetes and monitoring system integration: 1–2 days.







