Streams and Tasks
Change tracking, scheduled SQL, task graphs, incremental ELT, serverless tasks, and production pitfalls.
Streams and Tasks From Scratch
Streams track table changes and tasks run scheduled SQL. Together they let you build incremental pipelines inside Snowflake.
Why this matters: Rebuilding every table from scratch wastes compute and delays dashboards. Incremental pipelines process only what changed, but they need reliable change tracking and scheduling.
The Concepts You Must Own
- ✓Streams capture change metadata for inserts, updates, and deletes on a source table.
- ✓Tasks run SQL on a schedule or after another task completes.
- ✓Task graphs chain multiple dependent steps.
- ✓Streams are consumed when used in DML such as INSERT or MERGE.
- ✓Staleness happens if changes are not consumed within retention.
| Concept | Meaning | Why it matters |
|---|---|---|
| Stream | Change tracking object. | Incremental reads without scanning full source. |
| Task | Scheduled SQL execution. | Native orchestration for Snowflake SQL. |
| Task graph | Dependency chain of tasks. | Multi-step pipelines. |
| Staleness | Stream history no longer usable. | A production risk if tasks fail too long. |
| MERGE | Upsert changes into target. | Common stream consumption pattern. |
Step-by-Step Workflow
- ✓Create a source table and a stream on it.
- ✓Write a MERGE that reads changed rows from the stream.
- ✓Create a task to run the MERGE on a schedule.
- ✓Chain downstream tasks for Silver and Gold transformations.
- ✓Monitor task history, failures, lag, and stream staleness.
CREATE STREAM ORDERS_STREAM
ON TABLE RAW.ORDERS;
CREATE TASK MERGE_ORDERS_TASK
WAREHOUSE = WH_TRANSFORM_M
SCHEDULE = '5 MINUTE'
AS
MERGE INTO SILVER.ORDERS tgt
USING ORDERS_STREAM src
ON tgt.order_id = src.order_id
WHEN MATCHED THEN UPDATE SET status = src.status, updated_at = src.updated_at
WHEN NOT MATCHED THEN INSERT (order_id, status, updated_at)
VALUES (src.order_id, src.status, src.updated_at);
ALTER TASK MERGE_ORDERS_TASK RESUME;Do not read the example as magic syntax to memorize. Read it as a production habit: name the objects clearly, make assumptions visible, preserve enough metadata to debug later, and keep the business promise attached to the SQL.
Common Mistakes That Break Snowflake Projects
- ✓Assuming a stream stores a separate copy of all changed data forever.
- ✓Forgetting to resume a task after creating it.
- ✓Building task graphs without alerting on failure.
- ✓Consuming a stream accidentally in a test DML statement.
- ✓Ignoring delete handling from change metadata.
How to debug this topic
Start by asking what promise failed: freshness, correctness, access, speed, or cost. Then inspect the Snowflake evidence: query history, warehouse metering, task history, copy history, grants, row counts, and sample records. Good Snowflake debugging is not guessing. It is reading the platform metadata until the failure has a shape.
Production Notes
- ✓Use task history views for monitoring and alerting.
- ✓Keep tasks small and composable instead of one giant SQL script.
- ✓Document whether each incremental model handles deletes.
- ✓Have a full-refresh fallback for corrupted incremental state.
Production standard: A Snowflake design is not complete when the query returns rows. It is complete when the team knows who owns it, how fresh it should be, how access is controlled, what it costs, how to detect failure, and how to recover safely.
Explain It Like a Professional
Streams track changes and tasks schedule SQL. A common pattern is stream plus MERGE plus task. Mention that streams are consumed by DML, can become stale, and need monitoring. Tasks can form graphs, but production still needs alerting and recovery procedures.
Mini project
Build RAW to SILVER incremental orders with a stream and task, then a second task that updates GOLD.DAILY_REVENUE after the first task succeeds.
Questions you should answer out loud
- ✓How would you explain Streams and Tasks to a non-technical manager?
- ✓Which Snowflake objects, roles, or SQL statements does this topic use?
- ✓What can fail in production and which metadata view would you inspect first?
- ✓What is the cost or security risk if this is implemented carelessly?
- ✓How would you test that the result is correct and rerunnable?
🎯 Key Takeaways
- ✓Streams and tasks enable native incremental ELT.
- ✓Streams track changes; tasks run scheduled SQL.
- ✓MERGE is the common bridge between a stream and target table.
- ✓Monitoring matters because streams can become stale.
- ✓Incremental pipelines still need full-refresh recovery plans.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.