Skip to content

feat: lag-aware DB read routing (#715) - #796

Open
Bug00Joe wants to merge 1 commit into
RevoraOrg:masterfrom
Bug00Joe:feat/db-lag-aware-routing
Open

feat: lag-aware DB read routing (#715)#796
Bug00Joe wants to merge 1 commit into
RevoraOrg:masterfrom
Bug00Joe:feat/db-lag-aware-routing

Conversation

@Bug00Joe

@Bug00Joe Bug00Joe commented Aug 1, 2026

Copy link
Copy Markdown

Overview

This PR adds lag-aware read routing for cross-region replicas. When replica lag exceeds the RPO SLO, readQuery() steers SELECTs to the primary and emits db.replica.route_primary. When lag recovers, replica routing resumes and db.replica.recovered is emitted.

Related Issue

Closes #715

Changes

🗄️ Lag-aware routing

  • [MODIFY] src/db/replicaLagMonitor.ts
    • Background poller for replica lag via pg_last_xact_replay_timestamp().
    • Conservative default (unhealthy until first successful poll).
    • Automatic recovery when lag drops below SLO; emits db.replica.recovered.
  • [MODIFY] src/db/pool.tsreadQuery() applies routing per-query (not per-connection); emits db.replica.route_primary on primary fallback.
  • [MODIFY] src/config/env.ts — declares REPLICA_DB_URL, REPLICA_LAG_THRESHOLD_MS, REPLICA_POLL_INTERVAL_MS.
  • [MODIFY] src/db/replicaLagMonitor.test.ts — recovery metric assertion.
  • [MODIFY] docs/lag-aware-read-routing.md — docs for Multi-region failover: cross-region replica lag SLO with automatic read routing #715.

Verification Results

npx jest src/db/replicaLagMonitor.test.ts --forceExit
✅ 28/28 passed
Acceptance Criteria Status
ReplicaLagMonitor service
Router wired in pool.ts per-query readQuery()
Emit db.replica.route_primary on primary fallback
Recovery restores replica routing after lag drops ✅ + db.replica.recovered

Timeline

  • Implemented lag monitor + per-query routing + env schema + recovery metric.
  • Documented security assumptions and edge cases.

Add ReplicaLagMonitor and per-query readQuery routing that steers
SELECTs to the primary when cross-region replica lag exceeds the SLO.
Emits db.replica.route_primary on fallback and db.replica.recovered
when lag drops back below threshold. Declares REPLICA_* env schema.
@drips-wave

drips-wave Bot commented Aug 1, 2026

Copy link
Copy Markdown

@Bug00Joe 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

@Bug00Joe

Bug00Joe commented Aug 2, 2026

Copy link
Copy Markdown
Author

@thlpkee20-wq done closes #715

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.

Multi-region failover: cross-region replica lag SLO with automatic read routing

1 participant