PgBouncer 多核扩展 4 倍吞吐
推荐指数 73.0 NO. 011 · 2026.07.12
发布2026/07/11Score119Comments15
为什么值得看
ClickHouse 团队通过 so_reuseport 让多个 PgBouncer 进程绑定同一端口,突破单线程瓶颈。对高并发 Postgres 场景是零改造成本的即时方案。
编辑判断
PgBouncer 的单线程设计被诟病多年,但社区常见的 workaround 是用 HAProxy 或 Envoy 在前端做七层转发,架构复杂且增加一跳延迟。ClickHouse 的方案直接绕过了代理层,让内核做负载均衡,客户端完全无感知。
这个 trick 并非 PgBouncer 专属,任何单线程网络服务(如 Redis、Node.js 早期版本)都可以用同样模式扩展。如果你的服务还在用单进程监听端口,这是最低成本的多核化改造路径。
需要留意的是 so_reuseport 的调度策略是五元组哈希,长连接场景下可能出现热点核倾斜,短连接或配合连接重置策略效果更佳。
社区反馈
正面 15 条评论
核心争论:so_reuseport 多进程方案是否优于 HAProxy/替代代理,以及 PgBouncer 生态替代方案成熟度
相关内容
Postgres at Scale: Running Multiple PgBouncers Crunchy Data 提出 Multi-PgBouncer 方案,通过运行多个 PgBouncer 实例实现水平扩展,解决单实例性能瓶颈。 Scaling Postgres connections with PgBouncer PlanetScale 分析 PgBouncer 作为 PostgreSQL 连接池器的扩展能力,提及新兴替代方案 Neki 的更优架构。 Pgbouncer最佳实践:系列二 中文技术博客详述 PgBouncer 在高并发场景下的吞吐量表现,客户端增至四倍时仍能保持稳定。 Optimizing Database Connection Loads With PgBouncer and Testwick Mattermost 生产环境案例,揭示 PgBouncer 高 CPU 负载问题及 RDS 自动扩展的应对策略。
Article should show the config: [pgbouncer] listen_addr = 0.0.0.0 listen_port = 6432 so_reuseport = 1 peer_id = 1 unix_socket_dir = /tmp/pgbouncer1 [peers] 1 = host=/tmp/pgbouncer1 2 = host=/tmp/pgbouncer2 3 = host=/tmp/pgbouncer3 4 = host=/tmp/pgbou
Interesting. We run pgbouncer via kubernetes so it was straightforward to make multiple pgbouncer processes on one machine. Also straightforward to get them running on multiple machines, which helps because we run on Azure and they like to cause rolling outages across our fleet via VM maintenance...
Ack, makes sense. I’m very curious on how this affects throughput due to a potential extra network hop from pgbouncer to Postgres. Expecting it to have a minor difference, but still curious.