What if your system could tell you it's sick before users even notice?
Why Health check pattern in Microservices? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a busy restaurant with many chefs in different kitchens. You want to know if each kitchen is ready to cook orders, but you have to call each chef one by one to ask if they are okay.
Calling each chef manually wastes time and can cause mistakes. If a chef is busy or doesn't answer, you don't know if the kitchen is really ready. This slows down your service and frustrates customers.
The health check pattern acts like a smart assistant who regularly checks each kitchen's status automatically. It quickly tells you which kitchens are ready and which need help, so you can fix problems fast and keep orders moving smoothly.
callKitchenStatus(kitchen1) callKitchenStatus(kitchen2) callKitchenStatus(kitchen3)
for kitchen in kitchens: status = kitchen.health_check() log(status)
This pattern lets your system spot problems early and keep services running smoothly without manual checks.
In a microservices app, health checks help the system know if each service is working well before sending user requests, avoiding errors and downtime.
Manual status checks are slow and unreliable.
Health check pattern automates and speeds up status monitoring.
It helps keep complex systems healthy and responsive.
Practice
health check pattern in microservices?Solution
Step 1: Understand the health check pattern purpose
The health check pattern is designed to monitor if a microservice is alive and functioning properly by sending simple requests to it.Step 2: Identify the correct purpose among options
Only To regularly verify if a service is running and responsive describes this monitoring function, while others describe unrelated tasks like database size, encryption, or deployment.Final Answer:
To regularly verify if a service is running and responsive -> Option AQuick Check:
Health check = verify service status [OK]
- Confusing health check with deployment or encryption
- Thinking health check changes service data
- Assuming health check increases service capacity
Solution
Step 1: Identify typical health check endpoint behavior
A health check endpoint usually responds with a simple status code like 200 OK to indicate the service is healthy.Step 2: Match this behavior with the options
Create an endpoint like/healththat returns status 200 if service is healthy matches this by returning status 200 on/health. Other options perform unrelated or harmful actions.Final Answer:
Create an endpoint like /health that returns status 200 if service is healthy -> Option AQuick Check:
Health endpoint = status 200 OK [OK]
- Confusing health check with data deletion
- Expecting health check to return logs
- Assuming health check restarts service
function healthCheck() {
if (database.isConnected() && cache.isAvailable()) {
return { status: 200, message: 'Healthy' };
} else {
return { status: 503, message: 'Unhealthy' };
}
}
What will the endpoint return if the database is connected but the cache is down?Solution
Step 1: Analyze the condition in the healthCheck function
The function returns healthy only if both database.isConnected() and cache.isAvailable() are true.Step 2: Evaluate the given scenario
Database is connected (true), cache is down (false), so the condition is false and the function returns status 503 with 'Unhealthy'.Final Answer:
{ status: 503, message: 'Unhealthy' } -> Option DQuick Check:
Both checks true = 200, else 503 [OK]
- Assuming partial health returns 200 OK
- Confusing 503 with 404 status
- Expecting an error instead of status response
GET /health
Response: { "status": "ok" }
But monitoring tools report the service as unhealthy. What is the likely problem?Solution
Step 1: Understand monitoring tool expectations
Most monitoring tools expect the health check endpoint to return HTTP status code 200 to mark service healthy.Step 2: Identify the issue with the current implementation
The response body contains status 'ok' but if the HTTP status code is not 200, tools may mark it unhealthy.Final Answer:
The endpoint does not return an HTTP status code 200 -> Option BQuick Check:
Health check needs HTTP 200 status [OK]
- Thinking response body text controls health status
- Assuming endpoint URL name matters
- Ignoring actual service availability
Solution
Step 1: Understand health check pattern for dependencies
Health checks should verify the service and its critical dependencies like database and cache to ensure full functionality.Step 2: Evaluate the options for best practice
Each service exposes a/healthendpoint that returns 200 only if all dependencies (database, cache) are reachable; otherwise returns 503 correctly implements a health endpoint that returns 200 only if all dependencies are healthy, otherwise 503. This supports automatic recovery and monitoring.Final Answer:
Each service exposes a /health endpoint that returns 200 only if all dependencies (database, cache) are reachable; otherwise returns 503 -> Option CQuick Check:
Health check includes dependencies, returns 200 or 503 [OK]
- Ignoring dependency status in health check
- Relying on a single central health endpoint
- Checking dependencies outside service endpoints
