3.3 Serverless NEGs, Sessions, and Edge Controls
Key Takeaways
Serverless NEGs do not use ordinary backend health checks or balancing-mode capacity settings.
Use Cloud Run best-effort affinity where needed; keep authoritative session state external.
CDN cacheability follows the chosen mode and origin directives; do not cache private responses accidentally.
Serverless Network Endpoint Groups (Serverless NEGs)
A Network Endpoint Group (NEG) specifies a group of backend endpoints. While Zonal NEGs contain VM IP/port pairs or GKE Pod IPs, Serverless NEGs point directly to serverless compute engines:
- Cloud Run services
- App Engine apps (standard and flexible)
- Cloud Functions (HTTP-triggered 2nd gen / Cloud Run functions)
Multi-Region Cloud Run Active-Active Pattern
By attaching multiple regional Serverless NEGs to a single global Backend Service, the Global External ALB directs each client to the nearest Cloud Run region. Serverless NEGs do not support ordinary backend health checks or balancing-mode capacity settings. Where supported, explicitly configure outlier detection to reduce traffic to failing regions; it is not a guaranteed instantaneous regional failover or overflow mechanism. Test dependency failures as well as service failures.
## Create a Serverless NEG pointing to Cloud Run in us-central1
gcloud compute network-endpoint-groups create neg-cr-us \
--region=us-central1 \
--network-endpoint-type=serverless \
--cloud-run-service=order-service
## Create a Serverless NEG pointing to Cloud Run in europe-west1
gcloud compute network-endpoint-groups create neg-cr-eu \
--region=europe-west1 \
--network-endpoint-type=serverless \
--cloud-run-service=order-service
## Create a global Backend Service
gcloud compute backend-services create orders-backend-service \
--global \
--load-balancing-scheme=EXTERNAL_MANAGED \
--protocol=HTTPS
## Attach both regional Serverless NEGs to the global Backend Service
gcloud compute backend-services add-backend orders-backend-service \
--global \
--network-endpoint-group=neg-cr-us \
--network-endpoint-group-region=us-central1
gcloud compute backend-services add-backend orders-backend-service \
--global \
--network-endpoint-group=neg-cr-eu \
--network-endpoint-group-region=europe-west1
Session Affinity Options
For supported VM/GKE backends, backend-service session affinity can improve locality. Serverless NEG backend services do not support load-balancer session affinity; configure Cloud Run's own best-effort affinity when needed. Store authoritative session state externally so failover and scaling do not lose it.
+--------------------------------------------------------------------------+
| Session Affinity Decision Matrix |
+--------------------------------------------------------------------------+
| |
| Are clients connecting through corporate NAT proxies or shared gateways? |
| | |
| +--- YES ---> Use GENERATED_COOKIE |
| | (Sets 'GCLB' cookie; prevents IP hotspot skewing) |
| | |
| +--- NO ----> Do clients send a unique tracking header? |
| | |
| +--- YES ---> Use HEADER_FIELD |
| | (Hashes header, e.g., 'X-User-ID') |
| | |
| +--- NO ----> Use CLIENT_IP |
| (2-tuple hash of src/dest IP) |
+--------------------------------------------------------------------------+
NONE(Default): Requests are distributed across backends using round-robin or least-loaded algorithms. No stickiness.CLIENT_IP: Hashes the client's source IP address. Critical Pitfall: If thousands of users access the application from behind a single corporate enterprise NAT gateway or university proxy, all users share the same source IP. The load balancer hashes them to the exact same backend instance, creating an extreme performance bottleneck (hotspot) while other instances sit idle.GENERATED_COOKIE: The load balancer issues an HTTP session cookie (namedGCLB) on the first response. Subsequent requests containing this cookie are routed to the same backend instance. If the instance fails or scale-down occurs, the cookie is regenerated for a healthy instance. Cookie-based affinity avoids grouping all users solely by a shared NAT address, but distribution remains best effort.HEADER_FIELD: Hashes the value of an arbitrary HTTP request header specified by the developer (e.g.,X-Organization-TokenorAuthorization).
Edge Security Integration: Google Cloud Armor
Cloud Armor attaches directly to the Backend Services of External Application Load Balancers, inspecting traffic at the edge before packets can reach backend compute instances or Serverless NEGs.
- Layer 7 WAF & OWASP Top 10: Cloud Armor contains preconfigured rules for SQL injection (
sqli-v33-stable), Cross-Site Scripting (xss-v33-stable), Local File Inclusion (lfi-v33-stable), and Remote Code Execution (rce-v33-stable). - Adaptive Rate Limiting: Enforces maximum request thresholds per client IP or custom header over configurable time windows, with an automatic ban period (e.g., blocking an IP for 10 minutes if it exceeds 100 requests in 1 minute).
- Geo-Fencing: Evaluates client origin IP geolocations using ISO 3166-1 alpha-2 country codes:
origin.region_code == 'CN' || origin.region_code == 'RU' - Preview Mode: Allows developers to test new WAF rules against live production traffic without dropping packets; matched requests emit an audit log with the evaluated disposition.
Edge Caching: Cloud CDN
Cloud CDN leverages Google's globally distributed edge caching infrastructure to terminate client requests at the PoP, dramatically reducing origin server load and delivery latency.
- Enabling CDN: Can be enabled on supported backend services and backend buckets; verify compatibility with the selected backend and other features.
- Cache Modes:
CACHE_ALL_STATIC: Automatically caches static web assets (images, CSS, JS, fonts) using eligible content types and cacheability rules; it still respects origin directives such asprivate,no-store, andno-cache.USE_ORIGIN_HEADERS: Strictly respects originCache-Controlresponse headers (public,max-age,s-maxage,no-store).FORCE_CACHE_ALL: Caches all responses regardless of origin headers (used with extreme caution).
- Cache Invalidation: If an urgent deployment modifies cached assets, invalidations are requested globally; allow for propagation via URL maps:
gcloud compute url-maps invalidate-cdn-cache web-url-map \ --path="/static/v2/*" - Signed URLs & Signed Cookies: Protects private media streams and restricted downloads by verifying HMAC cryptographic signatures before serving cached assets.
Official references checked October 18, 2026: Google Cloud 1.
A global enterprise runs a containerized microservice on Cloud Run across two regions: us-central1 and europe-west1. The engineering team must expose this microservice on a single global Anycast IPv4 address, inspect all incoming HTTP requests for OWASP Top 10 vulnerabilities (including SQL injection), and serve static assets directly from edge caches. Which load balancing architecture satisfies these requirements?
Deploy a Global External Application Load Balancer using Premium Network Tier, configure Serverless NEGs pointing to each Cloud Run region, and attach a Cloud Armor security policy and Cloud CDN to the backend service.
Deploy a Regional External Application Load Balancer in each region using Standard Network Tier, configure Zonal NEGs, and enable Cloud Armor on the forwarding rule.
Deploy a Cross-Region Internal Application Load Balancer with Internet NEGs pointing to the default Cloud Run .run.app hostnames, and apply Cloud Armor at the URL map level.
Deploy an External Proxy Network Load Balancer (TCP/SSL) with Cloud CDN enabled, and attach a Cloud Armor backend policy to the target TCP proxy.
A corporate payroll web application runs on Compute Engine managed instance groups. Company branch offices access this application through an outbound enterprise NAT gateway, masking thousands of internal corporate users behind three shared public IP addresses. When users navigate the stateful application, employees from the same branch experience severe latency because all of their requests are directed to a single overloaded backend instance. How should the cloud developer resolve this issue while maintaining sticky sessions?
Switch the backend service session affinity setting to CLIENT_IP with a consistent hash ring.
Change the session affinity configuration to GENERATED_COOKIE on the backend service.
Configure the forwarding rule to use Standard Network Tier to bypass the regional proxy.
Set the backend service session affinity to NONE and configure round-robin DNS records.
Sections you finish are checked off in the contents.