Real-World Problem: The Cost of a Single Uplink Failure
During an afternoon deployment last quarter, a construction crew sliced through our branch office’s primary 500 Mbps fiber line. In an instant, active SSH sessions froze, CI/CD webhooks died mid-flight, and database replication stalled. We kept a secondary 100 Mbps LTE gateway on standby, but swapping patch cables and updating default gateways by hand ate 22 minutes of frantic troubleshooting. That downtime delayed a production release and cost our client real money.
Single-provider setups carry high operational risk. Peak-hour congestion slows user traffic, and routine maintenance windows can knock services offline without warning. Adding a secondary ISP solves physical path redundancy, but consumer routers cannot balance dual connections properly. Traffic usually pools on one pipe while the other idles. Worse, packet hashing scatters active TCP streams across alternating public IPs, dropping secure sessions continuously.
Root Cause Analysis: Why Standard Linux Routing Falls Short
Standard Linux routing tables cannot manage multi-gateway topologies cleanly. By default, the kernel maintains only one active default gateway (default via 192.168.1.1 dev eth0). When you bind two WAN interfaces, the kernel assigns precedence to the route with the lower metric. All egress traffic flows through that single interface until its physical carrier link drops completely.
Physical drops, however, are rare. Most ISP outages are ‘gray failures’:
- The Ethernet link to the ONT or modem stays UP at 1 Gbps with full carrier signal.
- Peering sessions drop upstream, DNS resolvers fail, or routing loops discard outbound packets.
- The local kernel keeps routing packets into the black hole because the physical port shows link state UP.
Equal-Cost Multi-Path (ECMP) routing hashes flows across multiple interfaces to pool bandwidth. Unfortunately, stateful applications break under this scheme. When a bank portal, VPN client, or VoIP tunnel detects packets from identical sessions arriving from different source IPs, it terminates the connection immediately. You need policy-based routing (PBR) and continuous link monitoring to maintain connection state across dual providers.
Solutions Compared: Scripting vs. BGP vs. mwan3
Network engineers generally solve dual-uplink routing through three patterns:
1. Custom Shell Scripts with Ping Checks and Route Replacement
A simple cron script pings a remote host like 8.8.8.8 every 10 seconds. When pings fail, it executes ip route replace to swap the default gateway.
- Pros: Zero extra packages; consumes minimal RAM (under 2 MB).
- Cons: Fragile. It cannot perform ratio-based balancing, ignores connection state, and causes severe route flapping during intermittent packet loss.
2. Dynamic Routing Protocols (BGP Multi-Homing)
Enterprise setups establish direct BGP peering against upstream transit providers using a registered Autonomous System Number (ASN) and public IP allocation.
- Pros: Inbound and outbound IP addresses stay identical during provider outages. Sub-second convergence.
- Cons: Expensive. Requires at least a /24 IPv4 block, dual enterprise transit contracts, and high-spec edge routers. Unrealistic for branch offices, retail sites, and homelabs.
3. OpenWrt Policy Routing with mwan3
The mwan3 package creates custom Linux routing tables managed by iprule and iptables/nftables marks. Independent probing daemons ping multiple targets through each WAN. Outbound flows follow strict rules based on packet marks, link health, and defined traffic weights.
- Pros: Works with low-cost residential and business DHCP, static, or PPPoE connections. Supports protocol-level steering and sticky sessions.
- Cons: Requires distinct routing metrics and careful host tracking configuration.
The Best Approach: Multi-WAN and Failover Architecture with mwan3
We deploy this architecture across our remote offices. A 500 Mbps symmetrical FTTH connection handles primary traffic, backed up by a 100 Mbps VDSL or 5G modem. Normal operations split traffic 3:1 across both pipes, aggregating throughput. If the fiber cuts, traffic shifts to backup in under 12 seconds with zero dropped HTTPS sessions.
Step 1: Network Interface Preparation
Isolate each uplink onto its own physical switch port or VLAN. Next, assign a unique metric to every interface in /etc/config/network so the kernel can differentiate default routes:
# Configure primary fiber interface (WAN1, metric 10)
uci set network.wan.metric='10'
uci set network.wan.peerdns='0'
uci add_list network.wan.dns='1.1.1.1'
uci add_list network.wan.dns='8.8.8.8'
# Configure secondary backup interface (WAN2, metric 20 on eth2)
uci set network.wan2=interface
uci set network.wan2.proto='dhcp'
uci set network.wan2.device='eth2'
uci set network.wan2.metric='20'
uci set network.wan2.peerdns='0'
uci add_list network.wan2.dns='9.9.9.9'
uci add_list network.wan2.dns='8.8.4.4'
# Bind WAN2 to the WAN firewall zone
uci add_list firewall.@zone[1].network='wan2'
uci commit network
uci commit firewall
/etc/init.d/network restart
/etc/init.d/firewall restart
Step 2: Installing and Configuring mwan3
Install the package along with its LuCI web dashboard:
opkg update
opkg install mwan3 luci-app-mwan3
Now configure /etc/config/mwan3. Three core primitives govern the routing logic: Interfaces define health monitors, Members bind interfaces to metrics and weights, and Policies group members together.
# 1. Interface health tracking
uci set mwan3.wan=interface
uci set mwan3.wan.enabled='1'
uci set mwan3.wan.family='ipv4'
uci add_list mwan3.wan.track_ip='1.1.1.1'
uci add_list mwan3.wan.track_ip='8.8.8.8'
uci set mwan3.wan.reliability='1'
uci set mwan3.wan.count='1'
uci set mwan3.wan.timeout='2'
uci set mwan3.wan.interval='3'
uci set mwan3.wan.down='3'
uci set mwan3.wan.up='5'
uci set mwan3.wan2=interface
uci set mwan3.wan2.enabled='1'
uci set mwan3.wan2.family='ipv4'
uci add_list mwan3.wan2.track_ip='9.9.9.9'
uci add_list mwan3.wan2.track_ip='8.8.4.4'
uci set mwan3.wan2.reliability='1'
uci set mwan3.wan2.count='1'
uci set mwan3.wan2.timeout='2'
uci set mwan3.wan2.interval='3'
uci set mwan3.wan2.down='3'
uci set mwan3.wan2.up='5'
# 2. Member definitions (weight sets ratio; metric sets failover order)
uci set mwan3.wan_m1_w3=member
uci set mwan3.wan_m1_w3.interface='wan'
uci set mwan3.wan_m1_w3.metric='1'
uci set mwan3.wan_m1_w3.weight='3'
uci set mwan3.wan2_m1_w1=member
uci set mwan3.wan2_m1_w1.interface='wan2'
uci set mwan3.wan2_m1_w1.metric='1'
uci set mwan3.wan2_m1_w1.weight='1'
uci set mwan3.wan2_m2_w1=member
uci set mwan3.wan2_m2_w1.interface='wan2'
uci set mwan3.wan2_m2_w1.metric='2'
uci set mwan3.wan2_m2_w1.weight='1'
# 3. Policy: Balanced (Active-Active 3:1 ratio across WAN1 and WAN2)
uci set mwan3.balanced=policy
uci add_list mwan3.balanced.use_member='wan_m1_w3'
uci add_list mwan3.balanced.use_member='wan2_m1_w1'
# 4. Policy: Strict Failover (WAN1 primary; WAN2 activates only if WAN1 drops)
uci set mwan3.wan_primary=policy
uci add_list mwan3.wan_primary.use_member='wan_m1_w3'
uci add_list mwan3.wan_primary.use_member='wan2_m2_w1'
uci commit mwan3
/etc/init.d/mwan3 restart
Step 3: Crafting Rules and Preserving Stateful Connections
OpenWrt evaluates rules in linear order from top to bottom. Bank logins, SSH terminal sessions, and payment gateways break if packets from one session switch source IPs mid-stream. We solve this by pinning TCP port 443 traffic to a sticky session timer:
# Pin HTTPS sessions to the original uplink for 3600 seconds
uci set mwan3.https_rule=rule
uci set mwan3.https_rule.dest_port='443'
uci set mwan3.https_rule.proto='tcp'
uci set mwan3.https_rule.use_policy='balanced'
uci set mwan3.https_rule.sticky='1'
uci set mwan3.https_rule.timeout='3600'
# Route all remaining traffic via the balanced 3:1 policy
uci set mwan3.default_rule=rule
uci set mwan3.default_rule.dest_ip='0.0.0.0/0'
uci set mwan3.default_rule.use_policy='balanced'
uci commit mwan3
/etc/init.d/mwan3 reload
Step 4: Operational Verification and Diagnostic Commands
Confirm interface tracking states and custom routing table bindings from the CLI:
# View live tracking daemon status and active link states
mwan3 status
# Inspect packet-marking policy rules
ip rule show
# Review custom mwan3 routing tables
ip route show table mwan3_wan
ip route show table mwan3_wan2
# Trace which uplink handles traffic toward a specific destination
mwan3 check 1.1.1.1
Production Hardening Tips
- Track Diverse Anycast Targets: Never track your local ISP gateway address. That gateway stays reachable even if overseas subsea cables sever. Instead, probe distinct public resolvers such as
1.1.1.1(Cloudflare, AS13335) and8.8.8.8(Google, AS15169). Keepreliabilityat1so a single remote resolver glitch will not spuriously mark your line down. - Calculate Flapping Windows: With
interval=3,timeout=2, anddown=3, the daemon declares an interface offline after roughly 9 to 11 seconds of sustained packet loss. Settingup=5forces a recovering link to pass 15 seconds of clean probes before handling traffic, preventing link oscillation during storms. - Prevent Resolver Freezes: Set
peerdns='0'on both uplinks and define static DNS IPs explicitly. When an uplink drops, an unpinned local resolver wastes seconds waiting on dead upstream nameservers before timing out.

