Reduce API read-replica recovery conflict storms - #1001
Merged
Conversation
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.
Summary
readDbMaxConnsWhy
The production latency monitor is flapping because the sole read replica cancels waves of queries with:
An Axiom query for 2026-08-03 15:42 UTC found 156 recovery-conflict cancellations in one minute. The corresponding 500s affected unrelated routes, which points to shared replica pressure rather than an individual handler. Track comment reads were the largest visible amplifier in that window:
/v1/tracks/:trackId/comment_count: 54 errors/v1/tracks/:trackId/comments: 38 errorsProduction currently has three bridge pods and one four-vCPU read replica. Without an explicit limit, pgx sizes each pool from the 16 CPUs visible to the pod, allowing up to 48 concurrent request reads. This change halves that connection budget to 24 while retaining an environment override.
The comments table has no index beginning with
entity_id, so both high-volume track comment routes scan the table before their moderation joins. The new partial index supports both the filter and the default timestamp order without blocking writes during creation.Validation
go test ./api/dbv1 -run 'Test(NewDBPools|ChooseReplica|DBPoolsProxyMethods)$' -count=1go test ./config -count=1go test ./indexer -run '^$' -count=1go test ./api -run '^$' -count=1Rollout
The connection cap can be changed without another code change by setting
readDbMaxConns. Pool wait time and replica CPU should be watched during rollout. The expected tradeoff is bounded application-side queuing instead of allowing enough concurrent work to saturate the replica and trigger a recovery-conflict wave.Non-goals
This PR does not change standby feedback, statement timeouts, query retry behavior, replica capacity, or Kubernetes resources. Those have broader operational tradeoffs and should be reviewed separately.