Cost optimization strategies in GCP - Time & Space Complexity
We want to understand how the cost-saving actions in cloud setups grow as we add more resources or services.
How does the effort or number of steps to optimize costs change when the cloud environment grows?
Analyze the time complexity of the following cost optimization steps.
// Pseudocode for cost optimization in GCP
for each project in organization:
list all active resources
for each resource:
check usage metrics
if underused:
recommend rightsizing or shutdown
else if idle:
recommend shutdown
else:
continue
log recommendation
notify owner
// This repeats for all projects and resources
This sequence checks all resources in all projects to find cost-saving opportunities.
Identify the API calls, resource checks, and notifications that repeat.
- Primary operation: Listing and checking each resource's usage metrics.
- How many times: Once for every resource in every project.
- Secondary operations: Logging recommendations and notifying owners for each resource.
As the number of projects and resources grows, the number of checks and notifications grows too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 projects with 10 resources each | ~100 resource checks and notifications |
| 100 projects with 100 resources each | ~10,000 resource checks and notifications |
| 1000 projects with 1000 resources each | ~1,000,000 resource checks and notifications |
Pattern observation: The total operations grow quickly as both projects and resources increase, multiplying together.
Time Complexity: O(n * m)
This means the effort grows proportionally to the number of projects times the number of resources per project.
[X] Wrong: "Checking one resource means the total effort grows only a little as we add more resources."
[OK] Correct: Each resource requires its own check and notification, so the total effort adds up quickly as resources increase.
Understanding how cost optimization scales helps you design efficient cloud management tools and shows you think about real-world cloud challenges.
"What if we batch notifications instead of sending one per resource? How would the time complexity change?"