Intermediate LevelExpress JS
How do you implement Kubernetes Liveness (/healthz) and Readiness (/readyz) health check probes in Express?
❓ Interview Prompt: Explain Kubernetes probes: Liveness (/healthz) vs Readiness (/readyz), checking DB/Redis connectivity, and zero-downtime rolling updates.
Kubernetes uses health probes to monitor container health and control traffic routing:
1. Liveness Probe ('/healthz'):
•
Verifies if the Node.js event loop process is running. If it returns 500 or fails, Kubernetes restarts the container.
2. Readiness Probe ('/readyz'):
•
Verifies if the application is ready to accept traffic (checks database ping, Redis connections). If it fails, Kubernetes stops routing HTTP traffic to this pod during rolling updates until it recovers!
Code Implementation Example
javascript
import express from 'express';
const app = express();
// Liveness Probe: Quick check if process is alive
app.get('/healthz', (req, res) => res.status(200).send('OK'));
// Readiness Probe: Verifies DB and Redis connectivity before accepting traffic!
app.get('/readyz', async (req, res) => {
try {
await db.ping();
await redis.ping();
res.status(200).send('READY');
} catch (err) {
res.status(503).send('UNAVAILABLE');
}
});💡Implementing Kubernetes Liveness and Readiness health probes in Express.
🎯 Interview Key Takeaway
Implement /healthz for process liveness and /readyz (checking DB/Redis) for Kubernetes readiness traffic routing.
#Health Checks#Kubernetes#DevOps#Intermediate
Back to all Express JS Questions→