Workflows for orchestration in GCP - Time & Space Complexity
When using Workflows to organize cloud tasks, it's important to know how the time to complete grows as you add more steps.
We want to understand how the number of steps affects the total time and calls made.
Analyze the time complexity of the following workflow definition.
main:
steps:
- step1:
call: http.get
args:
url: https://example.com/api/resource
- step2:
call: http.get
args:
url: https://example.com/api/resource2
- step3:
call: http.get
args:
url: https://example.com/api/resource3
This workflow calls three HTTP endpoints in sequence, each step waiting for the previous to finish.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: HTTP call to an external API endpoint.
- How many times: Once per step, so as many times as there are steps.
Each added step adds one more HTTP call, so the total calls grow directly with the number of steps.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 HTTP calls |
| 100 | 100 HTTP calls |
| 1000 | 1000 HTTP calls |
Pattern observation: The number of calls grows linearly as you add more steps.
Time Complexity: O(n)
This means the total time and calls increase directly in proportion to the number of workflow steps.
[X] Wrong: "Adding more steps won't affect total time much because calls run instantly."
[OK] Correct: Each step waits for the previous to finish, so total time adds up with each call.
Understanding how workflow steps add up helps you design efficient cloud processes and answer questions about scaling tasks.
"What if the workflow steps ran in parallel instead of sequence? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of Workflows
Workflows are designed to automate tasks by connecting different cloud services step-by-step.Step 2: Compare options with this role
Options B, C, and D describe storage, computing, and monitoring, which are not the main purpose of Workflows.Final Answer:
To automate and coordinate multiple cloud services in a sequence -> Option BQuick Check:
Workflows automate cloud tasks = A [OK]
- Confusing Workflows with storage or compute services
- Thinking Workflows monitor network traffic
- Assuming Workflows create virtual machines
Solution
Step 1: Recall the YAML structure for Workflows
The main entry point for a workflow is defined under themain:key.Step 2: Check other options
Options likesteps:,start:, andexecute:are not valid top-level keys for starting a workflow.Final Answer:
main: -> Option CQuick Check:
Workflow starts with main: in YAML [OK]
- Using steps: instead of main:
- Confusing start: as the entry point
- Trying execute: which is invalid
main:
steps:
- call: http.get
args:
url: "https://example.com/api"
- return: ${steps[0].body.status}What will the workflow return if the HTTP GET response body is
{"status": "success"}?Solution
Step 1: Understand the call and return steps
The first step calls an HTTP GET and stores the response insteps[0]. The second step returns thestatusfield from the response body.Step 2: Extract the status value from the response
The response body is{"status": "success"}, sosteps[0].body.statusis "success".Final Answer:
"success" -> Option AQuick Check:
Return status field from HTTP response = "success" [OK]
- Returning the whole body instead of the status field
- Syntax errors in accessing steps array
- Assuming null if not parsed correctly
main:
steps:
- call: http.get
args:
url: "https://example.com/api"
- return: steps[0].body.statusSolution
Step 1: Check expression syntax in return
In Workflows YAML, expressions must be wrapped in${}to be evaluated.Step 2: Verify other parts
Indentation is correct,http.getis valid, and URL is quoted properly.Final Answer:
Missing ${} around the expression in return -> Option DQuick Check:
Expressions need ${} to evaluate in return [OK]
- Forgetting ${} around expressions
- Misaligning YAML indentation
- Assuming http.get is invalid
- Not quoting URLs
Solution
Step 1: Understand conditional execution in workflows
Workflows supportswitchorifsteps to decide which steps run based on conditions.Step 2: Match the requirement
To call the second API only if the first returns "ok", useiforswitchto check the first response before proceeding.Final Answer:
Usingswitchorifsteps to check the first API's response -> Option AQuick Check:
Conditional steps use if/switch to control flow [OK]
- Using parallel steps which run both APIs simultaneously
- Using retry which repeats calls but doesn't conditionally run next
- Using return which ends workflow early
Ready to prove it?
Pass the test and this pattern moves unknown
