Microsoft Azure — Azure Load Balancer
Public and internal load balancer, frontend IP, backend pools, health probes, and load-balancing rules..
20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.
- ✓Azure subscription with contributor access, Azure CLI 2.50+, basic understanding of networking (TCP/IP, subnets, NSGs), familiarity with Azure portal and resource groups.
Azure Load Balancer is like having a specialized tool that handles load balancer in the Microsoft cloud — you manage the configuration, Azure handles the infrastructure.
Azure is Microsoft's cloud computing platform offering over 200 services. This article covers azure load balancer with production-ready configurations, best practices, and hands-on examples.
Azure Load Balancer: The Stateless Workhorse
Azure Load Balancer (ALB) operates at Layer 4 of the OSI model, distributing TCP/UDP traffic across healthy backend instances. Unlike Application Gateway (Layer 7), ALB is stateless—it doesn't inspect payloads. This makes it ideal for high-throughput, low-latency scenarios like database replicas, DNS servers, or any workload where you need raw packet forwarding. ALB supports both public (inbound internet) and internal (private VNet) load balancing. It uses a five-tuple hash (source IP, source port, destination IP, destination port, protocol) to maintain session affinity when configured with 'Client IP' persistence. Crucially, ALB does not terminate TLS; that's a job for a reverse proxy or Application Gateway. In production, you'll often pair ALB with a TLS terminator behind it. ALB's health probes are configurable: HTTP, HTTPS, or TCP. For TCP, a simple SYN-ACK check suffices; for HTTP, you can probe a specific endpoint like /health. Always set a short probe interval (5s) and low unhealthy threshold (2) to detect failures fast. ALB's backend pools can be VMs, VMSS, or IP addresses (including on-prem via VPN). Standard SKU is mandatory for production—Basic SKU lacks availability zones and SLA.
Backend Pools: Choosing the Right Membership Type
Azure Load Balancer supports three backend pool membership types: Virtual Machines, Virtual Machine Scale Sets (VMSS), and IP addresses (including on-premises). For production, VMSS is the recommended approach because it integrates with autoscaling and rolling upgrades. When using VMs, you must manually add/remove them from the backend pool—error-prone during scaling events. IP-based pools allow you to target any IP, including on-prem servers via VPN or ExpressRoute, but you lose automatic health probe integration with Azure VMs. For VMSS, ALB automatically adds new instances and removes terminated ones. However, note that ALB does not wait for connection draining before removing an instance—it stops sending new connections immediately. To avoid dropped connections, configure connection draining (idle timeout) on the load balancing rule. The default idle timeout is 4 minutes; set it to match your application's longest request. When using VMSS with a custom image, ensure the health probe endpoint is responsive before the instance is marked healthy. A common pitfall: the application starts listening on port 80 before it's ready to serve traffic, causing 502 errors. Use a startup script that delays health probe response until the app is fully initialized.
Health Probes: The Canary in the Coal Mine
Health probes are ALB's mechanism to determine backend instance health. Three protocols: TCP, HTTP, HTTPS. TCP probe checks if the port is listening—fast but shallow. HTTP/HTTPS probes check a specific endpoint (e.g., /health) and expect a 200 OK. Always prefer HTTP probes for application-level health. Design your /health endpoint to verify dependencies: database connectivity, cache, disk space. A common mistake is a health endpoint that returns 200 even when the app is broken (e.g., stuck in a deadlock). Implement a liveness check that fails fast. Probe configuration: interval (default 15s), unhealthy threshold (default 2), healthy threshold (default 2). For production, set interval to 5s, unhealthy threshold to 2, healthy threshold to 1. This gives ~10s detection of failure. Be careful with probe port—it must match the backend port unless you use a separate health port. ALB probes originate from Azure's infrastructure IP range (168.63.129.16). Ensure your NSG allows inbound traffic from this IP on the probe port. If you block it, all instances will be marked unhealthy. Also, probes are sent from all Azure regions where the LB is deployed; for cross-region, you need to allow broader ranges.
Load Balancing Rules and Session Persistence
Load balancing rules define how traffic is distributed. Each rule binds a frontend IP:port to a backend pool:port. You can have multiple rules for different protocols (e.g., TCP 80, TCP 443). Session persistence (affinity) is optional: None, Client IP, or Client IP and Protocol. For stateless apps, use None. For stateful apps (e.g., shopping cart), use Client IP. However, Client IP persistence can cause uneven distribution if many users share the same IP (NAT). For better distribution, use Client IP and Protocol, which adds the protocol to the hash. ALB uses a five-tuple hash by default; session persistence modifies the hash to include only the specified fields. Important: session persistence is not sticky sessions in the traditional sense—it's based on the hash, not a cookie. If a backend goes down, the hash is recalculated, and traffic may go to a different backend. For true sticky sessions, use Application Gateway with cookie-based affinity. Also, note that session persistence is per rule, not per backend. If you have multiple rules, each can have its own persistence setting. In production, avoid session persistence for high-availability; design your app to be stateless or use a distributed cache (Redis) for session state.
Outbound Rules: SNAT and Source NAT
Azure Load Balancer provides outbound connectivity for backend instances via Source Network Address Translation (SNAT). When a VM in the backend pool initiates outbound traffic, ALB translates the source IP to the frontend public IP. This is critical for VMs that need to reach the internet (e.g., for updates, external APIs). Outbound rules are configured on the load balancer. You define which frontend IP to use, which backend pool, and the port allocation strategy. The default SNAT port allocation is 1024 ports per VM, but you can adjust with 'Manual' or 'Use Default'. For production, use 'Manual' and allocate based on expected concurrent connections. A common failure: SNAT port exhaustion. Each outbound connection consumes a port; when all ports are used, new connections fail. Symptoms: intermittent connectivity, timeouts. To mitigate, use multiple frontend IPs (up to 16) to increase port pool. Also, consider using Azure NAT Gateway for larger scale—it provides 64,512 ports per IP and scales better. Outbound rules are only available on Standard SKU. Basic SKU uses default outbound access (no control). Always configure explicit outbound rules to avoid relying on default behavior, which can change.
High Availability: Availability Zones and Cross-Region
Standard SKU ALB supports availability zones. You can deploy a zone-redundant frontend (spans all zones) or zonal (pinned to one zone). Zone-redundant is recommended for production—it survives a zone outage. Backend VMs should also be distributed across zones. For cross-region load balancing, Azure offers Cross-Region Load Balancer (preview). It distributes traffic across regional ALBs using a global frontend IP. This is useful for active-passive or active-active multi-region deployments. Cross-region LB uses a health probe on the regional LB's frontend. If a region fails, traffic is redirected to the next healthy region. However, cross-region LB does not support session persistence—each request may go to a different region. For stateful apps, use Traffic Manager or Front Door instead. In production, always deploy ALB in a zone-redundant configuration. Also, ensure backend VMs are in the same zones as the frontend to avoid cross-zone latency. For critical workloads, combine ALB with Azure Front Door for global load balancing and DDoS protection.
Monitoring and Diagnostics: Logs, Metrics, Alerts
Azure Load Balancer integrates with Azure Monitor for metrics and logs. Key metrics: Packet Count, Byte Count, SNAT Connection Count, Health Probe Status, Data Path Availability. Set alerts on Health Probe Status (if backend count drops below threshold) and SNAT Connection Count (if >80% utilized). For deep diagnostics, enable NSG flow logs and LB logs. LB logs include load balancer rule events and health probe events. However, LB logs are verbose; enable only for troubleshooting. Use Azure Monitor Workbooks to create dashboards. A common issue: data path availability metric shows 0 when the LB frontend is unreachable (e.g., NSG blocking). Always monitor this metric. For proactive monitoring, set up action groups to notify on alert triggers. Also, use Azure Resource Health to check LB health. In production, we recommend a minimum of 90% data path availability alert. For SNAT, set a warning at 70% and critical at 90%. Additionally, log backend instance health changes to detect flapping instances (rapidly toggling healthy/unhealthy). Flapping can indicate misconfigured health probes or application instability.
Troubleshooting Common Failures
Even with proper configuration, things go wrong. Here are common failure modes and how to diagnose them. 1) Backend marked unhealthy: Check NSG rules—ensure 168.63.129.16 is allowed on probe port. Verify the health endpoint returns 200. Use az network lb probe show to see probe status. 2) No traffic to backend: Check load balancing rule mapping. Verify backend pool has instances. Use az network lb list-backend-address-pool to list addresses. 3) Intermittent timeouts: Likely SNAT port exhaustion. Check SnatConnectionCount metric. Increase frontend IPs or reduce idle timeout. 4) Connection resets: TCP reset enabled? If idle timeout is too low, connections may be reset prematurely. Increase idle timeout or disable TCP reset if not needed. 5) Uneven traffic distribution: Session persistence causing imbalance. Check load distribution setting. Use SourceIPProtocol for better distribution. 6) Cross-region issues: Ensure regional LBs are healthy. Cross-region LB uses health probes on regional frontends. If a regional LB is down, traffic fails over. Test failover by stopping a regional LB. Always have a troubleshooting runbook with these steps. Use Azure Network Watcher's IP flow verify to test connectivity. Also, enable diagnostic logs for the LB to capture rule hits.
Production Deployment with Infrastructure as Code
Manual creation of ALB is error-prone. Use Infrastructure as Code (IaC) with Terraform or ARM/Bicep. Below is a Terraform example for a production-grade ALB with zone redundancy, health probes, and outbound rules. Key practices: parameterize everything, use remote state, and implement CI/CD pipelines. In the Terraform config, note the use of for_each to create multiple frontend IPs for SNAT. Also, the health probe depends on the backend pool. Always set depends_on explicitly to avoid race conditions. For production, use modules to encapsulate LB configuration. Test changes in a staging environment first. Use Terraform workspaces for environment separation. Also, enable prevent_destroy on critical resources like the LB itself. For secrets (e.g., health endpoint passwords), use Azure Key Vault. Finally, integrate with Azure Policy to enforce SKU Standard and zone-redundancy. This ensures compliance across all subscriptions.
azurerm_backend_container for remote state.prevent_destroy on critical resources and run terraform plan in CI with manual approval.Security: NSGs, DDoS, and Private Endpoints
Azure Load Balancer itself is not a security appliance—it's a traffic distributor. Security must be implemented at the network and application layers. First, Network Security Groups (NSGs) on backend subnets: allow only traffic from the LB frontend IP (or VNet) and health probe source IP (168.63.129.16). Deny all other inbound. For public ALB, enable Azure DDoS Protection Standard on the VNet. This protects against volumetric attacks. ALB does not have built-in WAF; use Azure Application Gateway or Front Door for Layer 7 filtering. For internal ALB, use Private Endpoints to expose services privately. ALB can be placed behind a firewall (e.g., Azure Firewall) for traffic inspection. However, this adds latency. For compliance, enable diagnostic logs and send to Log Analytics. Use Azure Policy to enforce that ALBs are Standard SKU and have NSGs attached. Also, restrict outbound traffic from backend VMs using NSGs or Azure Firewall. Never allow unrestricted outbound. Finally, use Managed Identities for VMs to access Azure resources securely, avoiding connection strings in code.
Cost Optimization and Performance Tuning
Azure Load Balancer pricing is based on rules and data processed. Standard SKU has a fixed hourly cost plus per-GB data processing. To optimize: consolidate rules—each rule adds cost. Use a single rule for multiple ports if possible (e.g., TCP 80-443). For outbound, use NAT Gateway instead of LB outbound rules for better port scalability and lower cost at high volumes. NAT Gateway costs ~$0.045/hour plus data processing, but provides 64,512 ports per IP. For performance, ALB can handle millions of flows. However, ensure backend VMs are sized appropriately. Use accelerated networking on VMs for higher throughput. Also, enable TCP segmentation offload (TSO) on the OS. Monitor latency metrics: ALB adds <1ms latency. If you see higher, check backend response times. For global performance, use cross-region LB or Front Door. In production, right-size your backend pool: too few instances cause overload; too many waste cost. Use autoscaling based on CPU or request count. Finally, consider reserved instances for predictable workloads to save up to 72% on VM costs.
Migration from Basic to Standard SKU
If you're still on Basic SKU, migrate to Standard. Basic SKU is being deprecated and lacks features. Migration steps: 1) Create a new Standard SKU ALB in the same region. 2) Configure frontend IP, backend pool, probes, and rules matching the Basic LB. 3) Update DNS to point to the new LB's public IP. 4) Gradually shift traffic by updating application configurations. 5) Delete the old Basic LB. Important: Standard SKU requires explicit outbound rules; Basic had default outbound. Ensure you configure outbound rules before cutting over. Also, Standard SKU has different NSG requirements—it doesn't allow inbound traffic from the internet unless explicitly allowed. Test in a staging environment first. Use Azure Traffic Manager to perform a blue-green migration. During migration, monitor metrics to ensure no impact. Rollback plan: keep the Basic LB running until traffic is fully migrated. Note that Basic LB does not support availability zones, so if you need HA, you must redeploy backends across zones.
| File | Command / Code | Purpose |
|---|---|---|
| create-alb.sh | az network lb create \ | Azure Load Balancer |
| configure-backend-pool.sh | az network lb address-pool create \ | Backend Pools |
| health-endpoint.py | from flask import Flask, jsonify | Health Probes |
| create-lb-rule.sh | az network lb rule create \ | Load Balancing Rules and Session Persistence |
| create-outbound-rule.sh | az network lb outbound-rule create \ | Outbound Rules |
| zone-redundant-alb.sh | az network public-ip create \ | High Availability |
| setup-alerts.sh | az monitor metrics alert create \ | Monitoring and Diagnostics |
| troubleshoot.sh | az network lb probe show \ | Troubleshooting Common Failures |
| main.tf | resource "azurerm_lb" "prod" { | Production Deployment with Infrastructure as Code |
| nsg-rules.sh | az network nsg rule create \ | Security |
| cost-estimate.sh | az consumption prices list \ | Cost Optimization and Performance Tuning |
| migrate.sh | az network lb create --resource-group prod-rg --name prod-lb-std --sku Standard ... | Migration from Basic to Standard SKU |
Key takeaways
Common mistakes to avoid
3 patternsNot planning load balancer properly before deployment
Ignoring Azure best practices for load balancer
Overlooking cost implications of load balancer
Interview Questions on This Topic
Explain Azure Load Balancer and its use cases.
Frequently Asked Questions
20+ years shipping production infrastructure and CI/CD at scale. Written from production experience, not tutorials.
That's Azure. Mark it forged?
8 min read · try the examples if you haven't