0Pricing
Advanced PostgreSQL: Indexing, Partitioning, Replication · Leçon

Routage des connexions avec PgBouncer et HAProxy

Acheminez les clients vers le nœud primaire actuel et répartissez le trafic de lecture entre les réplicas afin de maintenir la disponibilité des applications lors des basculements.

Routage des connexions avec PgBouncer et HAProxy est une leçon Advanced PostgreSQL: Indexing, Partitioning, Replication gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Advanced PostgreSQL: Indexing, Partitioning, Replication, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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.

Questions Fréquemment Posées

La leçon « Routage des connexions avec PgBouncer et HAProxy » est-elle gratuite ?

Oui — le texte complet de « Routage des connexions avec PgBouncer et HAProxy » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Advanced PostgreSQL: Indexing, Partitioning, Replication, passe à CoddyKit PRO. Le cours Advanced PostgreSQL: Indexing, Partitioning, Replication comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Routage des connexions avec PgBouncer et HAProxy » ?

Acheminez les clients vers le nœud primaire actuel et répartissez le trafic de lecture entre les réplicas afin de maintenir la disponibilité des applications lors des basculements. Tu pratiques Advanced PostgreSQL: Indexing, Partitioning, Replication avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Advanced PostgreSQL: Indexing, Partitioning, Replication ?

Aucune expérience préalable n'est requise. Advanced PostgreSQL: Indexing, Partitioning, Replication sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.

Combien de temps prend la leçon « Routage des connexions avec PgBouncer et HAProxy » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Advanced PostgreSQL: Indexing, Partitioning, Replication ?

Oui. Chaque leçon Advanced PostgreSQL: Indexing, Partitioning, Replication inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. Outils de basculement automatique (Patroni)
  2. Surveillance de l’état de la réplication
  3. Stratégies de reprise après sinistre
  4. Routage des connexions avec PgBouncer et HAProxy
← Retour à Advanced PostgreSQL: Indexing, Partitioning, Replication