Google Cloud — Cloud Load Balancing
Comprehensive guide to Google Cloud Load Balancing covering HTTP(S) Load Balancer, TCP/UDP proxy, Internal Load Balancer, SSL certificates, and backend service configuration..
20+ years shipping production infrastructure and CI/CD at scale. Everything here is grounded in real deployments.
- ✓Google Cloud account with billing enabled, gcloud CLI installed and configured, basic knowledge of VPCs and firewall rules, familiarity with HTTP/HTTPS protocols, a running web application (e.g., Flask, Nginx) deployed on Compute Engine or GKE.
Cloud Load Balancing is like having a specialized tool that handles the heavy lifting so you don't have to build and manage the underlying infrastructure yourself — it just works with Google Cloud.
Cloud Load Balancing distributes incoming traffic across multiple backends to ensure high availability and scalability. Unlike traditional load balancers that require pre-warming, GCP's global load balancer scales instantly and serves traffic from a single anycast IP. Understanding the types — HTTP(S), TCP/UDP, and Internal — is critical for architecting resilient systems.
Why Cloud Load Balancing Matters in Production
In production, traffic spikes are inevitable. A single server can't handle millions of concurrent users, and even if it could, a single point of failure is unacceptable. Cloud Load Balancing distributes incoming traffic across multiple backend instances, ensuring high availability, fault tolerance, and scalability. Google Cloud offers several load balancers: External HTTP(S), Internal TCP/UDP, and SSL Proxy. Each serves a specific use case. For most web applications, the External HTTP(S) Load Balancer is the go-to choice because it supports global anycast IP, autoscaling, and advanced features like URL rewriting and traffic splitting. Without a load balancer, you risk downtime during traffic surges and manual failover nightmares. This section sets the foundation: understand the problem before choosing the tool.
Architecture: Frontend, Backend, and Routing Rules
A Google Cloud Load Balancer consists of three components: forwarding rules (frontend), target proxies (routing), and backend services (instance groups). The forwarding rule binds an IP and port to a target proxy. The proxy interprets the protocol (HTTP, HTTPS, TCP) and applies URL maps to route requests to the correct backend service. Backend services define the instance group, health checks, and balancing mode (rate or utilization). This layered design allows you to decouple traffic routing from backend management. For example, you can route /api/ to one backend and / to another without changing the frontend IP. Understanding this architecture is critical for debugging routing issues and scaling efficiently.
Health Checks: The Difference Between Graceful Degradation and Outage
Health checks are the load balancer's eyes into backend health. Without them, the load balancer blindly sends traffic to unhealthy instances, causing errors. Google Cloud supports HTTP, HTTPS, TCP, and SSL health checks. For HTTP(S) health checks, you define a request path (e.g., /healthz) and expected response codes. The load balancer probes each instance at a configurable interval (default 5s) and marks it unhealthy after consecutive failures. In production, set the health check path to a lightweight endpoint that validates critical dependencies (database, cache). Avoid heavy endpoints that could fail under load. Also, configure the 'unhealthy threshold' to 2-3 to avoid flapping. A common mistake is using the same path as the main application, which can cause cascading failures.
Session Affinity: When You Need Sticky Sessions (and When You Don't)
Session affinity (sticky sessions) ensures that a client's requests are always sent to the same backend instance. This is useful for stateful applications like shopping carts or WebSocket connections. Google Cloud supports three modes: NONE (default), CLIENT_IP, and GENERATED_COOKIE. CLIENT_IP uses the client's IP, but fails if clients share IPs (e.g., behind NAT). GENERATED_COOKIE inserts a cookie (GCLB) to track the backend. However, sticky sessions reduce load balancing effectiveness and can cause uneven load distribution. In production, prefer stateless backends with external session stores (Redis, Memcached). If you must use affinity, set a reasonable cookie TTL (e.g., 1 hour) and monitor backend load imbalance.
Traffic Splitting: Canary Deployments and A/B Testing
Traffic splitting lets you route a percentage of requests to different backend services. This is essential for canary deployments (gradually rolling out a new version) and A/B testing. Google Cloud's External HTTP(S) Load Balancer supports this via the 'weight' field in backend services. You can assign weights to multiple backends in the same URL map path rule. For example, 90% traffic to v1, 10% to v2. Monitor error rates and latency before shifting more traffic. In production, start with 1% traffic to the canary, then increase by 5-10% every 10 minutes if metrics are healthy. Use request-level cookies or headers for consistent routing if needed.
SSL/TLS Termination: Offload Crypto to the Load Balancer
SSL termination at the load balancer reduces CPU load on backend instances by handling encryption/decryption at the edge. Google Cloud supports SSL certificates managed by Certificate Manager or self-managed. You can also enable TLS 1.3 and configure cipher suites. In production, use managed certificates for automatic renewal and rotation. For internal traffic, consider using Internal HTTPS Load Balancer with private certificates. Important: after termination, traffic between the load balancer and backend is unencrypted by default. To encrypt it, enable 'backend service protocol' HTTPS and configure certificates on the backend. This is called end-to-end encryption and is recommended for compliance.
Logging and Monitoring: See Every Request
Without visibility, you're flying blind. Google Cloud Load Balancer integrates with Cloud Logging and Cloud Monitoring. Enable access logs to capture request metadata: client IP, latency, status code, backend name. Logs are sampled by default (1 in 10) to reduce cost; in production, set sampling to 1.0 during incidents. Use Cloud Monitoring to create dashboards for request count, latency (p50, p95, p99), error rate, and backend utilization. Set up alerts for 5xx error rate > 1% and latency p99 > 500ms. Also monitor health check status to detect backend failures early. Logs can be exported to BigQuery for advanced analytics.
Scaling: Autoscaling Backends with Load Balancer Awareness
The load balancer and autoscaler work together to handle traffic spikes. Autoscalers use metrics like CPU utilization, requests per second, or custom metrics to add/remove instances. For optimal performance, set the autoscaler's target utilization to 60-70% to leave headroom for spikes. The load balancer's balancing mode (RATE or UTILIZATION) affects how traffic is distributed. Use RATE for request-based scaling (e.g., 1000 requests per second per instance) and UTILIZATION for CPU-based. In production, combine both: set a max RATE per instance to prevent overload, and use CPU utilization for scaling. Also, configure cool-down periods to avoid thrashing.
Internal Load Balancing: Secure Microservices Communication
For microservices running in VPC, use Internal Load Balancers (ILB) to distribute traffic between services without exposing them to the internet. ILB supports TCP/UDP and HTTP(S) protocols. It uses RFC 1918 IP addresses and is only accessible within the VPC. In production, use ILB for service-to-service communication (e.g., frontend to backend). Combine with Private Service Connect for hybrid connectivity. ILB also supports health checks and session affinity. A common pattern is to front each microservice with an ILB, allowing independent scaling and blue/green deployments.
Cost Optimization: Pay Only for What You Use
Cloud Load Balancing pricing is based on forwarding rules and data processed. Each forwarding rule costs a fixed hourly rate (e.g., $0.025/hr for external HTTP LB). Data processing is charged per GB (e.g., $0.008/GB). To optimize, consolidate multiple services under one load balancer using URL maps instead of separate LBs. Use internal LBs for backend traffic to avoid egress charges. Also, consider using preemptible VMs for autoscaling groups to reduce compute costs. Monitor your LB usage with billing reports and set budget alerts. A common waste is leaving forwarding rules for deleted backends; clean up regularly.
Disaster Recovery: Multi-Region Failover with Global LB
Global load balancers can route traffic to backends in multiple regions. If one region fails, the LB automatically redirects traffic to healthy regions. This is achieved by adding backend services from different regions to the same LB. Configure health checks per region and set failover priority. For active-passive, set one region as primary with high priority, and secondary with lower priority. For active-active, distribute traffic across regions using weights. In production, test failover regularly by simulating a region outage. Also, ensure your database can handle multi-region writes or use a global database like Spanner.
Common Pitfalls and How to Avoid Them
Even experienced teams make mistakes. Here are the top pitfalls: (1) Health check path too heavy — use a lightweight endpoint. (2) Not setting connection draining — when an instance is removed, existing connections are dropped. Enable connection draining (timeout up to 1 hour) to allow in-flight requests to complete. (3) Ignoring backend capacity — if all backends are at max capacity, new requests fail. Set max RATE per instance and autoscale. (4) Misconfigured firewall rules — load balancer health checks come from specific IP ranges (35.191.0.0/16, 130.211.0.0/22). Ensure your firewall allows these. (5) Not testing failover — simulate a region outage in staging. (6) Overlooking SSL certificate expiry — use managed certificates to auto-renew.
| File | Command / Code | Purpose |
|---|---|---|
| create-lb.sh | gcloud compute forwarding-rules create http-content-rule \ | Why Cloud Load Balancing Matters in Production |
| url-map.yaml | defaultService: https://www.googleapis.com/compute/v1/projects/my-project/global... | Architecture |
| healthz.py | from flask import Flask, jsonify | Health Checks |
| enable-sticky-sessions.sh | gcloud compute backend-services update web-backend \ | Session Affinity |
| traffic-split.sh | gcloud compute backend-services add-backend web-backend \ | Traffic Splitting |
| ssl-cert.sh | gcloud compute ssl-certificates create my-cert \ | SSL/TLS Termination |
| enable-logging.sh | gcloud compute backend-services update web-backend \ | Logging and Monitoring |
| autoscaler.sh | gcloud compute instance-groups managed set-autoscaling web-instance-group \ | Scaling |
| create-ilb.sh | gcloud compute forwarding-rules create internal-lb-rule \ | Internal Load Balancing |
| list-forwarding-rules.sh | gcloud compute forwarding-rules list --global | Cost Optimization |
| multi-region-backend.sh | gcloud compute backend-services create web-backend \ | Disaster Recovery |
| connection-draining.sh | gcloud compute backend-services update web-backend \ | Common Pitfalls and How to Avoid Them |
Key takeaways
Common mistakes to avoid
3 patternsNot understanding load balancing pricing model
Using default settings without tuning for load balancing
Missing IAM permissions and service account configuration
Interview Questions on This Topic
What is Cloud Load Balancing and when would you use it?
Frequently Asked Questions
20+ years shipping production infrastructure and CI/CD at scale. Everything here is grounded in real deployments.
That's Google Cloud. Mark it forged?
5 min read · try the examples if you haven't