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?"
Practice
Solution
Step 1: Understand the trigger type
HTTP triggered functions run automatically when they receive a web request.Step 2: Identify the function behavior
They process the request and return a response to the caller.Final Answer:
The function runs the code and sends back a response. -> Option CQuick Check:
HTTP trigger = runs code + responds [OK]
- Thinking the function only logs requests
- Assuming manual trigger is needed
- Believing the function stores requests without running
Solution
Step 1: Recall function signature for HTTP triggers
HTTP triggered functions accept a single 'request' parameter representing the web request.Step 2: Match the correct signature
Only 'def my_function(request):' matches this pattern.Final Answer:
def my_function(request): -> Option DQuick Check:
HTTP function signature = def func(request) [OK]
- Using event and context parameters (for background triggers)
- Defining function without parameters
- Using wrong parameter names
def hello_world(request):
if request.method == 'GET':
return 'Hello, World!'
else:
return 'Unsupported method', 405
Solution
Step 1: Check the request method
The function checks if the request method is 'GET'.Step 2: Determine response for GET
For 'GET', it returns the string 'Hello, World!'.Final Answer:
"Hello, World!" -> Option AQuick Check:
GET request returns greeting [OK]
- Confusing method names case sensitivity
- Expecting a JSON response without formatting
- Ignoring else branch
def process_request(request):
if request.method == 'POST'
return 'Received POST'
else:
return 'Only POST allowed', 405
Solution
Step 1: Check syntax of if statement
The if statement is missing a colon ':' at the end.Step 2: Confirm syntax error impact
Without the colon, the code will cause a syntax error and fail to deploy.Final Answer:
Missing colon ':' after if statement -> Option AQuick Check:
if statement needs ':' [OK]
- Ignoring syntax errors
- Assuming return types are wrong
- Confusing parameter names
Solution
Step 1: Use correct method to parse JSON
request.get_json() is the correct method to parse JSON body in GCP HTTP functions.Step 2: Access JSON data correctly
JSON data is a dictionary, so access fields with data['name'], not data.name.Final Answer:
def get_name(request): data = request.get_json() return f"Hello, {data['name']}!" -> Option BQuick Check:
Use get_json() and dict access [OK]
- Using request.json() instead of get_json()
- Accessing JSON fields with dot notation
- Forgetting parentheses on get_json
Ready to prove it?
Pass the test and this pattern moves unknown
