Public vs private subnets in AWS - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
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?"
Practice
Solution
Step 1: Understand public subnet characteristics
A public subnet allows resources to communicate directly with the internet by assigning public IP addresses.Step 2: Compare with other subnet types
Private subnets do not assign public IPs and restrict internet access, unlike public subnets.Final Answer:
It has direct internet access and assigns public IPs automatically. -> Option AQuick Check:
Public subnet = direct internet access [OK]
- Confusing private subnet with public subnet
- Thinking all subnets have internet access
- Assuming VPN is needed for public subnet
Solution
Step 1: Identify private subnet IP assignment
Private subnets do not assign public IPs automatically, soMapPublicIpOnLaunchmust be false.Step 2: Understand routing and gateway settings
Internet Gateway (IGW) and routing to 0.0.0.0/0 are for public subnets, not private.Final Answer:
<code>MapPublicIpOnLaunch: false</code> -> Option CQuick Check:
Private subnet = no public IP assignment [OK]
- Setting public IP assignment to true for private subnet
- Confusing internet gateway with subnet config
- Using public route table for private subnet
Destination: 0.0.0.0/0, Target: igw-12345What type of subnet is this likely to be?
Solution
Step 1: Analyze route table destination and target
The route directs all internet traffic (0.0.0.0/0) to an internet gateway (igw), which enables internet access.Step 2: Determine subnet type from routing
Routing to an internet gateway means the subnet is public and can access the internet directly.Final Answer:
Public subnet with internet access -> Option AQuick Check:
Route to IGW = public subnet [OK]
- Assuming NAT gateway means public subnet
- Confusing IGW with VPN or NAT
- Ignoring route table targets
MapPublicIpOnLaunch: false but instances still get public IPs. What is the likely issue?Solution
Step 1: Understand subnet vs instance IP assignment
Subnet setting disables automatic public IP assignment, but instance launch settings can override this.Step 2: Identify cause of public IP despite subnet config
If instance launch config explicitly assigns public IPs, it overrides subnet default behavior.Final Answer:
The instance launch configuration overrides subnet settings to assign public IPs. -> Option BQuick Check:
Instance config can override subnet IP assignment [OK]
- Blaming route table for IP assignment
- Ignoring instance-level settings
- Confusing CIDR overlap with IP assignment
Solution
Step 1: Identify subnet roles for internet access
The web server needs internet access, so it belongs in a public subnet with a route to the internet gateway.Step 2: Secure the database internally
The database should be in a private subnet without direct internet access to keep it secure.Final Answer:
Place the web server in a public subnet and the database in a private subnet. -> Option DQuick Check:
Internet-facing resources in public, internal in private [OK]
- Putting database in public subnet
- Putting web server in private subnet
- Relying only on security groups without subnet design
