Performance optimization in GCP - Time & Space Complexity
When we optimize performance in cloud systems, we want to know how the work grows as we add more tasks or users.
We ask: How does the time to complete tasks change when the workload increases?
Analyze the time complexity of scaling a cloud function to handle multiple requests.
// Pseudocode for handling requests in GCP Cloud Functions
function handleRequests(requests) {
for (let i = 0; i < requests.length; i++) {
processRequest(requests[i]);
}
}
function processRequest(request) {
// Calls external API
callExternalAPI(request.data);
}
This sequence processes each request by calling an external API once per request.
Look at what repeats as the number of requests grows.
- Primary operation: Calling the external API for each request.
- How many times: Once per request, so as many times as there are requests.
Each new request adds one more API call, so the total work grows steadily with the number of requests.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The work grows directly in proportion to the number of requests.
Time Complexity: O(n)
This means the time to complete all requests grows in a straight line as the number of requests increases.
[X] Wrong: "Adding more requests won't affect total time much because calls happen fast."
[OK] Correct: Each request adds a separate call, so total time adds up and grows with the number of requests.
Understanding how work grows with input helps you explain system behavior clearly and shows you can think about scaling in real projects.
"What if we batch multiple requests into one API call? How would the time complexity change?"
Practice
Solution
Step 1: Understand machine types impact
More CPUs and memory allow the instance to handle more work faster.Step 2: Evaluate other options
Smaller disks reduce storage but do not improve performance; disabling autoscaling limits flexibility; standard disks are slower than SSDs.Final Answer:
Choose a machine type with more CPUs and memory -> Option AQuick Check:
More CPU and memory = better performance [OK]
- Thinking smaller disks improve speed
- Disabling autoscaling helps performance
- Ignoring disk type impact
Solution
Step 1: Understand autoscaling in GKE
Autoscaling adjusts the number of pods based on CPU or other metrics automatically.Step 2: Identify correct command
Thekubectl autoscalecommand sets autoscaling with CPU target, enabling dynamic scaling.Final Answer:
Use thekubectl autoscalecommand with CPU utilization target -> Option AQuick Check:
Autoscale command enables dynamic scaling [OK]
- Manually setting fixed node counts
- Disabling autoscaling to save costs
- Using preemptible VMs without autoscaling
Solution
Step 1: Understand disk types and performance
Standard persistent disks are slower; SSD persistent disks offer much better IOPS and throughput.Step 2: Evaluate options
Larger standard disks increase capacity but not speed; balanced disks are better than standard but slower than SSD; adding CPUs does not improve disk speed.Final Answer:
Switch to an SSD persistent disk -> Option CQuick Check:
SSD disks improve disk performance most [OK]
- Confusing disk size with speed
- Thinking more CPUs improve disk speed
- Choosing balanced disks over SSD for max speed
Solution
Step 1: Understand memory impact on Cloud Functions
More memory increases CPU allocation, improving performance.Step 2: Evaluate other options
Reducing memory slows function; disabling logging has minimal effect; older runtimes may be slower or unsupported.Final Answer:
Increase the memory allocation to 1 GB -> Option BQuick Check:
More memory means more CPU and faster execution [OK]
- Reducing memory to save cost
- Disabling logging expecting big speedup
- Using older runtimes for speed
Solution
Step 1: Understand autoscaling benefits
Autoscaling adjusts instances to traffic, handling spikes efficiently and saving cost during low traffic.Step 2: Use Cloud CDN to reduce backend load
Cloud CDN caches content closer to users, reducing latency and backend requests.Final Answer:
Enable autoscaling with a minimum number of instances and use Cloud CDN -> Option DQuick Check:
Autoscaling + CDN = best performance and cost [OK]
- Fixing instance count causes wasted resources
- Manual scaling ignores traffic spikes
- Disabling CDN increases backend load
Ready to prove it?
Pass the test and this pattern moves unknown
