Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 45 ยท Networking

Load Balancing, Cloud CDN and DNS

Learn the Google Cloud global external HTTP(S) load balancer: forwarding rules, URL maps, backends, Cloud CDN caching and Cloud DNS records, with a URL map router.

A single server is a single point of failure, and it is far away from most of your users. A load balancer gives your application one public address and spreads incoming requests across many healthy backends. Cloud CDN adds caching at Google's edge locations so repeated requests are answered close to the user. Cloud DNS turns your domain name into the load balancer's address.

This lesson follows one request from a browser to your backends and shows the pieces that Google Cloud calls the load balancer: the front end, the URL map and the backend services. Product names and options change, so verify the details in current documentation.

Which load balancer?

Google Cloud offers several load balancers, grouped by traffic type and scope:

  • Application load balancers work at the HTTP/HTTPS level (layer 7). They can be external (internet-facing) or internal, and global or regional.
  • Network load balancers pass TCP/UDP traffic (layer 4) with very low overhead, for things that are not HTTP.
  • The classic default for websites and APIs is the global external Application Load Balancer, often still called the "global external HTTP(S) load balancer".

Its main trick is one anycast IP address that is advertised from Google's edge worldwide: a user's request enters Google's network at the nearest edge location, and the load balancer sends it to the closest healthy backend that has capacity. Autoscaling groups are covered in Instance Groups, Autoscaling and Load Balancing.

Anatomy of the global HTTP(S) load balancer

From address to backend

Forwarding rule The public IP and port that receive traffic.
Target proxy Ends HTTPS using your certificate.
URL map Chooses a backend by host and path.
Backend service Group of VMs, containers or a bucket.

Health checks decide which backends receive requests.

1 / 2
flowchart LR
  U["Browser"] --> F["Forwarding rule<br/>IP 203.0.113.10:443"]
  F --> P["Target HTTPS proxy<br/>certificate"]
  P --> M["URL map"]
  M -->|"/api/*"| A["Backend service: api"]
  M -->|"/static/*"| B["Backend bucket: assets"]
  M -->|"default"| W["Backend service: web"]

One public address; the URL map sends each path to its own backend.

  1. The forwarding rule owns the external IP address and port.
  2. The target HTTPS proxy terminates TLS using a certificate, often a Google-managed certificate that renews itself, and applies the URL map.
  3. The URL map matches the request's host and path and chooses a backend service (or a backend bucket backed by Cloud Storage). The default goes to a fallback backend.
  4. A backend service lists its backends: managed instance groups, network endpoint groups (for Cloud Run, GKE or serverless), and so on. It also holds the health check, balancing mode and timeouts.
  5. Health checks probe each backend. Unhealthy ones receive no traffic until they pass again.

The VPC firewall must allow Google's health-check ranges to reach your backends (VPC Networks, Subnets and Firewall Rules), or every backend looks unhealthy. This is a very common setup mistake.

Terminal
# The shape of the commands (read-only; many more flags are needed in real use)
gcloud compute backend-services create web-backend --global \
  --protocol=HTTP --health-checks=web-check

gcloud compute url-maps create shop-map --default-service=web-backend
gcloud compute url-maps add-path-matcher shop-map \
  --path-matcher-name=paths --default-service=web-backend \
  --backend-service-path-rules="/api/*=api-backend"

gcloud compute target-https-proxies create shop-proxy \
  --url-map=shop-map --ssl-certificates=shop-cert
gcloud compute forwarding-rules create shop-fr --global \
  --target-https-proxy=shop-proxy --ports=443

For serverless backends, see Cloud Run; for a full static-site build see Project: Host a Static Website.

Cloud CDN

Cloud CDN is switched on per backend service or backend bucket. When a request arrives at an edge location:

  • If a fresh copy of the response is in the edge cache, it is returned immediately (a cache hit), and your backend does no work.
  • If not (a cache miss), the request goes to the backend, and the response may be stored for the next visitor.

What gets cached depends on response headers and the cache mode. With Cache-Control: public, max-age=3600 a response can be cached for an hour; private or no-store prevents caching. The cache key (by default host, path and query string) decides when two requests count as the same object, so a random query string such as ?t=173... on every request can make every request a miss. You can invalidate cached paths if you must remove content early, but invalidation takes time and has limits, so prefer versioned file names such as app.4f3a9.js.

Cloud Armor: DDoS protection and a web application firewall

Cloud Armor is Google's edge security service. It works together with the external Application Load Balancer, so malicious traffic is stopped at Google's edge, before it reaches your VMs, containers or functions. You attach security policies to backend services (and edge policies to backend buckets and CDN-enabled services).

