feat(db): add read-replica pool routing with Prometheus metrics - #1096
Merged
chinweobtagaz merged 1 commit intoAug 26, 2026
Merged
Conversation
- 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.
|
@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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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