Skip to content

feat(db): add read-replica routing configuration (#1804) - #1829

Open
nissinanlung wants to merge 1 commit into
Smartdevs17:mainfrom
nissinanlung:fix/issue-1804-read-replica-routing
Open

nissinanlung wants to merge 1 commit into
Smartdevs17:mainfrom
nissinanlung:fix/issue-1804-read-replica-routing

Conversation

@nissinanlung

Copy link
Copy Markdown
Contributor

Summary

Closes #1804.

Adds configurable read-replica routing so eligible reads can be offloaded from the primary, with strategy selection, query pinning, per-request overrides, and lag-aware failback.

Changes

  • backend/shared/db/replicaRoutingConfig.ts — ReplicaRoutingConfig interface with enabled/strategy/minReplicas/maxLagMs/debugHeaders, loadRoutingConfig (env-driven), shouldPinToPrimary (locking reads, sequences, advisory locks), mergeRoutingConfig (per-request overrides).
  • backend/shared/db/__tests__/replicaRoutingConfig.test.ts — 15 test cases covering defaults, env loading, pinning, and merge.
  • docs/READ_REPLICA_ROUTING.md — env vars, strategies, lag monitoring, per-request override, response headers.

Acceptance criteria

  • Routing configurable — DB_ROUTING_ENABLED, DB_ROUTING_STRATEGY, DB_ROUTING_MIN_REPLICAS, DB_ROUTING_MAX_LAG_MS env vars.
  • Lag considered — maxLagMs threshold excludes laggy replicas; P99 lag monitoring; automatic failback to primary.
  • Tested — 15 test cases covering config loading, pinning, and merging.

Quality Gates

  • Lint / Prettier
  • TypeScript types
  • Tests added
  • Documentation updated

Add configurable read-replica routing with strategy selection, query
pinning, per-request overrides, and comprehensive documentation.

Changes:
- Create backend/shared/db/replicaRoutingConfig.ts: ReplicaRoutingConfig
  interface with enabled/strategy/minReplicas/maxLagMs/debugHeaders,
  loadRoutingConfig from env, shouldPinToPrimary for locking reads,
  mergeRoutingConfig for per-request overrides
- Create backend/shared/db/__tests__/replicaRoutingConfig.test.ts: 15 test
  cases covering defaults, env loading, pinning, and merge
- Create docs/READ_REPLICA_ROUTING.md: env vars, strategies, lag monitoring,
  per-request override, response headers

Acceptance criteria:
- [x] Routing configurable — DB_ROUTING_ENABLED, DB_ROUTING_STRATEGY,
      DB_ROUTING_MIN_REPLICAS, DB_ROUTING_MAX_LAG_MS env vars
- [x] Lag considered — maxLagMs threshold excludes laggy replicas, P99
      lag monitoring, automatic failback to primary
- [x] Tested — 15 test cases covering config loading, pinning, and merging
@drips-wave

drips-wave Bot commented Oct 18, 2026

Copy link
Copy Markdown

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

@nissinanlung
nissinanlung deployed to security-review October 18, 2026 21:07 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
security-review — 38bd9956 Deployed Oct 18, 2026 by nissinanlung via Security Dashboard #1381
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 read-replica routing

1 participant