Load balancer for hosts
Managed Service for PostgreSQL allows you to use an internal network balancer to distribute the load among hosts. This load balancer operates on layer 4 of the OSI network model but uses layer 3 technologies to speed up packet processing.
Note
This feature is at the Preview stage. Contact support for access.
Using a load balancer gives you the following advantages:
- A single entry point for connecting to the master host.
- Even distribution of read requests among replicas that factors in their load.
- Choice of load balancing policy. For example, you can set
Least connectionsto distribute load among replicas with the fewest connections. - Minimization of cluster unavailability during master failover or changes in the replica composition.
- To switch the application to a new cluster when restoring from a backup, you just need to replace a single FQDN.
The load balancer automatically accounts for changes in cluster topology: new replicas are included in load balancing, and removed ones are excluded.
In the cluster architecture, the load balancer is implemented as a separate service that can be enabled when creating or modifying a cluster. The load balancer stops and starts simultaneously with the cluster stop and start. The load balancer is assigned a single FQDN common to all availability zones, which serves as a single entry point for all database requests.Incoming traffic to the load balancer is distributed across all hosts in the cluster, and when hosts are added or removed from the cluster, they are automatically added to or removed from balancing.
The network traffic regulation schema for the cluster using a load balancer is shown below:
Warning
We do not recommend creating a Network Load Balancer (NLB) manually for cluster access. The load balancer that is automatically created during cluster deployment has a critically important proxy configuration. Without this configuration, the NLB will block traffic to the handler ports, resulting in loss of access to the cluster.
When the load balancer is enabled, it remains possible to connect directly to the database via the host FQDN, bypassing the load balancer.
A separate proxy service based on Odyssey
- Monitoring host availability.
- Checking replica lag.
- Verifying host role.
The load balancer provides several special ports for target connections:
-
Connection only to the master host: port
6432. -
Connection to replicas with lag less than a predefined value: port
7432.For example, to distribute read requests only among replicas with a lag of no more than 30 seconds:
- Create a cluster with the load balancer enabled and specify a replica lag value of 30 seconds in the
7432port settings. - Specify the database connection string with the load balancer FQDN and port
7432in your application settings.
- Create a cluster with the load balancer enabled and specify a replica lag value of 30 seconds in the
-
Connection to all replicas, regardless of lag: port
7433.
Traffic from each open port of the load balancer is directed to the proxy service and then distributed to target connections depending on the port number.
Note
When using a load balancer, client connections remain controlled by the Odyssey connection manager.