0
0
Azurecloud~5 mins

VNet-to-VNet connectivity in Azure - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: VNet-to-VNet connectivity
O(n²)
Understanding Time Complexity

When connecting two virtual networks (VNets) in Azure, it's important to understand how the time to set up and maintain this connection changes as the number of VNets grows.

We want to know: how does the work increase when more VNets are connected?

Scenario Under Consideration

Analyze the time complexity of creating VNet-to-VNet connections using Azure PowerShell.

# Connect multiple VNets by creating peerings
foreach ($vnet1 in $vnets) {
  foreach ($vnet2 in $vnets) {
    if ($vnet1 -ne $vnet2) {
      New-AzVirtualNetworkPeering -Name "$($vnet1.Name)-to-$($vnet2.Name)" `
        -VirtualNetwork $vnet1 -RemoteVirtualNetworkId $vnet2.Id -AllowForwardedTraffic
    }
  }
}

This script creates peering connections between every pair of VNets in a list.

Identify Repeating Operations

We look for the main repeated actions:

  • Primary operation: Creating a peering connection between two VNets using New-AzVirtualNetworkPeering.
  • How many times: For each VNet, a peering is created with every other VNet, so the number of peerings grows with the square of the number of VNets.
How Execution Grows With Input

As the number of VNets increases, the number of peering connections grows quickly because each VNet connects to all others.

Input Size (n)Approx. Api Calls/Operations
1090
1009,900
1000999,000

Pattern observation: The number of operations grows roughly by the square of the number of VNets.

Final Time Complexity

Time Complexity: O(n²)

This means that if you double the number of VNets, the number of peering connections roughly quadruples, so the work grows fast as you add more VNets.

Common Mistake

[X] Wrong: "Adding more VNets only increases the number of connections a little bit because each VNet connects to just one other."

[OK] Correct: Each VNet connects to every other VNet, so the total connections grow much faster, not just by one per new VNet.

Interview Connect

Understanding how connection counts grow helps you design scalable network setups and shows you can think about how cloud resources behave as systems grow.

Self-Check

What if we only connected each VNet to a fixed number of other VNets instead of all? How would the time complexity change?