Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Cost optimization at scale
📖 Scenario: You work as an MLOps engineer managing cloud resources for machine learning projects. Your goal is to optimize costs by tracking resource usage and identifying expensive resources to reduce waste.
🎯 Goal: Build a simple Python program that stores resource costs, sets a cost threshold, filters resources exceeding the threshold, and prints them out. This helps identify which resources to optimize or shut down.
📋 What You'll Learn
Create a dictionary with resource names and their monthly costs
Add a variable for the cost threshold
Filter resources with costs above the threshold using a dictionary comprehension
Print the filtered expensive resources
💡 Why This Matters
🌍 Real World
Cloud and MLOps engineers often need to monitor and optimize resource costs to save money and improve efficiency.
💼 Career
This project teaches basic cost tracking and filtering skills useful for managing cloud budgets and optimizing machine learning infrastructure.
Progress0 / 4 steps
1
Create resource cost dictionary
Create a dictionary called resource_costs with these exact entries: 'GPU_Cluster': 1200, 'Data_Storage': 300, 'API_Server': 450, 'Model_Training': 900, 'Monitoring': 150
MLOps
Hint
Use curly braces to create a dictionary with keys as resource names and values as costs.
2
Set cost threshold
Create a variable called cost_threshold and set it to 500
MLOps
Hint
Just assign the number 500 to the variable named cost_threshold.
3
Filter expensive resources
Create a dictionary called expensive_resources using a dictionary comprehension that includes only entries from resource_costs where the cost is greater than cost_threshold
MLOps
Hint
Use a dictionary comprehension with for name, cost in resource_costs.items() and an if condition.
4
Print expensive resources
Write a print statement to display the expensive_resources dictionary
MLOps
Hint
Use print(expensive_resources) to show the filtered dictionary.
Practice
(1/5)
1. What is the main goal of cost optimization at scale in MLOps?
easy
A. To increase the number of servers regardless of workload
B. To avoid monitoring costs after deployment
C. To use only the most expensive cloud resources
D. To save money by matching resource use to workload needs
Solution
Step 1: Understand cost optimization purpose
Cost optimization means using resources efficiently to reduce expenses.
Step 2: Match resources to workload needs
Adjusting resources based on workload avoids waste and saves money.
Final Answer:
To save money by matching resource use to workload needs -> Option D
Quick Check:
Cost optimization = save money by matching resources [OK]
Hint: Cost optimization means using just enough resources [OK]
Common Mistakes:
Thinking more servers always means better
Ignoring cost monitoring after deployment
Assuming expensive resources are always best
2. Which of the following is a correct way to specify a spot instance in a Kubernetes pod spec for cost savings?
easy
A. affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "kubernetes.io/lifecycle"
operator: In
values:
- spot
B. tolerations:
- key: "spot-instance"
operator: Exists
effect: NoSchedule
C. nodeSelector:
kubernetes.io/instance-type: spot
D. resources:
requests:
cpu: "spot"
memory: "spot"
Solution
Step 1: Understand spot instance labeling in Kubernetes
Spot instances are often labeled with lifecycle=spot to identify cheaper nodes.
Step 2: Check node affinity syntax
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: "kubernetes.io/lifecycle"
operator: In
values:
- spot correctly uses nodeAffinity with matchExpressions to select nodes labeled as spot.
Final Answer:
affinity with nodeSelectorTerms matching lifecycle=spot label -> Option A
Quick Check:
Spot instance selection uses nodeAffinity with lifecycle=spot label [OK]
Hint: Use nodeAffinity with lifecycle=spot label for spot nodes [OK]
Common Mistakes:
Using nodeSelector with wrong label key
Setting resource requests to 'spot' (invalid)
Confusing tolerations with node affinity
3. Given this autoscaling configuration snippet for a Kubernetes deployment: