Bird
Raised Fist0
Microservicessystem_design~12 mins

Health check pattern in Microservices - Architecture Diagram

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
System Overview - Health check pattern

This system uses the health check pattern to monitor the status of microservices. It ensures that the load balancer only sends user requests to services that are healthy and ready to respond. The system must detect failures quickly and avoid sending traffic to unhealthy services.

Architecture Diagram
User
  |
  v
Load Balancer <--> Health Check Service
  |
  v
+-------------------+    +-------------------+
| Microservice A     |    | Microservice B     |
| (with Health API)  |    | (with Health API)  |
+-------------------+    +-------------------+
       |                        |
       v                        v
  Database A               Database B
  (optional cache)         (optional cache)
Components
User
client
Sends requests to the system
Load Balancer
load_balancer
Distributes user requests only to healthy microservices
Health Check Service
monitoring_service
Periodically checks health endpoints of microservices
Microservice A
service
Handles user requests and exposes health status
Microservice B
service
Handles user requests and exposes health status
Database A
database
Stores data for Microservice A
Database B
database
Stores data for Microservice B
Request Flow - 9 Hops
Health Check ServiceMicroservice A
Microservice AHealth Check Service
Health Check ServiceLoad Balancer
UserLoad Balancer
Load BalancerMicroservice A
Microservice ADatabase A
Database AMicroservice A
Microservice ALoad Balancer
Load BalancerUser
Failure Scenario
Component Fails:Microservice B
Impact:Load Balancer stops sending user requests to Microservice B, causing potential reduced capacity but no failed user requests to that service.
Mitigation:Health Check Service detects failure via failed health checks and informs Load Balancer to mark Microservice B as unhealthy. Traffic is routed only to healthy services.
Architecture Quiz - 3 Questions
Test your understanding
Which component decides if a microservice should receive user requests?
AHealth Check Service
BLoad Balancer
CDatabase
DUser
Design Principle
The health check pattern improves system reliability by continuously monitoring service health and preventing traffic from reaching unhealthy services. This reduces failed requests and helps maintain a smooth user experience.

Practice

(1/5)
1. What is the main purpose of the health check pattern in microservices?
easy
A. To regularly verify if a service is running and responsive
B. To increase the size of the service database
C. To encrypt communication between services
D. To deploy new versions of the service automatically

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To regularly verify if a service is running and responsive -> Option A
  4. Quick Check:

    Health check = verify service status [OK]
Hint: Health check means checking if service is alive and working [OK]
Common Mistakes:
  • Confusing health check with deployment or encryption
  • Thinking health check changes service data
  • Assuming health check increases service capacity
2. Which of the following is the correct way to implement a health check endpoint in a microservice?
easy
A. Create an endpoint like /health that returns status 200 if service is healthy
B. Create an endpoint that deletes all data when called
C. Create an endpoint that returns service logs only
D. Create an endpoint that restarts the service automatically

Solution

  1. 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.
  2. Step 2: Match this behavior with the options

    Create an endpoint like /health that returns status 200 if service is healthy matches this by returning status 200 on /health. Other options perform unrelated or harmful actions.
  3. Final Answer:

    Create an endpoint like /health that returns status 200 if service is healthy -> Option A
  4. Quick Check:

    Health endpoint = status 200 OK [OK]
Hint: Health endpoint returns 200 OK if service is fine [OK]
Common Mistakes:
  • Confusing health check with data deletion
  • Expecting health check to return logs
  • Assuming health check restarts service
3. Consider this pseudocode for a health check endpoint:
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?
medium
A. An error is thrown
B. { status: 200, message: 'Healthy' }
C. { status: 404, message: 'Not Found' }
D. { status: 503, message: 'Unhealthy' }

Solution

  1. Step 1: Analyze the condition in the healthCheck function

    The function returns healthy only if both database.isConnected() and cache.isAvailable() are true.
  2. 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'.
  3. Final Answer:

    { status: 503, message: 'Unhealthy' } -> Option D
  4. Quick Check:

    Both checks true = 200, else 503 [OK]
Hint: Both dependencies must be healthy for 200 OK [OK]
Common Mistakes:
  • Assuming partial health returns 200 OK
  • Confusing 503 with 404 status
  • Expecting an error instead of status response
4. A microservice health check endpoint is implemented as follows:
GET /health
Response: { "status": "ok" }
But monitoring tools report the service as unhealthy. What is the likely problem?
medium
A. The service is actually down and not responding
B. The endpoint does not return an HTTP status code 200
C. The endpoint URL should be /status instead of /health
D. The response body is missing the word 'healthy'

Solution

  1. Step 1: Understand monitoring tool expectations

    Most monitoring tools expect the health check endpoint to return HTTP status code 200 to mark service healthy.
  2. 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.
  3. Final Answer:

    The endpoint does not return an HTTP status code 200 -> Option B
  4. Quick Check:

    Health check needs HTTP 200 status [OK]
Hint: Health check must return HTTP 200 status code [OK]
Common Mistakes:
  • Thinking response body text controls health status
  • Assuming endpoint URL name matters
  • Ignoring actual service availability
5. You design a microservice system with multiple services. To improve reliability, you want to implement health checks that also verify database and cache connectivity. Which approach best follows the health check pattern?
hard
A. Each service exposes a /health endpoint that always returns 200 regardless of dependency status
B. Only one central service exposes a health check endpoint for all services combined
C. Each service exposes a /health endpoint that returns 200 only if all dependencies (database, cache) are reachable; otherwise returns 503
D. Health checks are done by querying the database directly without service endpoints

Solution

  1. 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.
  2. Step 2: Evaluate the options for best practice

    Each service exposes a /health endpoint 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.
  3. Final Answer:

    Each service exposes a /health endpoint that returns 200 only if all dependencies (database, cache) are reachable; otherwise returns 503 -> Option C
  4. Quick Check:

    Health check includes dependencies, returns 200 or 503 [OK]
Hint: Health check must verify all critical dependencies [OK]
Common Mistakes:
  • Ignoring dependency status in health check
  • Relying on a single central health endpoint
  • Checking dependencies outside service endpoints