Advanced PostgreSQL: Indexing, Partitioning, Replication · Lektion

Verbindungsrouting mit PgBouncer und HAProxy

Leiten Sie Clients zum aktuellen Primärserver und verteilen Sie Lesezugriffe auf Replikate, damit Anwendungen auch bei Failover verfügbar bleiben.

Lektion 4 von 413 Schritte

Verbindungsrouting mit PgBouncer und HAProxy ist eine kostenlose Advanced PostgreSQL: Indexing, Partitioning, Replication-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Advanced PostgreSQL: Indexing, Partitioning, Replication-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Advanced PostgreSQL: Indexing, Partitioning, Replication-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Kostenlos starten

Lerne Advanced PostgreSQL: Indexing, Partitioning, Replication mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
11
Lektionen
44

Häufig gestellte Fragen

Ist die Lektion „Verbindungsrouting mit PgBouncer und HAProxy“ kostenlos?

Ja — der vollständige Text von „Verbindungsrouting mit PgBouncer und HAProxy“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Advanced PostgreSQL: Indexing, Partitioning, Replication-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Advanced PostgreSQL: Indexing, Partitioning, Replication-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Verbindungsrouting mit PgBouncer und HAProxy“?

Leiten Sie Clients zum aktuellen Primärserver und verteilen Sie Lesezugriffe auf Replikate, damit Anwendungen auch bei Failover verfügbar bleiben. Du übst Advanced PostgreSQL: Indexing, Partitioning, Replication mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Advanced PostgreSQL: Indexing, Partitioning, Replication zu starten?

Keine Vorkenntnisse erforderlich. Advanced PostgreSQL: Indexing, Partitioning, Replication auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Verbindungsrouting mit PgBouncer und HAProxy“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Advanced PostgreSQL: Indexing, Partitioning, Replication-Lektion Code schreiben und ausführen?

Ja. Jede Advanced PostgreSQL: Indexing, Partitioning, Replication-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Tools für automatisches Failover (Patroni)
  2. Replikationszustand überwachen
  3. Strategien für Disaster Recovery
  4. Verbindungsrouting mit PgBouncer und HAProxy
← Zurück zu Advanced PostgreSQL: Indexing, Partitioning, Replication