BigQuery/Snowflake Cost Governance Framework
As Sherlock, I'll deduce and architect a robust cost governance framework for BigQuery (Google Cloud) and Snowflake. This includes budgets (soft spending targets with alerts), caps (hard limits that enforce stops), and guardrails (preventive policies, monitoring, and automation). The goal: prevent bill shocks, enforce accountability, and optimize spend without hindering analytics.
This framework is modular, IaC-ready (Terraform/CLI examples), and uses native tools. It assumes:
- BigQuery: GCP project-level.
- Snowflake: Account/organization-level.
- Multi-tenant setups (e.g., teams/projects).
1. Core Principles
- Budgets: Threshold-based (e.g., 80% alert → 100% notify → auto-mitigate).
- Caps: Absolute limits (e.g., suspend resources at 110%).
- Guardrails:
- Pre-execution checks (e.g., dry-run queries).
- Post-execution audits (e.g., labels, quotas).
- Automation (e.g., suspend warehouses, kill queries).
- Monitoring: Centralized dashboards (Looker Studio/Grafana) + alerts (Pub/Sub, email/Slack).
- Optimization: Partitioning, clustering, caching, reservations.
Estimated ROI: 30-70% cost reduction via enforcement.
2. BigQuery Cost Governance
BigQuery costs: On-demand queries (6.25/TBscanned),∗∗slots∗∗(flat−ratereservations),∗∗st0.02/GB/mo).
Budgets & Caps
Use Cloud Billing Budgets (GCP-native):
# Terraform: budgets.tf
resource "google_billing_budget" "bq_budget" {
billing_account = "billingAccounts/XXXXXX"
display_name = "BigQuery Monthly Budget"
budget_amount {
specified_amount {
currency_code = "USD"
units = "5000" # $5K cap
}
}
threshold_rules {
threshold_percent = 50 # Alert at 50%
spend_basis = "FORECASTED_SPEND"
}
threshold_rules {
threshold_percent = 90
actions {
pubsub_topic = google_pubsub_topic.bq_alert.id # Trigger Lambda/Cloud Function
}
}
all_updates_rule {
disable_default_iam_recipients = true
monitoring_notification_channels = [google_monitoring_notification_channel.slack.id]
}
}
- Caps: At 100%, auto-apply query quotas via IAM or API.
Guardrails
-
Slot Reservations (Editions: Standard → Enterprise for autoscaling caps):
# gcloud CLI: Reserve 500 slots ($40K/mo commitment, predictable)
gcloud alpha bigquery-reservations create --reservation-id=my-reservation \
--location=US --edition=ENTERPRISE --slot-capacity=500 --ignore-idempotency-checks
-
Query Controls (prevent expensive scans):
-- BigQuery UI/API: Create Query Control
CREATE QUERY CONTROL `project.dataset.query-control`
OPTIONS (
maxBytesBilled = 10000000000, -- 10GB cap per query
requireLabel = {'team': 'data-eng'} -- Enforce labels
);
-
Automation Guardrails (Cloud Function triggered by budget alert):
# functions/main.py: Suspend project billing or set quotas
from google.cloud import bigquery
client = bigquery.Client()
job_config = bigquery.QueryJobConfig(maximum_bytes_billed=1e10) # Enforce globally
# Kill long-running jobs: client.cancel_job(job_id)
-
Monitoring Queries (Scheduled daily):
-- bq_cost_dashboard.sql (Schedule via BigQuery UI)
SELECT
user_email,
SUM(bytes_processed)/POW(1024,3) AS TB_scanned,
cost AS dollars_spent
FROM `region-us.INFORMATION_SCHEMA.JOBS_*`
WHERE creation_time >= TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
GROUP BY user_email
HAVING dollars_spent > 100 -- Alert high spenders
ORDER BY dollars_spent DESC;
Dashboard: Export to Looker Studio with INFORMATION_SCHEMA.JOBS_BY_PROJECT.
3. Snowflake Cost Governance
Snowflake costs: Compute credits (2−4/credit,warehousesize/time),∗23/TB/mo).
Budgets & Caps
Use Resource Monitors (native caps):
-- SQL: Create org/account-level monitor (run as ACCOUNTADMIN)
CREATE RESOURCE MONITOR bq_sf_gov_global
WITH CREDIT_QUOTA = 1000 -- 1K credits cap/mo
STARTING = '2024-01-01'
MONTHLY;
CREATE RESOURCE MONITOR team_dataeng_monitor
WITH CREDIT_QUOTA = 200
STARTING = '2024-01-01'
MONTHLY;
-- Assign to warehouses/users/roles
ALTER WAREHOUSE compute_wh SET RESOURCE_MONITOR = bq_sf_gov_global;
-- Caps trigger: SUSPEND (immediate), NOTIFY (alert)
ALTER RESOURCE MONITOR bq_sf_gov_global SET CREDIT_QUOTA = 1100; -- 10% buffer
- Budgets: Query
ACCOUNT_USAGE.RESOURCE_MONITORS for thresholds.
Guardrails
-
Warehouse Auto-Suspend:
ALTER WAREHOUSE compute_wh
SET AUTO_SUSPEND = 300 -- 5min idle
AUTO_RESUME = TRUE
QUERY_ACCELERATION_CREDIT_SCALE = 1; -- Cap acceleration
-
Query Preemption & Profiles:
-- Concurrency/scaling policies
ALTER WAREHOUSE compute_wh SET
MIN_CLUSTER_COUNT = 1,
MAX_CLUSTER_COUNT = 10, -- Hard cap on scaling
SCALING_POLICY = 'ECONOMY'; -- Cost-optimized
-
Automation (Snowpipe/Tasks/Streams):
- Use Alerting on
QUERY_HISTORY:
-- Scheduled Task: Kill expensive queries
CREATE TASK kill_expensive_queries
WAREHOUSE = util_wh
SCHEDULE = '5 MINUTE'
AS
DELETE FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY())
WHERE execution_status = 'RUNNING'
AND bytes_scanned > 1000000000000 -- 1TB
AND user_name != 'ACCOUNTADMIN';
ALTER TASK kill_expensive_queries RESUME;
-
Monitoring Queries:
-- sf_cost_dashboard.sql (Schedule as Task)
SELECT
warehouse_name,
SUM(credits_used_compute) AS credits,
COUNT(*) AS queries
FROM TABLE(SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY())
WHERE start_time >= DATEADD(MONTH, -1, CURRENT_DATE())
GROUP BY warehouse_name
HAVING credits > 50
ORDER BY credits DESC;
Dashboard: Snowflake Native App or export to Tableau/Grafana.
4. Cross-Platform Implementation Guide
| Step | BigQuery | Snowflake | Tools |
|---|
| 1. Setup Budgets | Cloud Billing Budget | Resource Monitor (1K credits) | Terraform/CLI |
| 2. Enforce Caps | Slot Reservations + Quotas | CREDIT_QUOTA + SUSPEND | SQL/gcloud |
| 3. Guardrails | Query Controls + Labels | Auto-Suspend + Profiles | Cloud Functions/Tasks |
| 4. Monitor | INFO_SCHEMA.JOBS | ACCOUNT_USAGE.QUERY_HISTORY | Scheduled Queries/Tasks |
| 5. Alert | Pub/Sub → Slack | Email on Monitor Trigger | Webhooks |
| 6. Optimize | Clustering, BI Engine | Search Opt, Materialized Views | Native |
Terraform Full Example (GCP + Snowflake Provider)
# Add providers: google-beta, snowflake
provider "snowflake" { ... }
# Snowflake Resource Monitor
resource "snowflake_resource_monitor" "global" {
name = "GLOBAL_GOV"
credit_quota = 1000
frequency = "MONTHLY"
}
5. Best Practices & Advanced
- Labels/Tags: Enforce
cost_center=teamX → chargeback via views.
- ML Forecasting: Use BigQuery ML for spend prediction.
- Multi-Cloud: Terraform workspaces for GCP/AWS (Snowflake).
- Testing: Start with 10% budgets, simulate loads.
- Common Pitfalls: Ignore caching (free re-runs), unpartitioned tables.
- Savings: Reservations (BigQuery: 50% off), Standard Edition (Snowflake: cheaper credits).
Deploy this, and your costs are locked down like a crime scene. Questions on customization? Provide your setup details. 🔍