HTTP triggered functions in GCP - Time & Space Complexity
When using HTTP triggered functions, it's important to understand how the number of requests affects the work done.
We want to know how the function's work grows as more HTTP requests come in.
Analyze the time complexity of the following operation sequence.
exports.helloHttp = (req, res) => {
const name = req.query.name || 'World';
res.status(200).send(`Hello, ${name}!`);
};
This function responds to each HTTP request by sending a greeting message.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Handling each HTTP request and sending a response.
- How many times: Once per incoming HTTP request.
Each new HTTP request causes the function to run once, doing a small fixed amount of work.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 function executions |
| 100 | 100 function executions |
| 1000 | 1000 function executions |
Pattern observation: The work grows directly with the number of requests.
Time Complexity: O(n)
This means the total work grows linearly with the number of HTTP requests received.
[X] Wrong: "The function runs once and handles all requests at the same time."
[OK] Correct: Each HTTP request triggers a separate function execution, so work grows with requests.
Understanding how serverless functions scale with requests helps you design efficient cloud services and answer real-world questions confidently.
"What if the function made an external API call for each request? How would the time complexity change?"