توجيه الاتصالات باستخدام PgBouncer وHAProxy
وجّه العملاء إلى العقدة الأساسية الحالية ووزّع حركة القراءة على النسخ المتماثلة للحفاظ على توفر التطبيقات أثناء عمليات التحويل عند الفشل.
توجيه الاتصالات باستخدام PgBouncer وHAProxy درس مجاني في Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.
تعلم Advanced PostgreSQL: Indexing, Partitioning, Replication مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 11
- الدروس
- 44
الأسئلة الشائعة
هل درس «توجيه الاتصالات باستخدام PgBouncer وHAProxy» مجاني؟
نعم — نص درس «توجيه الاتصالات باستخدام PgBouncer وHAProxy» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Advanced PostgreSQL: Indexing, Partitioning, Replication، انتقل إلى CoddyKit PRO. تتضمن دورة Advanced PostgreSQL: Indexing, Partitioning, Replication 4 دروس في المجموع.
ماذا ستتعلم في «توجيه الاتصالات باستخدام PgBouncer وHAProxy»؟
وجّه العملاء إلى العقدة الأساسية الحالية ووزّع حركة القراءة على النسخ المتماثلة للحفاظ على توفر التطبيقات أثناء عمليات التحويل عند الفشل. تتمرن على Advanced PostgreSQL: Indexing, Partitioning, Replication مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Advanced PostgreSQL: Indexing, Partitioning, Replication؟
لا تُشترط خبرة سابقة. Advanced PostgreSQL: Indexing, Partitioning, Replication على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «توجيه الاتصالات باستخدام PgBouncer وHAProxy»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Advanced PostgreSQL: Indexing, Partitioning, Replication هذا؟
نعم. كل درس في Advanced PostgreSQL: Indexing, Partitioning, Replication يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أدوات التحويل التلقائي عند التعطل (Patroni)
- مراقبة سلامة النسخ المتماثل
- استراتيجيات التعافي من الكوارث
- توجيه الاتصالات باستخدام PgBouncer وHAProxy