Event triggered functions in GCP - Time & Space Complexity
When using event triggered functions, it is important to understand how the number of events affects the work done by the system.
We want to know how the system's work grows as more events happen.
Analyze the time complexity of the following operation sequence.
// Cloud Function triggered by new file upload to Cloud Storage
exports.processFile = (event, context) => {
const fileName = event.name;
console.log(`Processing file: ${fileName}`);
// Simulate processing
// ...
};
This function runs once for each new file uploaded to a storage bucket, processing that file.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The function execution triggered by each file upload event.
- How many times: Once per file uploaded to the bucket.
Each new file upload causes one function execution, so the total work grows directly with the number of files.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 function executions |
| 100 | 100 function executions |
| 1000 | 1000 function executions |
Pattern observation: The work grows in a straight line as more files are uploaded.
Time Complexity: O(n)
This means the total work increases directly with the number of events triggering the function.
[X] Wrong: "The function runs once no matter how many files are uploaded."
[OK] Correct: Each file upload triggers a separate function execution, so the work grows with the number of files.
Understanding how event-driven functions scale with input helps you design systems that handle growing workloads smoothly.
"What if the function batches multiple file events together before processing? How would the time complexity change?"
Practice
Solution
Step 1: Understand event triggered functions
Event triggered functions run automatically when a specific event happens, like a file upload or database change.Step 2: Compare options with the definition
Only To automatically run code when a specific event occurs describes automatic running on events. Options B and C describe manual or scheduled runs, and D is unrelated.Final Answer:
To automatically run code when a specific event occurs -> Option DQuick Check:
Event triggered function = automatic run on event [OK]
- Confusing event triggered with scheduled functions
- Thinking manual start is event triggered
- Mixing storage with function triggers
Solution
Step 1: Identify Cloud Storage event trigger syntax
Cloud Storage events use --trigger-resource for the bucket and --trigger-event for the event type like object finalize.Step 2: Match options with correct syntax
gcloud functions deploy myFunc --trigger-resource my-bucket --trigger-event google.storage.object.finalize correctly uses --trigger-resource and --trigger-event for Cloud Storage. Others are for HTTP, Pub/Sub, or scheduled triggers.Final Answer:
gcloud functions deploy myFunc --trigger-resource my-bucket --trigger-event google.storage.object.finalize -> Option AQuick Check:
Cloud Storage trigger = resource + event flags [OK]
- Using --trigger-http for storage events
- Confusing Pub/Sub topic triggers with storage
- Using schedule flags for event triggers
exports.processUpdate = (event, context) => {
const message = Buffer.from(event.data, 'base64').toString();
console.log(`Received message: ${message}`);
};Solution
Step 1: Understand event data decoding
The function decodes the base64 event.data to a string and logs it.Step 2: Check function behavior on message publish
When a message is published, the function runs and logs the decoded message content.Final Answer:
The function logs the decoded message content -> Option CQuick Check:
Pub/Sub event data decoded and logged [OK]
- Forgetting to decode base64 data
- Assuming function errors without checking code
- Thinking function ignores messages
Solution
Step 1: Check trigger configuration
If the trigger path is incorrect, the function won't run on document creation events.Step 2: Eliminate other causes
Syntax errors prevent deployment (not silent failure). HTTP trigger won't respond to Firestore events. Firestore being empty doesn't stop triggers on new documents.Final Answer:
The function is missing the correct Firestore trigger path -> Option AQuick Check:
Wrong trigger path = no function run [OK]
- Assuming empty Firestore blocks triggers
- Confusing HTTP triggers with event triggers
- Ignoring trigger path correctness
Solution
Step 1: Identify correct event for new file upload
The event google.storage.object.finalize triggers when a new object is created or overwritten.Step 2: Confirm resource and event usage
The resource is the bucket name 'photos'. Wildcards like 'photos/*' are not valid for --trigger-resource.Final Answer:
Use --trigger-resource photos and --trigger-event google.storage.object.finalize -> Option BQuick Check:
New file upload = finalize event on bucket resource [OK]
- Using delete or archive events instead of finalize
- Trying to use wildcards in trigger-resource
- Using metadataUpdate event which triggers on metadata changes
Ready to prove it?
Pass the test and this pattern moves unknown
