0
0
AWScloud~5 mins

Public vs private subnets in AWS - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Public vs private subnets
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations
  • Primary operation: Creating subnets and route tables, associating route tables, and creating routes.
  • How many times: Once per subnet (public or private).
How Execution Grows With Input

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
10About 40 (4 per subnet)
100About 400
1000About 4000

Pattern observation: The number of operations grows linearly as you add more subnets.

Final Time Complexity

Time Complexity: O(n)

This means the time to set up subnets and routing grows directly in proportion to how many subnets you create.

Common Mistake

[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.

Interview Connect

Understanding how subnet creation scales helps you design networks that grow smoothly and avoid surprises in setup time as your cloud grows.

Self-Check

"What if we used a single route table for all private subnets instead of one per subnet? How would the time complexity change?"