Cost optimization for cloud resources in Apache Airflow - Time & Space Complexity
When managing cloud resources with Airflow, it's important to understand how the cost of running tasks grows as the number of resources or tasks increases.
We want to know how the time and cost scale when automating cloud resource management.
Analyze the time complexity of the following Airflow DAG task that checks and optimizes cloud resources.
from airflow import DAG
from airflow.operators.python import PythonOperator
from datetime import datetime
def optimize_resources(resource_list):
for resource in resource_list:
if resource.is_idle():
resource.shutdown()
dag = DAG('cloud_cost_optimization', start_date=datetime(2024, 1, 1), schedule_interval='@daily')
t1 = PythonOperator(
task_id='optimize',
python_callable=optimize_resources,
op_args=[['vm1', 'vm2', 'vm3', 'vm4']],
dag=dag
)
This code loops through a list of cloud resources and shuts down those that are idle to save cost.
Look at what repeats in the code.
- Primary operation: Looping through each resource in the list.
- How many times: Once for each resource in the input list.
As the number of resources grows, the time to check and optimize grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and possible shutdowns |
| 100 | 100 checks and possible shutdowns |
| 1000 | 1000 checks and possible shutdowns |
Pattern observation: The work grows directly with the number of resources.
Time Complexity: O(n)
This means the time to optimize grows in a straight line as you add more resources.
[X] Wrong: "Checking all resources happens instantly no matter how many there are."
[OK] Correct: Each resource needs to be checked one by one, so more resources mean more time.
Understanding how task time grows with resource count helps you design efficient cloud workflows and shows you can think about scaling in real projects.
"What if the optimize_resources function used parallel tasks for each resource? How would the time complexity change?"
Practice
Solution
Step 1: Understand cost optimization meaning
Cost optimization means using resources efficiently to reduce expenses.Step 2: Relate to Airflow usage
Airflow helps automate resource usage only when needed, saving money.Final Answer:
To use cloud resources only when needed to save money -> Option CQuick Check:
Cost optimization = Use resources only when needed [OK]
- Thinking more resources always mean better performance
- Ignoring the need to stop unused resources
- Believing monitoring is unnecessary
Solution
Step 1: Identify Airflow automation features
DAG scheduling allows tasks to run automatically at set times.Step 2: Connect scheduling to resource control
Scheduling can start or stop cloud resources based on time, saving costs.Final Answer:
DAG scheduling -> Option AQuick Check:
DAG scheduling = Automate resource start/stop [OK]
- Thinking manual triggers automate resource control
- Confusing static allocation with automation
- Ignoring the role of scheduling in cost saving
with DAG('resource_control', schedule_interval='0 8 * * *') as dag:
start_instance = BashOperator(
task_id='start_instance',
bash_command='cloud-cli start instance'
)
stop_instance = BashOperator(
task_id='stop_instance',
bash_command='cloud-cli stop instance',
trigger_rule='all_done'
)
start_instance >> stop_instanceSolution
Step 1: Analyze DAG schedule and tasks
DAG runs daily at 8 AM, starting the instance first.Step 2: Understand task order and trigger rules
stop_instance runs after start_instance completes, stopping the instance.Final Answer:
Instance starts at 8 AM and stops immediately after -> Option BQuick Check:
Task order start >> stop means start then stop [OK]
- Assuming stop runs before start
- Ignoring trigger_rule effects
- Thinking instance stays running all day
stop_instance = BashOperator(
task_id='stop_instance',
bash_command='cloud-cli stop instance',
trigger_rule='all_done'
)Why does this code cause an error?
Solution
Step 1: Check syntax of BashOperator parameters
Parameters must be separated by commas; here comma is missing after bash_command.Step 2: Identify cause of syntax error
Missing comma causes Python syntax error, preventing DAG parsing.Final Answer:
Missing comma between bash_command and trigger_rule -> Option DQuick Check:
Comma missing = Syntax error [OK]
- Thinking trigger_rule is invalid parameter
- Assuming BashOperator can't run shell commands
- Believing task_id naming causes error
Solution
Step 1: Choose schedule for weekdays only
'0 9 * * 1-5' runs at 9 AM Monday to Friday, matching requirement.Step 2: Separate start and stop times
Stopping at 6 PM requires a different schedule '0 18 * * 1-5', best in separate DAG for clarity.Final Answer:
schedule_interval='0 9 * * 1-5' to start, and a separate DAG with '0 18 * * 1-5' to stop -> Option AQuick Check:
Separate schedules for start and stop on weekdays [OK]
- Using '@daily' runs every day, not weekdays only
- Starting and stopping immediately in one DAG
- Relying on manual runs instead of automation
Ready to prove it?
Pass the test and this pattern moves unknown
