Database Health Check Component Design

Design a database health check component that validates connection pool status, query latency, and replication lag for PostgreSQL, MySQL, or MongoDB.

Component Checks

Detailed Explanation

Database Health Check

The database health check is the most critical component in any health check endpoint. A failed database usually means the service cannot function.

Response Component

{
  "database": {
    "status": "UP",
    "duration": "12ms",
    "message": "PostgreSQL connection pool active",
    "details": {
      "type": "postgresql",
      "poolSize": 20,
      "activeConnections": 5,
      "idleConnections": 15,
      "waitingRequests": 0
    }
  }
}

What to Check

  1. Connection availability: Can you get a connection from the pool?
  2. Query execution: Run a simple SELECT 1 query
  3. Latency: Is the response time within acceptable limits?
  4. Pool health: Are connections being created and recycled properly?
  5. Replication lag: For read replicas, is data reasonably current?

Implementation Pattern

async function checkDatabase() {
  const start = Date.now();
  try {
    await pool.query('SELECT 1');
    const duration = Date.now() - start;
    return {
      status: duration < 100 ? 'UP' : 'DEGRADED',
      duration: `${duration}ms`,
      message: `Query completed in ${duration}ms`
    };
  } catch (error) {
    return {
      status: 'DOWN',
      duration: `${Date.now() - start}ms`,
      message: error.message
    };
  }
}

Threshold Guidelines

Metric Healthy Degraded Unhealthy
Query latency < 50ms 50-500ms > 500ms
Pool utilization < 70% 70-90% > 90%
Replication lag < 1s 1-10s > 10s

Use Case

Any service that depends on a relational or document database, especially microservices that need to detect database outages before client requests fail.

Try It — Health Check Endpoint Designer

Open full tool