0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · 课时

使用 PgBouncer 和 HAProxy 路由连接

将客户端路由到当前主节点,并在副本之间分配读取流量,让应用在发生故障切换时仍保持可用。

使用 PgBouncer 和 HAProxy 路由连接 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Routing Problem

After a failover the primary moves to a new host. Applications need a stable endpoint so they do not have to be reconfigured each time. Connection routing solves this.

PgBouncer Basics

PgBouncer is a lightweight connection pooler. It multiplexes many client connections onto a small set of server connections, reducing backend load.

Pooling Modes

PgBouncer offers three pool modes:

  • session — connection held for the whole client session
  • transaction — returned after each transaction (most common)
  • statement — returned after each statement

Basic Config

A minimal pgbouncer.ini points at the database and sets the pool mode.

[databases]
app = host=10.0.0.5 port=5432 dbname=app
[pgbouncer]
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 20

HAProxy for the Primary

HAProxy can health-check backends and forward traffic only to the node that is currently the primary, giving clients one fixed write endpoint.

Detecting the Primary

HAProxy uses an HTTP health check against a tool like Patroni's REST API. Only the primary returns 200 on the leader endpoint.

option httpchk GET /primary
http-check expect status 200

Separate Read Endpoint

Define a second HAProxy frontend that balances across replicas (the /replica health check), so read-only queries scale across standbys.

listen postgres_read
  bind *:5433
  balance roundrobin
  option httpchk GET /replica

Read/Write Splitting

Applications connect to port 5432 for writes (primary) and 5433 for reads (replicas). Many drivers and ORMs support separate read/write data sources.

Replica Lag Caution

Replicas can lag behind the primary. Route only queries that tolerate slightly stale data to replicas; read-your-own-write paths should hit the primary.

Putting It Together

A common stack: app -> PgBouncer -> HAProxy -> Patroni-managed PostgreSQL cluster. PgBouncer pools, HAProxy routes by role, Patroni manages failover.

Failover Behavior

On failover, Patroni promotes a new primary, HAProxy health checks flip the leader within seconds, and pooled connections reconnect to the new endpoint with minimal disruption.

Quick Check

How does HAProxy keep sending writes to the right node after failover?

Recap

You learned to route connections for high availability: PgBouncer pools connections, HAProxy health-checks to find the primary and balance reads across replicas, and the whole stack flips automatically on failover.

常见问题解答

「使用 PgBouncer 和 HAProxy 路由连接」课时是免费的吗?

是的 — 「使用 PgBouncer 和 HAProxy 路由连接」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。

「使用 PgBouncer 和 HAProxy 路由连接」这节课中我会学到什么?

将客户端路由到当前主节点,并在副本之间分配读取流量,让应用在发生故障切换时仍保持可用。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 PgBouncer 和 HAProxy 路由连接」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?

能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 自动故障转移工具(Patroni)
  2. 监控复制健康状况
  3. 灾难恢复策略
  4. 使用 PgBouncer 和 HAProxy 路由连接
← 返回 Advanced PostgreSQL: Indexing, Partitioning, Replication