Public vs private subnets in AWS - Performance Comparison
When working with public and private subnets, it's important to understand how the number of resources affects the time it takes to set up and manage network traffic.
We want to know how the time to configure and route traffic grows as we add more subnets.
Analyze the time complexity of creating and routing traffic through public and private subnets.
// Create a VPC
aws ec2 create-vpc --cidr-block 10.0.0.0/16
// Create public subnets
for each public subnet:
aws ec2 create-subnet --vpc-id vpc-123 --cidr-block 10.0.x.0/24
aws ec2 create-route-table --vpc-id vpc-123
aws ec2 associate-route-table --subnet-id subnet-public-x --route-table-id rtb-public-x
aws ec2 create-route --route-table-id rtb-public-x --destination-cidr-block 0.0.0.0/0 --gateway-id igw-123
// Create private subnets
for each private subnet:
aws ec2 create-subnet --vpc-id vpc-123 --cidr-block 10.0.y.0/24
aws ec2 create-route-table --vpc-id vpc-123
aws ec2 associate-route-table --subnet-id subnet-private-y --route-table-id rtb-private-y
aws ec2 create-route --route-table-id rtb-private-y --destination-cidr-block 0.0.0.0/0 --nat-gateway-id nat-123
This sequence creates multiple public and private subnets, each with its own route table and routes to internet or NAT gateways.
- Primary operation: Creating subnets and route tables, associating route tables, and creating routes.
- How many times: Once per subnet (public or private).
Each new subnet requires creating a subnet, a route table, associating it, and adding routes. So, the work grows directly with the number of subnets.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 40 (4 per subnet) |
| 100 | About 400 |
| 1000 | About 4000 |
Pattern observation: The number of operations grows linearly as you add more subnets.
Time Complexity: O(n)
This means the time to set up subnets and routing grows directly in proportion to how many subnets you create.
[X] Wrong: "Adding more subnets won't increase setup time much because they share the same VPC and gateway."
[OK] Correct: Each subnet needs its own route table and associations, so the setup time increases with each subnet added.
Understanding how subnet creation scales helps you design networks that grow smoothly and avoid surprises in setup time as your cloud grows.
"What if we used a single route table for all private subnets instead of one per subnet? How would the time complexity change?"