Skip to content

fix: DB接続プールに上限を設定し高負荷時の500エラーを解消 - #441

Merged
taminororo merged 1 commit into
developfrom
fix/kanba/440-db-connection-pool-limit
Aug 1, 2026
Merged

fix: DB接続プールに上限を設定し高負荷時の500エラーを解消#441
taminororo merged 1 commit into
developfrom
fix/kanba/440-db-connection-pool-limit

Conversation

@taminororo

@taminororo taminororo commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

対応Issue

resolve #440

概要

api/lib/externals/db/db.gosql.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 がグリーン
  • ミニPC環境(pgbouncerなし)で、修正前後のhey -z 1m -c 400 -q 0.5実測比較
    • 修正前: 196.3 req/s、500エラー率37.8%
    • 修正後: 195.67 req/s、500エラー率0%(12,000件全件成功)

備考

Summary by CodeRabbit

  • 改善
    • PostgreSQL接続プールの設定を最適化し、同時接続数とアイドル接続数の上限を20に設定しました。
    • 接続の最大有効期間を30分に設定し、接続管理の安定性を向上しました。

sql.Openで作成する接続プールに上限がなく、高負荷時にPostgresの
max_connectionsを超過してリクエストが500エラーで失敗していた。
SetMaxOpenConns/SetMaxIdleConns/SetConnMaxLifetimeを設定し、
上限を超えた分は待機させる形にする。

ミニPC実測(pgbouncerなし、fix/kanba/437ブランチ、400並列負荷)で、
本修正により500エラー率が37.8%→0%(12,000件全件成功)に改善する
ことを確認済み。インフラ班によるpgbouncer(インフラ側の同種の対策)
導入の実測結果とも整合する。
@taminororo taminororo self-assigned this Aug 1, 2026
@coderabbitai

coderabbitai Bot commented Aug 1, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: be31e491-f0b1-4e4d-9016-cf5f968a64b0

📥 Commits

Reviewing files that changed from the base of the PR and between 19ffa76 and c1a6c99.

📒 Files selected for processing (1)
  • api/lib/externals/db/db.go

📝 Walkthrough

Walkthrough

ConnectMySQLでDB接続プールを設定します。最大接続数と最大アイドル接続数を20にし、接続最大寿命を30分にします。

Changes

DB接続プール設定

Layer / File(s) Summary
接続プール上限の設定
api/lib/externals/db/db.go
timeパッケージを追加しました。データベース接続後に、最大接続数と最大アイドル接続数を20に設定し、接続最大寿命を30分に設定します。

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed タイトルはDB接続プールの上限設定と高負荷時の500エラー解消という主な変更を明確に示しています。
Description check ✅ Passed Issue、概要、背景、テスト結果、備考を記載しており、変更内容と検証結果を十分に説明しています。
Linked Issues check ✅ Passed Issue #440の接続プール設定、timeの追加、テスト実施という要件を満たす変更内容です。
Out of Scope Changes check ✅ Passed 変更はIssue #440に関連するdb.goの接続プール設定に限定され、目的外の変更は確認できません。
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/kanba/440-db-connection-pool-limit

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@taminororo
taminororo merged commit 9ae477b into develop Aug 1, 2026
3 checks passed
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.

APIのDB接続プールに上限を設定する(SetMaxOpenConns等)

1 participant