Documents
Health Check Endpoint Enhancements
Health Check Endpoint Enhancements
Type
Document
Status
Published
Created
Nov 23, 2025
Updated
Nov 23, 2025
Updated by
Dosu Bot

The health check endpoint was enhanced to provide real-time MongoDB status reporting, improving observability and enabling more effective monitoring and alerting for the backend service.

The /health endpoint, implemented in backend/src/routes/health.ts, now includes MongoDB connection status in its JSON response. This is achieved by invoking the isMongoConnected() helper, which checks both an internal connection state variable and the Mongoose connection's readyState to determine if the application is currently connected to MongoDB. The result is reported under the database.mongodb field as either "connected" or "disconnected", along with the raw readyState value for additional diagnostics. The endpoint also reports the status of other dependencies such as Redis and background queues, as well as overall system health, uptime, environment, version, and memory usage. The endpoint always returns HTTP 200, regardless of health status, to provide a stable liveness signal for basic monitoring systems. For readiness checks, the /health/ready endpoint enforces stricter semantics, returning HTTP 200 only if all critical dependencies (including MongoDB) are healthy, and HTTP 503 otherwise. This allows infrastructure tooling to distinguish between liveness and readiness states and to react promptly to infrastructure failures source.

MongoDB connection management is handled in backend/src/config/database.ts. The connection logic includes automatic retry with configurable delays, connection state tracking via the isConnected variable, and event handlers for connected, disconnected, reconnected, and error events. The isMongoConnected() function returns true only if both isConnected is true and the Mongoose connection is in the connected state. This ensures that the health check accurately reflects the application's ability to interact with the database source.

Example health check response:

{
  "status": "healthy",
  "timestamp": "2025-11-23T12:34:56.789Z",
  "uptime": 12345.67,
  "environment": "production",
  "version": "1.2.3",
  "node": "v18.17.0",
  "memory": {
    "used": 128,
    "total": 256,
    "unit": "MB"
  },
  "database": {
    "mongodb": "connected",
    "readyState": 1
  },
  "redis": {
    "status": "healthy",
    "latency": 2,
    "error": null
  },
  "queues": {
    "status": "healthy",
    "metrics": {},
    "error": null
  }
}

Monitoring and alerting systems can consume the /health endpoint to track the status of MongoDB and other dependencies. For example, an alert can be triggered if database.mongodb is "disconnected" or if the overall status is "degraded". For readiness-based orchestration (such as Kubernetes readiness probes), the /health/ready endpoint provides a clear signal by returning HTTP 503 when MongoDB or other critical dependencies are unavailable source.

These enhancements ensure that the health check endpoint provides actionable, real-time insight into the application's connectivity to MongoDB, supporting robust monitoring and rapid incident response.