A connection that sat idle for a few minutes fails on its next write. The client hangs for a long time, then reports Connection reset, broken pipe or a read timeout. Busy connections never fail. Every stateful hop in Azure keeps a flow table entry for each TCP connection and removes it after an idle timeout. If nothing tells the two endpoints, the next packet hits a hop that no longer knows the flow.
Common victims are connection pools (JDBC, Npgsql, SqlClient, Redis clients), AMQP and MQTT links, long-running SQL queries that return nothing for minutes, SSH sessions, and gRPC streams.
Symptom, cause and fix
| Symptom | Likely cause | Fix |
|---|---|---|
| First request after ~4 minutes idle hangs, then fails | Flow expired on a hop with TCP reset disabled; the client retransmits into nothing | Enable TCP reset; set keepalives below the timeout |
Immediate ECONNRESET after idle | Flow expired with TCP reset enabled (the correct behaviour) | Keepalives, or have the pool validate and retire idle connections |
| Only outbound connections to the internet fail | NAT Gateway, LB outbound rule or instance public IP timeout | Raise the outbound idle timeout; add keepalives |
| Only traffic through the hub firewall fails | Azure Firewall TCP idle timeout | Keepalives below the firewall timeout; don't rely on raising it |
| Long-running query fails at the same minute mark every time | Query returns no rows for longer than the shortest timeout on the path | Keepalives on the DB driver, or stream progress |
| Fails only after a VM restart or image change | Kernel or registry keepalive tuning was lost | Put the setting in cloud-init, a GPO or the image |
Check 1: map every stateful hop on the path
The effective timeout is the shortest one on the path. List the hops before you change anything:
- Standard Load Balancer: each load-balancing rule, inbound NAT rule and outbound rule has its own idle timeout. The default is 4 minutes.
- NAT Gateway: one idle timeout for the gateway. The default is 4 minutes.
- Instance-level public IP on a NIC: has its own idle timeout setting.
- Azure Firewall: has a fixed TCP idle timeout that you can't set yourself. Check the current Azure Firewall FAQ for the value and whether a support request can change it.
- NVAs, Application Gateway, on-premises firewalls over VPN or ExpressRoute: each has its own timers.
Read the current values with the CLI:
# Load balancer rules and outbound rules
az network lb rule list -g rg-app --lb-name lb-app \
--query "[].{rule:name, idle:idleTimeoutInMinutes, reset:enableTcpReset}" -o table
az network lb outbound-rule list -g rg-app --lb-name lb-app \
--query "[].{rule:name, idle:idleTimeoutInMinutes, reset:enableTcpReset}" -o table
# NAT Gateway
az network nat gateway show -g rg-net -n natgw-spoke1 \
--query "{idle:idleTimeoutInMinutes, subnets:subnets[].id}" -o json
# Public IPs attached to NICs
az network public-ip list -g rg-app \
--query "[].{ip:name, idle:idleTimeoutInMinutes, attachedTo:ipConfiguration.id}" -o table
To find which path a subnet actually takes, check effective routes on a NIC. A 0.0.0.0/0 route to a VirtualAppliance next hop means the firewall is in the path. Without one, egress uses NAT Gateway (if it's on the subnet), then outbound rules, then the instance public IP. The precedence is described in the Azure outbound connectivity documentation.
az network nic show-effective-route-table -g rg-app -n vm-app01-nic -o table
Check 2: prove it is an idle expiry
Reproduce it with a timer. Open a connection, leave it idle for a set time, then send. If 3 minutes works and 5 minutes fails, you have found the boundary.
On Linux, see whether the application socket uses keepalive at all:
# timer:(keepalive,...) means SO_KEEPALIVE is set; no timer means it isn't
ss -tno state established '( dport = :5432 )'
Capture the failure on the client. If TCP reset is off, you will see the client retransmitting with no reply. If it is on, you will see an RST arrive straight after the first send.
sudo tcpdump -ni eth0 'host 10.40.2.15 and tcp port 5432' -w /tmp/idle.pcap
When you need to see both sides, Network Watcher packet capture on the server VM shows whether the packet arrived at all.
Tip
Linux retransmits for a long time (governed by
net.ipv4.tcp_retries2) before giving up. That explains the "hangs for 15 minutes" reports. Enabling TCP reset on the load balancer turns these into failures that happen at once, which pools can recover from.
Fix 1: enable TCP reset and set timeouts deliberately
TCP reset on idle makes the load balancer send an RST to both endpoints when it expires the flow. Clients then fail at once instead of hanging. Turn it on for every rule:
az network lb rule update -g rg-app --lb-name lb-app -n rule-https \
--idle-timeout 15 --enable-tcp-reset true
az network lb outbound-rule update -g rg-app --lb-name lb-app -n outbound-all \
--idle-timeout 15 --enable-tcp-reset true
az network nat gateway update -g rg-net -n natgw-spoke1 --idle-timeout 10
Allowed ranges differ by resource. The CLI rejects values outside them; check --help for your version.
Warning
Longer idle timeouts hold SNAT ports for longer. On a NAT Gateway or an outbound rule that already runs close to its port limit, raising the timeout from 4 to 30 minutes can cause SNAT exhaustion. Check the SNAT metrics before and after you change it, and prefer keepalives over very long timeouts.
You can't tune Azure Firewall's TCP idle timeout like this. Design for it: keepalives on clients behind the firewall must fire well below its timeout.
Fix 2: keepalives below the shortest timeout
Keepalives keep the flow entry fresh. Two conditions apply: the application must enable SO_KEEPALIVE on the socket, and the interval must be shorter than the shortest idle timeout on the path. Leave a margin, for example 2 minutes against a 4-minute timeout.
Linux kernel defaults wait 2 hours before the first probe, which is useless here:
cat <<'EOF' | sudo tee /etc/sysctl.d/90-azure-keepalive.conf
net.ipv4.tcp_keepalive_time = 120
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 5
EOF
sudo sysctl --system
Windows reads KeepAliveTime in milliseconds. A reboot is needed:
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' `
-Name KeepAliveTime -Value 120000 -Type DWord
Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' `
-Name KeepAliveInterval -Value 30000 -Type DWord
Driver-level settings are often better, because they apply only to the connections that need them and also work in containers and PaaS:
| Client | Setting |
|---|---|
| PostgreSQL libpq / psycopg | keepalives=1 keepalives_idle=120 keepalives_interval=30 |
| Npgsql | Keepalive=120 or Tcp Keepalive=true with time/interval (names vary by version) |
| OpenSSH | ServerAliveInterval 120 in ~/.ssh/config |
| JDBC (PostgreSQL) | tcpKeepAlive=true; the interval comes from the OS |
| Connection pools | Set max idle lifetime below the shortest timeout so idle connections are retired before they expire |
For AKS nodes, put kernel keepalive tuning in a node pool --linux-os-config file, or set it in each pod's securityContext sysctls where your cluster allows it. In App Service and Functions you don't control the kernel, so set keepalives in the driver.
Prevent it next time
- Turn on TCP reset for every Standard Load Balancer rule in your IaC modules. Add an Azure Policy audit for
enableTcpResetset to false. - Record the shortest idle timeout of each standard network path (spoke to internet, spoke to hub, on-premises) in the landing zone documentation.
- Set pool max idle lifetime and driver keepalives in your shared application configuration, not separately in each app.
- Add a synthetic check that opens a connection, waits past the timeout and sends a query. It catches regressions when someone adds a firewall or NVA to the path.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get