Skip to main content

Choosing Load Balancer or Application Gateway

Build an Azure Load Balancer and Application Gateway over the same VMs and see Layer 4 versus Layer 7 path-based routing in action.

Overview and What You Will Learn

Azure Load Balancer works at Layer 4 and distributes traffic by IP address and port, while Application Gateway works at Layer 7 and can route HTTP requests by URL path, host name, or header. In this lab, you will put both in front of the same two backend VMs and watch Load Balancer spread requests with no regard for the URL, while Application Gateway sends /api/ to one VM and everything else to the other.

Why This Matters in Production

Choosing the wrong layer means rebuilding your traffic path later. Picture a team at a company like Shopify that builds a Load Balancer expecting it to send /api requests to one set of servers and /images to another. It cannot, because Load Balancer never reads the request content, only the destination IP and port. Application Gateway was the right tool from the start, and switching afterward means reworking DNS, certificates, and firewall rules.

Core Principles

The two services sit at different layers of the network stack, and confusing them is one of the most common early networking mistakes. Load Balancer is fast and protocol-agnostic. Application Gateway understands HTTP and does more work per request.

Capability Load Balancer Application Gateway
Layer and traffic Layer 4, any TCP or UDP Layer 7, HTTP and HTTPS
Routing decision IP, port, protocol URL path, host, headers
Extras Very low latency, HA ports TLS termination, WAF, cookie affinity
flowchart TD Q{What kind of
traffic is it?} Q -->|Web, needs URL routing
TLS offload, or WAF| AG[Application Gateway
Layer 7] Q -->|TCP or UDP service
or no content routing| LB[Load Balancer
Layer 4] classDef decision fill:#F59E0B,color:#111827 classDef l7 fill:#8B5CF6,color:#FFFFFF classDef l4 fill:#326CE5,color:#FFFFFF class Q decision class AG l7 class LB l4
Remember

Both services are regional. If you need global HTTP routing across regions, look at Azure Front Door or Traffic Manager in front of them.

Detailed Step-by-Step Practical Lab

This lab creates billable resources, including an Application Gateway and a NAT gateway that charge by the hour. Delete everything when you finish.

How to Build the Network, NSG, and NAT Gateway

Application Gateway v2 needs its own dedicated subnet. The backend subnet gets an NSG, because a Standard Load Balancer keeps backends closed to inbound traffic until a rule allows it, and a NAT gateway, because VMs in new virtual networks have no default outbound internet access for installing packages.

Bash
export RG="rg-lb-lab-mumbai"
az group create --name $RG --location centralindia
az network vnet create \
--resource-group $RG \
--name vnet-lb-lab \
--address-prefix 10.0.0.0/16 \
--subnet-name subnet-backend \
--subnet-prefix 10.0.1.0/24
az network vnet subnet create \
--resource-group $RG \
--vnet-name vnet-lb-lab \
--name subnet-appgw \
--address-prefixes 10.0.2.0/24
az network nsg create --resource-group $RG --name nsg-backend
az network nsg rule create \
--resource-group $RG \
--nsg-name nsg-backend \
--name allow-http \
--priority 100 \
--direction Inbound \
--access Allow \
--protocol Tcp \
--source-address-prefixes Internet \
--destination-port-ranges 80
az network public-ip create \
--resource-group $RG \
--name pip-nat-lab \
--sku Standard
az network nat gateway create \
--resource-group $RG \
--name nat-lab \
--public-ip-addresses pip-nat-lab
az network vnet subnet update \
--resource-group $RG \
--vnet-name vnet-lb-lab \
--name subnet-backend \
--network-security-group nsg-backend \
--nat-gateway nat-lab
How to Create Two Backend VMs That Identify Themselves

Each VM installs nginx and serves a page that names the server and the path, so you can see which VM answered. Both VMs serve both / and /api/, which makes the routing difference easy to see.

Bash
cat > cloud-init-nginx.yaml << 'EOF'
#cloud-config
package_update: true
packages:
- nginx
runcmd:
- mkdir -p /var/www/html/api
- sh -c 'echo "Served by $(hostname), path /" > /var/www/html/index.html'
- sh -c 'echo "Served by $(hostname), path /api/" > /var/www/html/api/index.html'
EOF
for n in 1 2; do
az vm create \
--resource-group $RG \
--name vm-backend-$n \
--image Ubuntu2204 \
--size Standard_B1s \
--vnet-name vnet-lb-lab \
--subnet subnet-backend \
--public-ip-address "" \
--nsg "" \
--admin-username azureadmin \
--generate-ssh-keys \
--custom-data cloud-init-nginx.yaml \
--output none
done
How to Create a Load Balancer for the Same VMs

The Standard Load Balancer gets a public frontend, an HTTP health probe, and a rule that forwards port 80 to the backend pool. Then add each VM's network interface to that pool.

Bash
az network lb create \
--resource-group $RG \
--name lb-lab \
--sku Standard \
--public-ip-address pip-lb-lab \
--frontend-ip-name fe-lab \
--backend-pool-name be-lab
az network lb probe create \
--resource-group $RG \
--lb-name lb-lab \
--name probe-http \
--protocol Http \
--port 80 \
--path /
az network lb rule create \
--resource-group $RG \
--lb-name lb-lab \
--name rule-http \
--protocol Tcp \
--frontend-port 80 \
--backend-port 80 \
--frontend-ip-name fe-lab \
--backend-pool-name be-lab \
--probe-name probe-http
for n in 1 2; do
az network nic ip-config address-pool add \
--resource-group $RG \
--nic-name vm-backend-${n}VMNic \
--ip-config-name ipconfig1 \
--lb-name lb-lab \
--address-pool be-lab
done
How to Test Load Balancer Behavior

