High availability configuration in GCP - Time & Space 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?
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.
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.
Each new instance adds one more creation operation.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 instance creations + 1 load balancer setup |
| 100 | About 100 instance creations + 1 load balancer setup |
| 1000 | About 1000 instance creations + 1 load balancer setup |
Pattern observation: The number of instance creation operations grows directly with the number of instances.
Time Complexity: O(n)
This means the work grows in a straight line as you add more instances.
[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.
Understanding how resource creation scales helps you design systems that stay reliable as they grow.
"What if we used a serverless platform instead of VMs? How would the time complexity change?"
Practice
Solution
Step 1: Understand high availability concept
High availability means the system stays up and working even if some components fail.Step 2: Identify the main goal
The goal is to avoid downtime and keep the service accessible to users.Final Answer:
To keep the service running even if some parts fail -> Option DQuick Check:
High availability = service stays running despite failures [OK]
- Confusing high availability with cost reduction
- Thinking it only improves speed for one user
- Assuming it limits user access
Solution
Step 1: Identify resource for auto healing
Managed Instance Groups (MIGs) can use health checks to detect unhealthy VMs.Step 2: Understand auto replacement
MIGs automatically recreate unhealthy instances to keep the service healthy.Final Answer:
Managed Instance Group with health checks -> Option AQuick Check:
Auto replace unhealthy VMs = Managed Instance Group + health checks [OK]
- Choosing storage or database services instead of compute
- Confusing Cloud Functions with VM management
- Ignoring health checks role
Solution
Step 1: Understand multi-zone MIG behavior
MIGs spread instances across zones for fault tolerance.Step 2: Analyze failure impact
If one zone fails, MIG recreates instances in healthy zones to maintain capacity.Final Answer:
Instances in the failed zone are replaced in the other zone -> Option CQuick Check:
Zone failure triggers instance replacement in healthy zones [OK]
- Thinking instances are replaced in the failed zone
- Assuming manual restart is needed
- Believing all instances shut down
Solution
Step 1: Check health check attachment
Health checks must be attached to trigger autohealing, but this alone is not enough.Step 2: Verify autohealing setting
Autohealing must be enabled in the instance group to replace unhealthy instances.Final Answer:
Autohealing is disabled in the instance group -> Option AQuick Check:
Autohealing disabled = no instance replacement [OK]
- Assuming health check attachment alone triggers replacement
- Blaming multi-zone setup
- Ignoring autohealing configuration
Solution
Step 1: Understand zone failure impact
A single zone failure can take down all instances in that zone if only single-zone is used.Step 2: Choose multi-zone MIG with health checks and autoscaling
This setup spreads instances across zones, replaces unhealthy ones, and adjusts capacity automatically.Step 3: Evaluate other options
Single-zone lacks zone failure protection; single VM is a single point of failure; storage bucket is not compute.Final Answer:
Multi-zone Managed Instance Group with health checks and autoscaling -> Option BQuick Check:
Multi-zone MIG + health checks + autoscaling = high availability [OK]
- Choosing single-zone MIG ignoring zone failure
- Using single VM which is a single failure point
- Confusing storage bucket with compute resources
Ready to prove it?
Pass the test and this pattern moves unknown
