fix: DB接続プールに上限を設定し高負荷時の500エラーを解消 - #441
Merged
Merged
Conversation
sql.Openで作成する接続プールに上限がなく、高負荷時にPostgresの max_connectionsを超過してリクエストが500エラーで失敗していた。 SetMaxOpenConns/SetMaxIdleConns/SetConnMaxLifetimeを設定し、 上限を超えた分は待機させる形にする。 ミニPC実測(pgbouncerなし、fix/kanba/437ブランチ、400並列負荷)で、 本修正により500エラー率が37.8%→0%(12,000件全件成功)に改善する ことを確認済み。インフラ班によるpgbouncer(インフラ側の同種の対策) 導入の実測結果とも整合する。
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesDB接続プール設定
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
対応Issue
resolve #440
概要
api/lib/externals/db/db.goのsql.Openで作成する接続プールにSetMaxOpenConns/SetMaxIdleConns/SetConnMaxLifetimeを設定し、上限を超えた分は待機させる形にする。無制限のままだと高負荷時にPostgresのmax_connectionsを超過し、リクエストが500エラーで即座に失敗していた。背景
インフラ班がSeeFT-stg環境(共有DB基盤)でpgbouncer(DB手前の接続プーラー)を導入したところ、500エラー率が改善することを確認した。ただしpgbouncerはインフラ側の設定であり、
docker-compose.ymlを使うローカル開発環境やCI環境(プーラーが存在しない)では同じ問題が再現し続ける。これを踏まえ、pgbouncerなしのミニPC環境(
fix/kanba/437-shift-cards-n-plus-oneブランチ)で本修正を検証したところ、400並列負荷時の500エラー率が37.8%→0%(12,000件全件成功)に改善することを実測で確認した。req/sはほぼ変化なし(196.3→195.67)で、レイテンシもp50 1.28秒〜p99 1.36秒という狭い範囲に収まった。アプリ側の対策だけで接続超過による500エラーを防げることが実証できたため、インフラ側の対策(pgbouncer)と独立に、コード側にも同様の安全策を入れる。画面スクリーンショット等
テスト項目
cd api && go test ./... -count=1がグリーンhey -z 1m -c 400 -q 0.5実測比較備考
docs/development/load-test-plan.md7章「issue 2」)で試験前修正の1つとして提案されていた項目Summary by CodeRabbit