The Load Balancer picks a backend per connection using a hash of the connection details, not round robin, and it ignores the URL. Give cloud-init a couple of minutes to finish installing nginx first.

Bash
LB_IP=$(az network public-ip show \
--resource-group $RG --name pip-lb-lab \
--query ipAddress --output tsv)
for i in $(seq 1 10); do curl -s http://$LB_IP/api/; done
How to Create an Application Gateway

Application Gateway deployment can take 10 to 20 minutes. This creates a Standard_v2 gateway with a default backend pool containing only vm-backend-2.

Bash
VM1_IP=$(az vm show --resource-group $RG --name vm-backend-1 \
--show-details --query privateIps --output tsv)
VM2_IP=$(az vm show --resource-group $RG --name vm-backend-2 \
--show-details --query privateIps --output tsv)
az network application-gateway create \
--resource-group $RG \
--name appgw-lab \
--sku Standard_v2 \
--capacity 1 \
--vnet-name vnet-lb-lab \
--subnet subnet-appgw \
--public-ip-address pip-appgw-lab \
--frontend-port 80 \
--http-settings-port 80 \
--http-settings-protocol Http \
--priority 100 \
--servers $VM2_IP
How to Configure Path-Based Routing on Application Gateway

Add a second backend pool that holds only vm-backend-1, then map /api/* to it. A listener can use only one rule, so this lab adds a second listener on port 8080 for the path-based rule. In production you would make your main listener path-based instead.

Bash
az network application-gateway address-pool create \
--resource-group $RG \
--gateway-name appgw-lab \
--name api-backend-pool \
--servers $VM1_IP
az network application-gateway frontend-port create \
--resource-group $RG \
--gateway-name appgw-lab \
--name port8080 \
--port 8080
az network application-gateway http-listener create \
--resource-group $RG \
--gateway-name appgw-lab \
--name listener-path \
--frontend-port port8080
az network application-gateway url-path-map create \
--resource-group $RG \
--gateway-name appgw-lab \
--name path-map-lab \
--paths "/api/*" \
--rule-name api-path-rule \
--address-pool api-backend-pool \
--http-settings appGatewayBackendHttpSettings \
--default-address-pool appGatewayBackendPool \
--default-http-settings appGatewayBackendHttpSettings
az network application-gateway rule create \
--resource-group $RG \
--gateway-name appgw-lab \
--name rule-path \
--rule-type PathBasedRouting \
--http-listener listener-path \
--url-path-map path-map-lab \
--priority 200
Remember

This path-based decision, choosing a backend from the URL, is something Load Balancer structurally cannot do. It never inspects the request, only the destination IP and port.

How to Compare the Two Routing Behaviors

Check that Application Gateway sees both backends as healthy, then repeat the same requests. A 502 error means a backend failed its health probe.

Bash
az network application-gateway show-backend-health \
--resource-group $RG \
--name appgw-lab \
--query "backendAddressPools[].backendHttpSettingsCollection[].servers[].[address, health]" \
--output table
APPGW_IP=$(az network public-ip show \
--resource-group $RG --name pip-appgw-lab \
--query ipAddress --output tsv)
for i in 1 2 3; do curl -s http://$APPGW_IP:8080/api/; done
# Expected: always vm-backend-1
for i in 1 2 3; do curl -s http://$APPGW_IP:8080/; done
# Expected: always vm-backend-2

The Load Balancer mixed both servers for the same path, while Application Gateway pinned each path to its own backend pool.

How to Clean Up
Bash
az group delete --name $RG --yes --no-wait

Production Best Practices & Common Pitfalls

Common Mistake

Choosing Load Balancer for a web application that needs path-based routing or WAF protection, then finding the requirement cannot be met and rebuilding the whole traffic layer around Application Gateway.

Tip

The two are not mutually exclusive. A common pattern puts Application Gateway in front for HTTP routing and WAF, with an internal Load Balancer behind it spreading traffic across backend VMs at Layer 4.

Security

Application Gateway can run a Web Application Firewall that blocks common attacks such as SQL injection. Use the WAF_v2 SKU with a WAF policy for any internet-facing web app, even when you do not need path-based routing.

  • Use Load Balancer for non-HTTP traffic. A custom TCP or UDP service, a database listener, or any workload that never needs content-based routing fits it best.
  • Give Application Gateway v2 its own subnet. The subnet must hold only the gateway, and a /24 leaves room to scale.
  • Use the v2 SKUs. Pick Standard_v2 or WAF_v2, because the older v1 SKUs are retired.
  • Remember that Standard Load Balancer is closed by default. Backends receive no inbound traffic until an NSG rule allows it.
  • Do not expect round robin from Load Balancer. It hashes connection details, so one client may stay on one backend for a long time.
  • Terminate TLS on Application Gateway, not Load Balancer. Load Balancer passes encrypted traffic through untouched, so it cannot inspect or route on it.

Quick Reference & Troubleshooting Commands

Command Description
az network lb create --sku Standard Create a Layer 4 Load Balancer
az network nic ip-config address-pool add Add a VM's NIC to a Load Balancer pool
az network application-gateway create --sku Standard_v2 Create a Layer 7 Application Gateway
az network application-gateway url-path-map create Configure path-based routing rules
az network application-gateway show-backend-health Diagnose 502 errors from unhealthy backends
az network lb list --output table List Load Balancers in the subscription

Explore More in Azure Networking and Traffic Management

All 6 Topics

Frequently Asked Questions

Is Choosing Load Balancer or Application Gateway free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Choosing Load Balancer or Application Gateway topic cover?

Build an Azure Load Balancer and Application Gateway over the same VMs and see Layer 4 versus Layer 7 path-based routing in action.

Found a mistake, an outdated step or a broken link?