6 guardrails against common mistakes that inflate Snowflake credit usage
Use Flexter to turn XML and JSON into Valuable Insights
- 100% Automation
- 0% Coding
Dodge costly pitfalls
Snowflake implementation has pioneered dozens of innovations in the data platform space. The long list includes separation of compute and storage, physical workload separation, data sharing, data app marketplace and many more. One innovation is the pay per use pricing model and instant scalability of compute resources. You can command hundreds or even thousands of servers at a fingertip but only pay for the resources you actually use. What is not to like?
This is a powerful tool to manage costs and to scale your workloads. However, we all know: with great power comes great responsibility. Great flexibility and choice can come at a price. Mistakes and accidents can happen and are a part of life. As long as you learn your lessons and don’t repeat them. By keeping a close watch and implementing some important guardrails in Snowflake, you can either learn how to prevent future errors or even catch them just in time before they lead to compute bill pandemonium.
In this blog post, we will explore crucial safeguards that prevent unexpected surges in your Snowflake compute usage.
Snowed under by costs no more 😀
Right size your virtual warehouse… and monitor
In an ideal world we want to run our virtual warehouse at exactly 100% of capacity. Not at 101% and not at 99%. If we under-provision resources then queries start to slow down, to queue, or both. If we overprovision resources we leave compute on the table and pay more than we use.
Here are some rule of thumb sizing recommendations to help you get started with virtual warehouse sizing. They are based on the data size of the tables in your query. Start with the smallest recommended virtual warehouse size for the corresponding data size and then gradually scale up until there are no significant improvements anymore.
|
Data size |
VWH size |
|---|---|
| < 1 GB | XS S |
| 1- 20 GB | S M L XL 2XL |
| 20 – 50 GB | M L XL 2XL 3XL |
| > 50 GB | XL 2XL 3XL 4XL |
Under provisioning resources
When you underprovision resources you will see one or more of the following symptoms.
- Queries end up in the queue and need to wait until a slot becomes available. It’s similar to a popular night club. There just isn’t enough space for everyone. As a rule of thumb you can run 8 queries concurrently on an individual cluster in Snowflake. Each query takes up one vCPU.
- Queries spill to disk
- Your queries take longer to run than normally
You can identify all of these issues by looking at the account usage views. Reach out to us if you need help with this.
There are various strategies you can use to address these issues
- You can increase the size of your virtual warehouse. This is useful for CPU intensive queries but also for situations where queries end up in the queue. This does not increase the degree of parallelism. It will be 8 independent of the size of your VWH. However, it cuts the execution time of queries in half when you also double the virtual warehouse size (this works for most but not all workloads). As such the queue times are also cut in half and queries come off the queue much faster.
- Queries spill to disk when a virtual warehouse runs out of memory during certain operations such as joins. The root cause of this problem often is bad SQL code or bad physical database design (missing cluster keys etc.). When queries spill to disk the first step should be to optimise the query itself and tune the physical database design. This should reduce memory consumption. Alternatively, Snowflake datalake architecture has introduced memory optimised virtual warehouses. They are branded as Snowpark optimised but memory intensive workloads in general benefit from this boxes with a huge amount of RAM.
- If you have a large number of concurrent queries you can run multiple virtual warehouses concurrently. This will actually increase the degree of parallelism and reduce queuing even further. You can combine a larger virtual warehouse with concurrent virtual warehouses to empty any queue.
- Use Snowflake query acceleration if your query involves a large table scan or other very expensive operations. Query acceleration offloads the table scan part of the explain plan to additional compute resources to free up resources on the virtual warehouse itself.
Overprovisioning resources
A new feature called Warehouse utilisation metrics will be rolled out soon. It is currently in private preview and shows the utilisation of resources as a percentage per virtual warehouse.
The remedy for overprovisioning resources is to scale down your virtual warehouse, e.g. from L to M or to combine workloads on the same virtual warehouse, e.g. instead of having two underutilised virtual warehouses for Sales and HR you could combine the workload on a single virtual warehouse.
FlowForward.
All Things Data Engineering
Straight to Your Inbox!
Autosuspend virtual warehouses! BUT keep workload type in mind
Most of you probably know that it is best practice to set your Snowflake virtual warehouses to autosuspend and auto resume. However, there is a catch that some of you might not be aware of.
Skip to the final SQL?
If you want to skip ahead and see the final SQL implementation, you can head straight to the end!
By auto suspending a virtual warehouse too quickly you will flush the cache of the warehouse. Subsequent queries that resume the warehouse need to reseed the cache. You will see a temporary degradation in performance when the VWH resumes. It’s a classic tradeoff. You save on compute costs but lose on query performance. You will need to find a balance between saving costs by suspending quickly and benefiting from Snowflake’s caching.
The balance depends on the type of workload. Some types of workloads benefit more from caching than others.
- As a rule of thumb ETL workloads do not benefit much from caching. For these workloads you can autosuspend your virtual warehouse immediately.
- BI style queries benefit from caching and as a rule of thumb you should set to suspend your warehouse after 10 minutes of idle time.
Here is a query to identify virtual warehouses that might benefit from shorter suspension times. I have “borrowed” it from the excellent post deep dive into Snowflake compute by Snowflake’s Samartha Chandrashekar.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
SELECT WAREHOUSE_NAME ,COUNT(*) AS QUERY_COUNT ,SUM(BYTES_SCANNED) AS BYTES_SCANNED ,SUM(BYTES_SCANNED*PERCENTAGE_SCANNED_FROM_CACHE) AS BYTES_SCANNED_FROM_CACHE ,SUM(BYTES_SCANNED*PERCENTAGE_SCANNED_FROM_CACHE)/SUM(BYTES_SCANNED) AS PERCENT_SCANNED_FROM_CACHE FROM "SNOWFLAKE"."ACCOUNT_USAGE"."QUERY_HISTORY" WHERE START_TIME>=dateadd(month ,-1 ,CURRENT_TIMESTAMP()) AND BYTES_SCANNED>0 GROUP BY 1 ORDER BY 5 ; |
The query identifies virtual warehouses that don’t tap into the cache frequently. This may mean that they are suspended too quickly. Only use this query for virtual warehouses running BI or interactive query workloads. It is not relevant for ETL workloads.
Here are some tips and tricks for successfully using autosuspend in Snowflake
- Monitor for virtual warehouses that do not have auto resume and auto suspend enabled.
- Understand Workload Patterns: Before setting an auto-suspend duration, analyse your workload’s activity patterns to understand the typical idle times.
- Opt for a Conservative Start: Begin with a conservative auto-suspend value to avoid premature suspensions. Monitor its impact and adjust based on actual usage patterns.
- Pair with AUTO_RESUME: Ensure that the AUTO_RESUME feature is enabled. This allows the virtual warehouse to automatically restart when a new query is submitted, providing a seamless experience to users.
- Be Cautious with Short Durations: Setting an extremely short auto-suspend time can lead to frequent suspensions and resumptions. This might introduce cache seeding latencies when queries are executed after resumption.
Kill long running queries! BUT keep workload type in mind.
Another useful autosuspend feature is to terminate very long running queries after they hit a threshold. The parameter STATEMENT_TIMEOUT_IN_SECONDS automatically kills long running queries when they hit the timeout threshold.
Don’t make the mistake of setting the STATEMENT_TIMEOUT_IN_SECONDS param at the account level. The threshold varies by workload and should be set at the virtual warehouse level (assuming you follow best practice and split out your workloads by virtual warehouse).
- For ETL workloads set the threshold to several minutes or even an hour. If single queries regularly run for more than an hour you probably have a more fundamental problem and should optimise your ETL query. Split it into smaller and more manageable chunks. Increase your virtual warehouse size etc.
- For BI workloads this should be set to a few minutes max. If your users have to wait for minutes to get their reports you have another problem.
- Interactive workloads such as data discovery sit in the middle of the previous two. 15-20 minutes is a good value to start with.
Here are some more tips and tricks for finding the right value for STATEMENT_TIMEOUT_IN_SECONDS
- Establish a Baseline: Before setting a default timeout, monitor the duration of typical queries for each workload to get an idea of what an appropriate timeout might be.
- Set STATEMENT_TIMEOUT_IN_SECONDS at the workload / virtual warehouse level.
- Educate Users: Make sure that users and developers are aware of the timeout setting. This way, if their queries are terminated due to a timeout, they understand why.
- Optimise Long Queries: If you find certain queries are consistently being terminated due to timeouts, it might be an indication that they need optimization.
- Use with Care for Critical Workloads: Be cautious when setting this parameter for critical workloads or ETL processes. Setting it too aggressively might disrupt important processes.
- Monitoring and Alerting: Set up monitoring and alerts to be informed when queries are terminated due to this timeout. This can help in identifying potential issues or patterns.
- Consider Ad-hoc vs. Scheduled Jobs: You might want to have different timeout settings for ad-hoc queries (run by analysts, for example) versus scheduled jobs or processes.
- Documentation: Always document the rationale behind the chosen timeout value, especially if you set it at a non-default value. This aids in transparency and future troubleshooting.
Don’t overschedule tasks and pipelines
One common source of accidents and bad practices is to overschedule tasks and pipelines.
An example: Engineers set up a task or alert to run every minute to test a feature but then forget to disable the task. This is akin to a virtual warehouse running 24/7 without auto suspend.
You can get the full list of scheduled tasks from the account usage views TASK_HISTORY and SERVERLESS_TASK_HISTORY. Set up an alert against these views to identify frequently running tasks.
For alerting and notifications use the same virtual warehouse. If you already have a virtual warehouse that is running 24/7 then use this VWH for alerts.
When pipelines are scheduled more frequently than necessary, it results in unnecessary data processing and compute usage. I often see data pipelines scheduled to process data in realtime or as a stream for no good business reason. Most use cases don’t require data to be updated every minute. Always question the business value of a real time use case by asking the question: what action do you take in near real time based on the insights you get in real time. Often you get blank stares in return.
Alert for credit usage outliers
A good way to detect accidental usage or overconsumption is to monitor for compute outliers. It’s a two step process.
Step 1: Create a baseline of your typical credit usage.
Creating a baseline can be done by looking at your historical data in the account usage view WAREHOUSE_METERING_HISTORY and usage views for serverless compute such as AUTOMATIC_CLUSTERING_HISTORY. The purpose of the baseline is to establish what’s “normal” usage for your operations. This can be done by calculating the rolling average of your daily / weekly credit consumption.
Step 2: Set Thresholds.
Based on your historical analysis, set thresholds (e.g., rolling average + 2x standard deviations) to detect credit usage outliers. Anything beyond this threshold can be considered an anomaly or outlier. Outliers require further investigation and drill down into account usage views to identify the root cause(s) of the problem.
Experiment with new features… but keep data volumes small
Carefully test new features on smaller data volumes before rolling them out for larger volumes. Setting up secondary indexes through Search Optimisation Services is great to speed up lookups but can be catastrophic when not planned properly for TB sized tables in a production environment.
And one final tip: read the manual before testing a new feature. It always surprises me that a majority of engineers don’t read the docs or just scan them superficially.
Appendix
This stored procedure, SCAN_QUERY_HISTORY_FOR_ANTIPATTERNS, retrieves the most recent query start time from a specific task’s history. Depending on whether this start time is available (not null), the procedure extracts different sets of query histories and identifies potential anti-patterns within them.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
CREATE OR REPLACE PROCEDURE SCAN_QUERY_HISTORY_FOR_ANTIPATTERNS() RETURNS STRING LANGUAGE JAVASCRIPT EXECUTE AS CALLER AS $ /* Stored Procedure: SCAN_QUERY_HISTORY_FOR_ANTIPATTERNS Description: This SP retrieves query history based on a specified warehouse and identifies potential anti-patterns in the queries. */ var result_set1, completed_time, final_query, sql_command3, cte_query; // First Query to get Start time result_set1 = snowflake.execute({sqlText: ` SELECT TO_VARCHAR(QUERY_START_TIME, 'YYYY-MM-DD HH24:MI:SS') AS FormattedTime FROM TABLE(information_schema.task_history(task_name=>'FIND_ANTIPATTERN_FROM_QUERY_HISTORY')) QUALIFY ROW_NUMBER() OVER (ORDER BY QUERY_START_TIME DESC) = 1; `}); // Determine if the task invoking this stored procedure is executing for the first time. result_set1.next(); completed_time = result_set1.getColumnValue(1); if (completed_time === null) { // This function retrieves query history based on the specified warehouse. // The RESULT_LIMIT parameter specifies the maximum number of rows to return, with a range from 1 to 10,000. // If RESULT_LIMIT is not specified, the default value is 100. insert_query = `INSERT INTO AP_OUTPUT(QUERY_ID, QUERY_TEXT, AP_TYPE, POS, STATUS, STATUS_MSG, AP_QUERY_PART) WITH FilteredQueries AS ( SELECT QUERY_ID, QUERY_TEXT FROM TABLE(information_schema.query_history_by_warehouse(RESULT_LIMIT => 10000)) WHERE QUERY_TEXT NOT LIKE 'CREATE%' ) SELECT QUERY_ID, QUERY_TEXT, AP_TYPE, POS, STATUS, STATUS_MSG, AP_QUERY_PART FROM FilteredQueries f, LATERAL APP.CODE_SCHEMA.get_ap(f.QUERY_TEXT, true, false) a;`; } else { insert_query = `INSERT INTO AP_OUTPUT(QUERY_ID, QUERY_TEXT, AP_TYPE, POS, STATUS, STATUS_MSG, AP_QUERY_PART) WITH FilteredQueries AS ( SELECT QUERY_ID, QUERY_TEXT FROM TABLE(information_schema.query_history_by_warehouse(END_TIME_RANGE_START=>to_timestamp_ltz('${completed_time}'),RESULT_LIMIT => 10000)) WHERE QUERY_TEXT NOT LIKE 'CREATE%' ) SELECT QUERY_ID, QUERY_TEXT, AP_TYPE, POS, STATUS, STATUS_MSG, AP_QUERY_PART FROM FilteredQueries f, LATERAL APP.CODE_SCHEMA.get_ap(f.QUERY_TEXT, true, false) a; `; } snowflake.execute({sqlText: insert_query}); return "Success"; $; |
Unoptimized pipelines or misconfigured virtual warehouses can quietly consume thousands in unneeded cloud credits. Speak directly with a senior architect at our Snowflake consultancy in Ireland for a thorough compute cost audit and performance alignment assessment.