Network load balancing consists of protocol-based operations. This type of load balancing allows you to balance load of your systems based on incoming IP protocol data, such as address, port, and protocol type.
Network load balancing uses forwarding rules that point to target pools , which list the instances available for load balancing and define which type of health check that should be performed on these instances. See the basic load balancing scenario for more information.
Network load balancing offers some load balancing options that are not available with HTTP load balancing. For example, you can load balance additional TCP/UDP-based protocols such as SMTP traffic. If your application is interested in TCP-connection-related characteristics, network load balancing allows your app to inspect the packets, which you cannot do with HTTP load balancing.
You can also use network load balancing to handle HTTPS traffic. You will need to:
- Create a firewall rule that allows traffic on port 443, and apply it to the balanced instances.
- Handle the encryption/decryption on your VM instances.
If you need cross-regional load balancing, use a DNS load balancer.
Load distribution algorithm
By default, to distribute traffic to instances, Google Compute Engine picks an instance based on a hash of the source IP and port and the destination IP and port. Incoming TCP connections are spread across instances and each new connection may go to a different instance. All packets for a connection are directed to the same instance until the connection is closed.
You can choose a different hashing method if you need better session affinity to ensure that requests from a specific client go to specific virtual machine instance.
Forwarding rules
Forwarding rules work in conjunction with target pools and target instances to support load balancing and protocol forwarding features. To use load balancing and protocol forwarding, you must create a forwarding rule that directs traffic to specific target pools (for load balancing) or target instances (for protocol forwarding). It is not possible to use either of these features without a forwarding rule.
Forwarding Rule resources live in the Forwarding Rules collection. Each forwarding rule matches a particular IP address, protocol, and optionally, port range to a single target pool or target instance. When traffic is sent to an external IP address that is served by a forwarding rule, the forwarding rule directs that traffic to the corresponding target pool or target instances. You can create up to 50 forwarding rule objects per project.
Learn more about forwarding rules and how to configure them.
Target pools
A Target Pool resource defines a group of instances that should receive incoming traffic from forwarding rules. When a forwarding rule directs traffic to a target pool, Google Compute Engine picks an instance from these target pools based on a hash of the source IP and port and the destination IP and port. See the Load distribution algorithm for more information about how traffic is distributed to instances.
Target pools can only be used with forwarding rules that handle TCP and UDP traffic. For all other protocols, you must create a target pool before you can use it with a forwarding rule. Each project can have up to 50 target pools.
If you intend for your target pool to contain a single virtual machine instance, you should consider using the Protocol Forwarding feature instead.
Learn more about target pools and how to configure them.
Get started
The network load balancing guide demonstrates how to quickly configure a load balancing solution and distribute traffic across a set of Apache instances. You can build on top of this scenario to work for other types of traffic or more complex configurations.