Load balancing

NOTE
The load balancing feature is available for ADPG Enterprise Edition.

The ADPG cluster implements load balancing, which distributes client requests across multiple database replicas. This technique allows ADPG to process more requests simultaneously.

Load balancing in ADPG
Load balancing in ADPG
Load balancing in ADPG
Load balancing in ADPG

ADPG uses HAProxy (High Availability Proxy) to implement load balancing. HAProxy is a software TCP/HTTP load balancer. It listens on two ports: connections to one of them are transferred to the leader, requests to another are distributed across ADPG nodes. Write transactions should be sent to the first port, read only transactions to the second one. If you send a write transaction to the port for read only requests, ADPG can refuse it.

NOTE
HAProxy does not check whether a current request is a write or read-only transaction. Client applications must choose the correct port for connections.

HAProxy chooses which node should process a client request according to specified rules. Only the leader can process write transactions. Read only transactions are passed to replicas. You can also configure HAProxy to allow the leader to process read only transactions. See Configure load balancing for details.

If PgBouncer is enabled, write transactions will be sent to a PgBouncer instance of an ADPG leader node and read only transactions — to PgBouncer instances of replica nodes. See Configure PgBouncer.

The ADPG uses the Etcd cluster as DCS (Distributed Configuration Store). The Patroni service that implements streaming replication stores information about ADPG cluster configuration in etcd. For more information about ADPG replication, see ADPG high availability overview.

The conf.d service updates HAProxy configuration files when the cluster configuration changes. For example, a new node is added, or a leader is changed. The conf.d service is installed on the host with HAProxy. It subscribes on ADPG cluster configuration updates stored in Etcd. When ADPG changes the configuration, the conf.d service generates new configuration files for HAProxy and reloads HAProxy to apply the changes.

HAProxy also uses the Patroni REST API for additional verification which node is the leader and which is the replica.

TIP
To improve HAProxy high availability, you can use technologies that are not related to ADPG. For example, HAProxy instances may be added as A/AAAA records with a single FQDN to DNS server. Also, the keepalived tool can be used to manage multiple HAProxy hosts.
Found a mistake? Seleсt text and press Ctrl+Enter to report it