Optimising Ingestion into Snowflake with Snowpipe Streaming

Background

With today’s AI models, it is possible to ship software for a technology without having to really understand it: an idea becomes a working feature or fix in a fraction of the time it used to, and then it is straight on to the next thing. When iterating that quickly and context-switching that often, it is often easy to forget to slow down and dedicate some time to properly learning how something works. Most of the time, this is not a hard requirement, but knowing the technical details can help you make better decisions, notice when something is wrong, and get more out of working with AI the next time around.

At Meltano, we are continually invested in optimising the runtime cost and performance of our many supported taps and targets. Our Snowflake target now supports Snowpipe Streaming for exactly that reason, so this is me setting some time aside to explore it, understand how it works, and share what I learn.

What is Snowpipe Streaming?

To understand the implications of Snowpipe Streaming, it is first important to understand what a warehouse is in the context of Snowflake.

A Snowflake warehouse is a cluster of on-demand compute resources required to execute queries, which includes loading data. Warehouses consume credits while they are active, at varying rates depending on the configured size. Importantly, the minimum time a warehouse can be active for is 60 seconds, meaning that even if loading data takes much less time than that, you still incur 60 seconds’ worth of credit usage. A warehouse also stays active after the work finishes, until it auto-suspends: by default, after 10 minutes of inactivity.

Snowpipe Streaming is an alternative method of loading data into Snowflake in an append-only manner, without spinning up a warehouse. As a result, Snowpipe Streaming is subject to a throughput-based billing model, which often works out cheaper than a warehouse for standard load operations. At the time of writing, Snowpipe Streaming costs 0.0037 credits/GB of uncompressed data (see the Snowflake Credit Consumption Table). As a comparison, the minimum possible cost with an X-Small warehouse is about 0.017 credits, assuming an equivalent 1 GB load takes no longer than 60 seconds and the warehouse suspends straight away. Additionally, unlike a warehouse, you pay only for what you ingest, which can drastically reduce usage for smaller loads (i.e. less than 1 GB).

How does Snowpipe Streaming work?

Snowpipe Streaming uses channels to send rows to a target table via a pipe (similar to Unix pipelines, if you are familiar with Bash/shell scripting). There are two types of channel:

Named channels guarantee order and “exactly-once” delivery, at the cost of client-side orchestration and a 20 MB/s per-channel throughput limit. Elastic channels do not guarantee order and instead promise “at-least-once” delivery, but they do not need to be managed by a client, and are subject only to the much higher 20 GB/s per-table throughput limit.

Challenges of Snowpipe Streaming

Snowpipe Streaming by nature is an append-only operation, which means you lose upsert/merge semantics that are possible with a typical warehouse load (Snowflake tables can define a primary key, but do not enforce it). As a result, a table can end up with more than one row for the same key: a full refresh (backfill) appends every row again, an incremental sync appends a new row each time a record changes, and Elastic channels’ “at-least-once” delivery means a retry can write the same row twice.

If you want to take advantage of the cost benefits of Snowpipe Streaming to ingest data, we recommend you move any deduplication logic downstream to a view or dbt model.

How does Snowpipe Streaming apply in the context of Meltano?

Historically, target-snowflake has loaded data primarily by uploading files to a stage and querying them. For small, frequent loads using this method, most of what you pay for is time when the warehouse does zero work, including the time before it auto-suspends after every load.

Meltano’s target-snowflake now has native support for Snowpipe Streaming as an ingestion method:

config:

  ingestion_method: snowpipe_streaming

In this configuration, the target uses Snowpipe Streaming for streams with no primary key(s), and falls back to an upsert operation (involving the warehouse) for those that do. You can force Snowpipe Streaming for all streams, regardless of schema, by also specifying the append-only load method:

config:

  ingestion_method: snowpipe_streaming

  load_method: append-only

In Snowpipe Streaming mode, the target leverages Elastic channels to scale the data load automatically, so a large backfill is not limited to the throughput of one channel. If Snowflake throttles the load, the target waits and retries instead of failing the sync.

For Elastic channels, the promise of “at-least-once” delivery aligns with the delivery promise of the Meltano Singer SDK that the target is built on. The target emits a STATE message only after Snowflake acknowledges every row before it. If a sync fails, the next run resumes from the last STATE message: a row can load twice, but no row is lost.

Conclusion

Snowpipe Streaming changes what you pay for when you load data into Snowflake. Instead of paying for warehouse time, much of which is idle for small, frequent loads, you pay only for the data you ingest. Meltano’s target-snowflake now supports Snowpipe Streaming as an ingestion method, meaning that any tap-to-target-snowflake pairing can now get this benefit.

If you are already using target-snowflake, give ingestion_method: snowpipe_streaming a try, and let us know how it goes! If you are curious about Meltano, you can register here for a free account and try out Snowpipe Streaming with Snowflake for yourself.

Intrigued?

See Meltano build models with AI and models ready for AI!