0
0
GCPcloud~5 mins

High availability configuration in GCP - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: High availability configuration
O(n)
Understanding Time Complexity

When setting up high availability, we want to know how the work grows as we add more resources.

We ask: How does adding more instances affect the number of operations needed?

Scenario Under Consideration

Analyze the time complexity of creating multiple VM instances behind a load balancer.

// Create a managed instance group with N instances
resource "google_compute_instance_template" "template" {
  name_prefix = "instance-template"
  machine_type = "e2-medium"
  disk {
    boot = true
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
}

resource "google_compute_instance_group_manager" "igm" {
  name = "instance-group"
  base_instance_name = "instance"
  instance_template = google_compute_instance_template.template.self_link
  target_size = var.instance_count
}

resource "google_compute_backend_service" "backend" {
  name = "backend-service"
  backend {
    group = google_compute_instance_group_manager.igm.instance_group
  }
  load_balancing_scheme = "EXTERNAL"
  protocol = "HTTP"
}

This sequence creates a template, then a group of N instances, and attaches them to a load balancer.

Identify Repeating Operations

Look at what repeats as we increase the number of instances.

  • Primary operation: Creating each VM instance in the managed instance group.
  • How many times: Once per instance, so N times.
  • Load balancer setup happens once, not per instance.
How Execution Grows With Input

Each new instance adds one more creation operation.

Input Size (n)Approx. Api Calls/Operations
10About 10 instance creations + 1 load balancer setup
100About 100 instance creations + 1 load balancer setup
1000About 1000 instance creations + 1 load balancer setup

Pattern observation: The number of instance creation operations grows directly with the number of instances.

Final Time Complexity

Time Complexity: O(n)

This means the work grows in a straight line as you add more instances.

Common Mistake

[X] Wrong: "Adding more instances only requires a fixed amount of work because the load balancer handles them all at once."

[OK] Correct: Each instance still needs to be created and configured separately, so work grows with the number of instances.

Interview Connect

Understanding how resource creation scales helps you design systems that stay reliable as they grow.

Self-Check

"What if we used a serverless platform instead of VMs? How would the time complexity change?"