Skip to content

feat(db): add read-replica pool routing with Prometheus metrics - #1096

Merged
chinweobtagaz merged 1 commit into
OpenKnight-Foundation:mainfrom
DavidAkere204:feat/read-replica-pool-routing
Aug 26, 2026
Merged

feat(db): add read-replica pool routing with Prometheus metrics#1096
chinweobtagaz merged 1 commit into
OpenKnight-Foundation:mainfrom
DavidAkere204:feat/read-replica-pool-routing

Conversation

@DavidAkere204

Copy link
Copy Markdown
Contributor
  • Add DbPool struct (backend/modules/db/src/db.rs) wrapping primary (read-write) and replica (read-only) sea-orm DatabaseConnection pools. Gracefully falls back to single-pool mode when DATABASE_REPLICA_URL is unset — server never crashes on startup without a replica.

  • Export DbPool from db crate root (db::DbPool).

  • Add Prometheus gauges: db_pool_connections_{active,idle,max} with a 'pool' label ('primary' | 'replica'). Exposed at GET /metrics.

  • Refactor GameService (service/games.rs): all read methods (get_game, list_games, get_game_history, get_player_rating_for_game) route to replica; all write/transactional methods (create_game, make_move, join_game, abandon_game, import_game, complete_game) route to primary.

  • Refactor PlayerService (service/players.rs): reads (find_player_by_id, get_player_by_username, authenticate_player) route to replica; writes (add_player, update_player, delete_player) route to primary.

  • Update api handlers (games.rs, players.rs, auth.rs, server.rs) to accept web::Data instead of web::Data. Auth token writes (generate, rotate, revoke) go to primary; player lookup on login goes to replica.

  • Add /metrics endpoint to server.rs that snapshots pool stats and returns Prometheus text format.

  • Add integration tests validating read/write pool routing using MockDatabase (db/src/integration_tests.rs, service/src/routing_tests.rs). Live-DB smoke tests skip gracefully when DATABASE_URL is absent.

  • Document DATABASE_REPLICA_URL in .env.example.

closes #1027

- Add DbPool struct (backend/modules/db/src/db.rs) wrapping primary
  (read-write) and replica (read-only) sea-orm DatabaseConnection pools.
  Gracefully falls back to single-pool mode when DATABASE_REPLICA_URL
  is unset — server never crashes on startup without a replica.

- Export DbPool from db crate root (db::DbPool).

- Add Prometheus gauges: db_pool_connections_{active,idle,max} with
  a 'pool' label ('primary' | 'replica'). Exposed at GET /metrics.

- Refactor GameService (service/games.rs): all read methods
  (get_game, list_games, get_game_history, get_player_rating_for_game)
  route to replica; all write/transactional methods (create_game,
  make_move, join_game, abandon_game, import_game, complete_game)
  route to primary.

- Refactor PlayerService (service/players.rs): reads (find_player_by_id,
  get_player_by_username, authenticate_player) route to replica; writes
  (add_player, update_player, delete_player) route to primary.

- Update api handlers (games.rs, players.rs, auth.rs, server.rs) to
  accept web::Data<DbPool> instead of web::Data<DatabaseConnection>.
  Auth token writes (generate, rotate, revoke) go to primary; player
  lookup on login goes to replica.

- Add /metrics endpoint to server.rs that snapshots pool stats and
  returns Prometheus text format.

- Add integration tests validating read/write pool routing using
  MockDatabase (db/src/integration_tests.rs,
  service/src/routing_tests.rs). Live-DB smoke tests skip gracefully
  when DATABASE_URL is absent.

- Document DATABASE_REPLICA_URL in .env.example.
@drips-wave

drips-wave Bot commented Aug 26, 2026

Copy link
Copy Markdown

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

@chinweobtagaz
chinweobtagaz merged commit 96d1035 into OpenKnight-Foundation:main Aug 26, 2026
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.

BE-52: PostgreSQL Read-Replica Query Routing for Leaderboards and History

2 participants