8000 Document PgBouncer failover behavior in the DB guide by buketcoban · Pull Request #53355 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Document PgBouncer failover behavior in the DB guide - #53355

Merged
ahus1 merged 2 commits into
keycloak:mainfrom
buketcoban:docs/db-guide-pgbouncer-failover
Sep 30, 2026
Merged

ahus1 merged 2 commits into
keycloak:mainfrom
buketcoban:docs/db-guide-pgbouncer-failover

Conversation

@buketcoban
@buketcoban buketcoban commented Sep 30, 2026 •
Copy link
Copy Markdown
Contributor

Adds a sub-paragraph under "Writer and reader instances" in the DB guide that describes how PgBouncer can keep reporting a stale in_hot_standby value after a failover or switchover, and links the upstream PgBouncer issue pgbouncer/pgbouncer#859, as requested in #49515.

This change was drafted with the help of an AI assistant. I reviewed every line and can explain and revise it.

Closes #49515

Closes keycloak#49515

Signed-off-by: Buket Çoban <100703834+buketcoban@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 13:40
@buketcoban
buketcoban requested review from a team as code owners September 30, 2026 13:40
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The documentation accurately addresses the reported failure mode and clearly warns about the mitigation’s routing requirements.

Review effort: Balanced
Findings: None

What changed in this PR

Documents PgBouncer’s stale in_hot_standby behavior during PostgreSQL failovers.

Changes:

  • Explains the resulting JDBC connection failure.
  • Documents PgBouncer reload and targetServerType=any mitigations and risks.
  • Links the upstream PgBouncer issue.
File Description
docs/​guides/​server/​db.adoc Adds PgBouncer failover guidance.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@buketcoban
Copy link
Copy Markdown
Contributor Author

@ahus1 as you suggested in #49515, this adds the PgBouncer sub-paragraph under "Writer and reader instances". Happy to adjust the wording.

Small note: the description includes Closes #49515, but GitHub hasn't linked this PR to the issue under Development, so it may need to be linked manually.

@ahus1 ahus1 self-assigned this Sep 30, 2026
Review
8000 fb15a43
Signed-off-by: Alexander Schwartz <alexander.schwartz@ibm.com>
Copilot AI balanced review requested due to automatic review settings September 30, 2026 14:55
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The documentation accurately describes the reported behavior, risks, and configuration syntax.

Review effort: Balanced
Findings: None

@ahus1
ahus1 commented Sep 30, 2026
Copy link
Copy Markdown
Member

@nils-jakobs / @atiqur-rahman-pro / @Cojacfar - can one of you please review the pull request and confirm that this is correct? Thanks!

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor

Thanks for adding this documentation, @buketcoban!

The explanation accurately describes the issue: PgBouncer caching stale in_hot_standby values after a failover, which causes pgjdbc to reject connections when targetServerType=primary.

The trade-offs and risks of setting targetServerType=any (ensuring PgBouncer or the load balancer guarantees routing exclusively to the writable primary) are clearly covered, and the configuration syntax is correct.

Looks good to me! @ahus1

@ahus1
ahus1 commented Sep 30, 2026
Copy link
Copy Markdown
Member

@atiqur-rahman-pro - are you running this setup in production? If not, I would wait a bit more for someone who does.

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor

No, I'm not running this setup in production myself।

My review was based on verifying the pgjdbc driver behavior, Keycloak configuration syntax, and the upstream PgBouncer mechanics discussed in #49515.

Waiting for hands-on confirmation from someone running it in production (like @nils-jakobs or @Cojacfar) definitely makes sense!

@Cojacfar
Copy link
Copy Markdown

@atiqur-rahman-pro

I've had this change live in my (relatively small) production environment for quite a while without issue now. I'm running Keycloak so it's in a Docker container and passing the ?targetServerType=any through KC_DB_URL.

For context, I'm using HAProxy to determine which backend Patroni node is active. So my DB_URL is going to a LB and is always routed to the actual primary and not a read-only instance.

Your change seems correct in my reading!

@atiqur-rahman-pro
Copy link
Copy Markdown
Contributor

Thank you for confirming your production setup and architecture details, @Cojacfar!

@ahus1 - looks like we now have hands-on production confirmation as well.

@ahus1
ahus1 merged commit c0ac727 into keycloak:main Sep 30, 2026
72 checks passed
@buketcoban
Copy link
Copy Markdown
Contributor Author

Thanks @ahus1 for the review and the improvements, and @atiqur-rahman-pro and @Cojacfar for confirming!

@ahus1
ahus1 commented Oct 1, 2026
Copy link
Copy Markdown
Member

@buketcoban - thank you for the PR. For future PRs, it would be good to know if you add documentation if you've tested it with a local setup as a developer, if you are describing a setup you used in your production environment, or if you derived it from documentation.

As a follow-up: it would be interesting to know for me (as a Keycloak maintainer and developer, but someone who never ran Keycloak in production):

  • What are advantages of running pgBouncer in front of the database when running Keycloak?
  • Should this be a recommended setup, or rather not recommended (given documentation provided here)?
  • Is running pgBouncer more of a "cargo cult" thing where DBAs require it for all applications, although it doesn't serve a purpose for Keycloak?

@ahus1
ahus1 commented Oct 2, 2026
Copy link
Copy Markdown
Member

I've created a follow-up issue #53452 to continue the discussion, and also extend it to AWS RDS Proxy.

My current gut-feeling: Don't use a DB proxy for Keycloak, instead size your connection pool on each instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keycloak and Patroni failover and targetServerType=primary on pgBouncer

5 participants

0