Resource monitors for cost control in Snowflake - Time & Space Complexity
We want to understand how the time to check and enforce resource limits grows as usage increases.
Specifically, how does Snowflake handle monitoring usage to control costs efficiently?
Analyze the time complexity of the following resource monitor checks.
CREATE RESOURCE MONITOR my_monitor
WITH CREDIT_QUOTA = 100
TRIGGERS ON 80 PERCENT DO NOTIFY
TRIGGERS ON 100 PERCENT DO SUSPEND;
-- Periodically check usage
SELECT SUM(CREDITS_USED)
FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY
WHERE START_TIME >= DATEADD(DAY, -1, CURRENT_TIMESTAMP());
-- If threshold reached, trigger action
ALTER WAREHOUSE "my_warehouse" SUSPEND;
This sequence creates a monitor, checks usage, and triggers actions when limits are reached.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Periodic usage check queries to monitor credit consumption.
- How many times: These checks happen regularly, depending on configuration (e.g., every few minutes).
As the number of warehouses or queries running increases, the monitor must check more usage data.
| Input Size (number of warehouses/queries) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 usage checks per interval |
| 100 | 100 usage checks per interval |
| 1000 | 1000 usage checks per interval |
Pattern observation: The number of checks grows linearly with the number of monitored resources.
Time Complexity: O(n)
This means the time to monitor usage grows directly with the number of resources being tracked.
[X] Wrong: "Resource monitors check all usage instantly regardless of scale."
[OK] Correct: Each resource's usage must be checked separately, so more resources mean more checks and longer total time.
Understanding how monitoring scales helps you design cost controls that stay efficient as systems grow.
"What if the resource monitor aggregated usage data instead of checking each resource separately? How would the time complexity change?"
Practice
Solution
Step 1: Understand resource monitor function
Resource monitors are designed to track cloud credit consumption and enforce limits to control costs.Step 2: Identify the correct purpose
Among the options, only tracking and controlling credit usage matches the resource monitor's role.Final Answer:
To track and control cloud credit usage by setting limits -> Option AQuick Check:
Resource monitor = cost control [OK]
- Confusing resource monitors with user access controls
- Thinking resource monitors create warehouses
- Assuming resource monitors handle data backups
Solution
Step 1: Review correct syntax for resource monitor creation
The correct syntax uses CREATE RESOURCE MONITOR with WITH CREDIT_QUOTA and TRIGGERS clauses.Step 2: Match syntax with options
CREATE RESOURCE MONITOR my_monitor WITH CREDIT_QUOTA = 1000 TRIGGERS ON 90 PERCENT DO NOTIFY matches the correct syntax including triggers and notification on 90% usage.Final Answer:
CREATE RESOURCE MONITOR my_monitor WITH CREDIT_QUOTA = 1000 TRIGGERS ON 90 PERCENT DO NOTIFY -> Option CQuick Check:
Correct syntax uses WITH CREDIT_QUOTA and TRIGGERS [OK]
- Using SET instead of WITH for quota
- Omitting TRIGGERS clause
- Incorrect keyword CREDIT_LIMIT instead of CREDIT_QUOTA
CREATE RESOURCE MONITOR rm1 WITH CREDIT_QUOTA = 500 TRIGGERS ON 80 PERCENT DO NOTIFY ON 100 PERCENT DO SUSPEND;
What happens when the credit usage reaches 100%?
Solution
Step 1: Analyze the triggers in the resource monitor
The monitor triggers a notification at 80% and suspends resources at 100% usage.Step 2: Understand the effect of the 100% trigger
At 100%, the monitor suspends warehouses linked to it to prevent further credit use.Final Answer:
The resource monitor suspends the warehouses linked to it -> Option AQuick Check:
100% trigger with SUSPEND = suspend warehouses [OK]
- Thinking notification happens at 100% instead of suspend
- Assuming quota increases automatically
- Believing nothing happens at limit
CREATE RESOURCE MONITOR monitor1 WITH CREDIT_QUOTA = 2000 TRIGGERS ON 90 PERCENT DO NOTIFY ON 110 PERCENT DO SUSPEND;
What is wrong with this command?
Solution
Step 1: Check trigger percentage limits
Trigger percentages must be between 0 and 100; 110% is invalid.Step 2: Validate other parts of the command
DO NOTIFY is valid, CREDIT_QUOTA can be any positive number, and suspension is allowed.Final Answer:
The trigger percentage cannot exceed 100% -> Option BQuick Check:
Trigger percent ≤ 100% [OK]
- Setting trigger above 100%
- Confusing DO NOTIFY syntax
- Thinking suspension is disallowed
Solution
Step 1: Identify notification and suspension percentages
Notification should occur at 80%, suspension at 95% usage.Step 2: Match triggers to correct actions and order
CREATE RESOURCE MONITOR cost_control WITH CREDIT_QUOTA = 1500 TRIGGERS ON 80 PERCENT DO NOTIFY ON 95 PERCENT DO SUSPEND; correctly sets 80% for notification and 95% for suspension.Step 3: Verify other options
The other options swap actions or percentages incorrectly or notify at 100% instead of 80%.Final Answer:
CREATE RESOURCE MONITOR cost_control WITH CREDIT_QUOTA = 1500 TRIGGERS ON 80 PERCENT DO NOTIFY ON 95 PERCENT DO SUSPEND; -> Option DQuick Check:
Notify 80%, suspend 95% = CREATE RESOURCE MONITOR cost_control WITH CREDIT_QUOTA = 1500 TRIGGERS ON 80 PERCENT DO NOTIFY ON 95 PERCENT DO SUSPEND; [OK]
- Swapping notify and suspend percentages
- Setting notify higher than suspend
- Using 100% notify instead of 80%
Ready to prove it?
Pass the test and this pattern moves unknown
