Skip to content

Add database connection health check with latency - #446

Open
D-Bluemoon wants to merge 4 commits into
Betta-Pay:mainfrom
D-Bluemoon:main
Open

Add database connection health check with latency#446
D-Bluemoon wants to merge 4 commits into
Betta-Pay:mainfrom
D-Bluemoon:main

Conversation

@D-Bluemoon

@D-Bluemoon D-Bluemoon commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR updates PostgreSQL health checks to report real round-trip latency and server time, and it degrades overall service health when DB latency exceeds 1000ms.

What changed

  • health.ts
    • checkPostgresql() now measures latency with performance.now()
    • query result extraction now reads serverVersion from SELECT 1, NOW() AS "serverVersion"
    • returns { healthy, latencyMs, serverVersion }
    • health status is marked degraded when PostgreSQL is connected but latency > 1000ms
  • schemas.ts
    • added optional healthy and serverVersion fields to DependencyHealth
  • health.test.ts
    • added tests for normal DB latency and slow-query degraded behavior
  • health.ts
  • index.ts
  • index.ts
    • updated service health DB checks to use SELECT 1, NOW() AS "serverVersion" so the shared helper can capture server time and latency

Rationale

  • SELECT 1 only verified connectivity, not latency.
  • performance.now() provides a more accurate in-process RTT measurement than Date.now().
  • Adding healthy and serverVersion makes the DB dependency health richer and more actionable.
  • Degrading overall health on high latency catches slow DB responses even when the DB is still reachable.

Fixes / Closes

Type of Change

  • Bug Fix (non-breaking change which fixes an issue)
  • New Feature
  • Breaking Change
  • Refactoring / Performance
  • Documentation / CI

Verification & Test Plan

Automated Tests

pnpm --filter @bettapay/validation test

Manual Verification

  1. Call the health endpoint for API Gateway / Indexer / Settlement Engine.
  2. Confirm the postgresql dependency now includes latencyMs, healthy, and serverVersion.
  3. Simulate a slow DB response and verify the overall health becomes degraded when latency exceeds 1000ms.

Backend Services (BettaPay-Backend)

  • Prisma models updated and migrated (if database schema changes) — N/A
  • Zod validation checks added/updated
  • API routes and services build successfully (pnpm build)
  • Local env variables updated in .env.example — N/A

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation — N/A
  • My changes generate no new warnings

@drips-wave

drips-wave Bot commented Jul 31, 2026

Copy link
Copy Markdown

@D-Bluemoon Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add database connection health check with latency

1 participant