What it gives you:

  • Automatic DDoS mitigation. Volumetric network-layer and protocol attacks are absorbed by Google's global front-end infrastructure when you use a global load balancer, with no setup. Because the load balancer terminates connections at the edge, your backends never see the flood.
  • IP allow and deny lists. Rules match single addresses or CIDR ranges ("whitelisting and blacklisting" in older vocabulary; the current terms are allow and deny). You can also match by region or country code.
  • Preconfigured WAF rules. Ready-made rule sets, based on the OWASP Core Rule Set, detect SQL injection, cross-site scripting, local and remote file inclusion, protocol attacks and other web exploits.
  • Custom rules written in a small expression language over request attributes such as the path, headers, query, origin IP and country.
  • Rate limiting. Throttle a client after a number of requests per interval, or temporarily ban it. Good for login endpoints and APIs.
  • Threat intelligence. Google-maintained lists of addresses (known Tor exit nodes, malicious IPs, crawlers, public clouds) that you can allow or deny with one expression.
  • Adaptive Protection. Machine learning learns your normal traffic and alerts you, with a suggested rule, when a Layer 7 attack starts. It is part of the managed protection tiers.
  • Bot management through reCAPTCHA integration, and preview mode so that you can test a rule and log what it would have blocked before enforcing it.

How rules are evaluated

Every rule has a priority (a number, lowest first). Cloud Armor checks rules in priority order and the first match wins. The last rule is the default rule with the lowest priority (the highest number), and it applies when nothing else matched. A policy for a public website typically allows by default and denies the bad. A policy for an admin tool does the reverse: deny by default and allow only the office range.

Cloud Armor is a paid service, billed per policy and rule and by the requests it inspects. Some features (Adaptive Protection, advanced DDoS response, the largest rule sets) need the Enterprise tier. Check current pricing before you enable it on busy services.

The stand-in below mimics a policy in plain Python: priorities, a country rule, a path-based WAF-like rule, an IP deny list and a rate limit. It is a teaching model and not Cloud Armor itself.

Python A security policy with priorities

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Where it is used

Cloud Armor shows up wherever a public endpoint needs protection: absorbing DDoS attempts against a site or API, blocking exploit attempts against a web application, protecting an online shop from scraping and credential stuffing during a sale, restricting an API to partner networks, and keeping a game's login and matchmaking services available during attacks. It complements, and does not replace, secure code: the firewall reduces risk but a vulnerable application is still vulnerable.

Cloud DNS

Domain names are resolved by DNS. Cloud DNS is Google's managed authoritative DNS service. You create a managed zone for example.com and add record sets:

Type Purpose Example
A Name to IPv4 address www โ†’ 203.0.113.10
AAAA Name to IPv6 address www โ†’ 2001:db8::10
CNAME Alias to another name docs โ†’ www.example.com.
MX Mail servers 10 mail.example.com.
TXT Text, for verification and SPF "v=spf1 -all"

Then you point your registrar's name servers at the four name servers Cloud DNS gives the zone. Changes spread as caches expire, controlled by each record's TTL (time to live): a low TTL means faster changes but more lookups. Lower the TTL before a planned migration, not during it. Note that an A record at the bare domain (example.com) cannot be a CNAME; use an A record pointing to the load balancer's IP.

Terminal
gcloud dns managed-zones create example-zone \
  --dns-name="example.com." --description="Main zone"
gcloud dns record-sets create www.example.com. \
  --zone=example-zone --type=A --ttl=300 --rrdatas=203.0.113.10

(The addresses in this lesson are from documentation ranges; they are not real servers.)

Stand-ins you can run

The real load balancer runs inside Google's network. The first stand-in is a URL map router: it picks a backend from host and path rules, preferring the most specific path, the way a URL map does conceptually. Real URL maps support more match types (prefix, exact, regex, headers); this model shows only prefix and exact matching.

JS A URL map router

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second stand-in shows what a CDN does. It counts hits and misses for a stream of requests, and shows how a changing query string defeats the cache.

JS Edge cache hits and misses

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real and what is the stand-in: forwarding rules, URL maps, backend services, Cloud CDN behavior and DNS records are real concepts, but nothing here talks to Google. The JavaScript only models routing and caching arithmetic in simplified form.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the URL map of an HTTP(S) load balancer decide?

    The URL map is the routing table of the application load balancer. Autoscaling and certificates are separate concerns.
  2. 2. All your backends are marked unhealthy right after setup, though the servers run fine. What is a likely cause?

    Health-check probes come from Google ranges. If the VPC firewall does not allow them, every backend fails its check.
  3. 3. Why can adding ?t=<timestamp> to every asset URL ruin CDN performance?

    By default the cache key includes the query string, so unique parameters mean the edge never finds a stored copy.
  4. 4. You want https://example.com (the bare domain) to point at a load balancer IP. Which record type fits?

    A bare domain cannot be a CNAME, so use an A record (or AAAA for IPv6) with the load balancer's address.

Summary

  • The global external application load balancer gives one anycast IP and routes users to the nearest healthy backend.
  • Its parts: forwarding rule, target proxy with certificate, URL map, backend services or buckets, and health checks.
  • Cloud CDN caches responses at the edge; headers and the cache key control what is stored, and versioned file names beat invalidation.
  • Cloud DNS hosts your zone's records; TTLs control how fast changes spread, and bare domains use A or AAAA records.
  • Always allow health-check ranges in the firewall.

Next: Hybrid Connectivity, VPN and Peering