PgBouncerとHAProxyによる接続ルーティング
クライアントを現在のプライマリへルーティングし、読み取りトラフィックをレプリカ間で分散して、フェイルオーバー中もアプリケーションの可用性を保つ方法を学びます。
「PgBouncerとHAProxyによる接続ルーティング」はCoddyKit上の無料Advanced PostgreSQL: Indexing, Partitioning, Replicationレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 = 20HAProxy 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 200Separate 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 /replicaRead/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による接続ルーティング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Advanced PostgreSQL: Indexing, Partitioning, Replicationコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Advanced PostgreSQL: Indexing, Partitioning, Replicationコースには全4レッスンが含まれています。
「PgBouncerとHAProxyによる接続ルーティング」で何を学びますか?
クライアントを現在のプライマリへルーティングし、読み取りトラフィックをレプリカ間で分散して、フェイルオーバー中もアプリケーションの可用性を保つ方法を学びます。 ブラウザで直接実行するハンズオンコードでAdvanced PostgreSQL: Indexing, Partitioning, Replicationを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 自動フェイルオーバーツール(Patroni)
- レプリケーションの健全性監視
- ディザスタリカバリ戦略
- PgBouncerとHAProxyによる接続ルーティング