Network Security Groups (NSG) in Azure - Time & Space Complexity
When managing Network Security Groups (NSGs), it is important to understand how the time to apply rules grows as you add more rules or associate NSGs with more resources.
We want to know how the number of operations changes when the NSG configuration grows.
Analyze the time complexity of applying multiple NSG rules to multiple network interfaces.
# Create NSG
az network nsg create --resource-group MyResourceGroup --name MyNSG
# Add multiple rules
for i in $(seq 1 $n); do
az network nsg rule create --resource-group MyResourceGroup --nsg-name MyNSG --name Rule$i --priority $((100 + i)) --access Allow --direction Inbound --protocol Tcp --source-address-prefixes '*' --source-port-ranges '*' --destination-address-prefixes '*' --destination-port-ranges 80
done
# Associate NSG to multiple NICs
for j in $(seq 1 $m); do
az network nic update --resource-group MyResourceGroup --name Nic$j --network-security-group MyNSG
done
This sequence creates one NSG, adds n rules to it, and associates it with m network interfaces.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Creating NSG rules and associating NSG to NICs.
- How many times: Rule creation happens n times; NIC update happens m times.
As you add more rules (n) or associate with more NICs (m), the number of API calls grows linearly with both.
| Input Size (n rules, m NICs) | Approx. Api Calls/Operations |
|---|---|
| 10 rules, 10 NICs | ~20 calls (10 rule creates + 10 NIC updates) |
| 100 rules, 100 NICs | ~200 calls |
| 1000 rules, 1000 NICs | ~2000 calls |
Pattern observation: Doubling rules or NICs roughly doubles the total operations.
Time Complexity: O(n + m)
This means the time to complete all operations grows directly with the number of rules plus the number of NICs.
[X] Wrong: "Adding more rules does not affect the time because the NSG is a single resource."
[OK] Correct: Each rule requires a separate API call to create, so more rules mean more operations and more time.
Understanding how NSG operations scale helps you design efficient network security setups and shows you can reason about cloud resource management in real projects.
What if we grouped multiple rules into fewer NSGs and associated each NSG with fewer NICs? How would the time complexity change?
Practice
Solution
Step 1: Understand NSG function
NSGs are designed to filter network traffic to and from Azure resources.Step 2: Identify correct purpose
They do not store data, manage identities, or monitor performance.Final Answer:
To control inbound and outbound traffic to Azure resources -> Option CQuick Check:
NSG controls traffic [OK]
- Confusing NSG with storage or identity services
- Thinking NSG monitors performance
- Assuming NSG stores data
Solution
Step 1: Understand NSG rule priority format
NSG rule priority is a unique integer between 100 and 4096 that determines rule evaluation order.Step 2: Eliminate incorrect types
Priority is not a string, boolean, or IP range.Final Answer:
Priority must be a unique integer between 100 and 4096 -> Option DQuick Check:
Priority = integer 100-4096 [OK]
- Using strings or booleans for priority
- Confusing priority with IP ranges
- Not using unique priority numbers
Priority: 200, Direction: Inbound, Access: Deny, Source: Any, Destination: 10.0.0.4, Protocol: TCP, Port: 22What happens when a user tries to SSH (port 22) to 10.0.0.4?
Solution
Step 1: Analyze rule details
The rule denies inbound TCP traffic on port 22 to destination 10.0.0.4 from any source.Step 2: Determine effect on SSH connection
SSH uses TCP port 22, so the connection attempt matches the deny rule and is blocked.Final Answer:
The connection is denied -> Option AQuick Check:
Deny inbound TCP port 22 = connection denied [OK]
- Assuming 'Any' source means allow
- Confusing deny with logging
- Ignoring direction and port in rule
Solution
Step 1: Check rule priority and conflicts
NSG rules are evaluated by priority; a deny rule with higher priority can block traffic despite allow rules.Step 2: Confirm rule attachment and protocol
While attachment and protocol matter, the most common cause is a conflicting higher priority deny rule.Final Answer:
A higher priority deny rule blocks port 80 traffic -> Option BQuick Check:
Higher priority deny overrides allow [OK]
- Ignoring rule priority order
- Assuming NSG attachment is always correct
- Confusing protocol types
Solution
Step 1: Allow only specific IP inbound on port 3389
Priority: 300, Direction: Inbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389 allows inbound TCP traffic on port 3389 only from IP 203.0.113.5, which matches the requirement.Step 2: Check other options for correctness
Priority: 100, Direction: Inbound, Access: Deny, Source: Any, Destination: Any, Protocol: TCP, Port: 3389 denies all inbound TCP 3389 traffic, blocking access. Priority: 200, Direction: Inbound, Access: Allow, Source: Any, Destination: Any, Protocol: TCP, Port: 3389 allows all sources, which is insecure. Priority: 400, Direction: Outbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389 is outbound, irrelevant for inbound access.Final Answer:
Priority: 300, Direction: Inbound, Access: Allow, Source: 203.0.113.5, Destination: Any, Protocol: TCP, Port: 3389 -> Option AQuick Check:
Allow inbound TCP 3389 from specific IP [OK]
- Allowing all sources instead of specific IP
- Using outbound direction instead of inbound
- Denying all traffic without exception
Ready to prove it?
Pass the test and this pattern moves unknown
