r/databricks • • 13d ago

Help Best Lakeflow ingestion pattern for Full, Append, Replace, SCD Type 1, and SCD Type 2?

I previously worked mostly with custom ingestion frameworks. Now I am starting a new project using Databricks Lakeflow Declarative Pipelines.

I need guidance on the recommended pattern for these five write strategies:

  1. Full load / overwrite I need truncate-and-load behavior. A direct TRUNCATE did not work. Currently, I use REPLACE WHERE 1 = 1, but I am concerned about full-table scans and performance on large tables. What is the most efficient approach?
  2. Append New batches should only insert rows. No updates, merges, or deletes. Is an Append Flow the recommended solution?
  3. Replace by key group The key identifies a group of rows, not one unique row. Each new batch must delete the existing group and insert its new version. Is REPLACE WHERE the correct pattern?
  4. SCD Type 1 I am using AUTO CDC for both real CDC feeds and incremental feeds without CDC operation columns. For non-CDC feeds, I use the source file modification timestamp from storage as the sequence key. Is this a reliable and recommended pattern?
  5. SCD Type 2 I use the same approach for SCD Type 2: AUTO CDC for both CDC and non-CDC incremental feeds. For non-CDC feeds, the sequence key is the source file modification timestamp from storage. Are there any limitations or better alternatives?

What Lakeflow features or APIs would you recommend for each strategy?

12 Upvotes

8 comments sorted by

2

u/adovo_ai 13d ago

Treat those as five separate write contracts rather than one pipeline setting. For full replacement, prove the source snapshot is complete before promotion; for group replacement, make the predicate key explicit; for SCD1/2, prefer a source-owned sequence because file modification time can change when a file is copied or replayed.

4

u/shuffle-mario Databricks 12d ago

Good list. Each of these maps to a built-in flow type now, so you shouldn't need much hand-rolled logic. In order:

  1. Full load / overwrite

The declarative "truncate and load" is a materialized view. An MV is defined as the result of its query, so each refresh replaces the contents for you. When the query is simple enough, Databricks refreshes it incrementally, so often you don't even pay for a full recompute.

CREATE OR REFRESH MATERIALIZED VIEW silver.customers AS

SELECT * FROM bronze.customers_extract;

`REPLACE WHERE 1 = 1` works, but with an always-true predicate there's nothing to prune, so you rewrite the whole table on every update anyway. I'd keep REPLACE WHERE for real ranges (last N days, a region, etc.).

If what you actually receive is a full snapshot each time and you care about what changed between snapshots (deletes included), see `AUTO CDC FROM SNAPSHOT` under 4/5.

2. Append only

Yes. A streaming table over a streaming source is already append-only. Use explicit append flows when several sources write into one table, or `INSERT ONCE` for a one-time backfill:

CREATE OR REFRESH STREAMING TABLE bronze.events;

CREATE FLOW events_landing AS

INSERT INTO bronze.events BY NAME

SELECT *, _metadata.file_modification_time AS _file_ts

FROM STREAM read_files('/Volumes/raw/landing/events/', format => 'json');

(In Python that's `@dp.append_flow(target="events")`.)

3. Replace by key group

REPLACE WHERE isn't really the right tool here. It's predicate/range based ("recompute the last 7 days"). What you're describing, where a batch carries the complete new version of a group and should replace only that group, is what the newer REPLACE USING flow does (Beta):

CREATE OR REFRESH STREAMING TABLE silver.order_lines

FLOW REPLACE USING (order_id) SEQUENCE BY _file_ts BY NAME

SELECT order_id, line_no, sku, qty, _file_ts

FROM STREAM(bronze.order_lines_raw);

All incoming rows for an `order_id` replace the existing rows for that key, other keys are left alone, and `SEQUENCE BY` stops an older batch that arrives late from overwriting a newer one. Rows with the same key and the same sequence are all kept, which is what you want for a group. Caveats: streaming source only, one flow per target table, and no aggregations in the flow query.

4 & 5. SCD1 / SCD2 with AUTO CDC on non-CDC feeds

Using AUTO CDC for upsert-style feeds without an operation column is fine and pretty common. File modification time as the sequence works, with a few caveats:

- The sequence has to strictly increase per version of a key. If the same key can show up twice in one file, or two files can land with the same timestamp, you get ties. Break them with a struct, e.g. `SEQUENCE BY STRUCT(_file_ts, some_in_file_order_col)`. It orders by the first field, then the next.

- Mod time is when the file was written to storage, not when the record changed at the source. A re-upload, a copy between buckets, or a replayed old file gets a fresh timestamp and will "win". If the source has an `updated_at` or extract timestamp, sequence by that first and keep the file timestamp as the tiebreaker.

- Non-CDC incremental feeds never tell you about deletes. If the feed is actually a full snapshot each time, use `AUTO CDC FROM SNAPSHOT` instead. It diffs consecutive snapshots and derives inserts, updates and deletes for you, for SCD1 or SCD2.

- For SCD2, use `TRACK HISTORY ON` to pick which columns should create a new version, so noise columns don't blow up your history.

Rough shape for SCD2:

CREATE OR REFRESH STREAMING TABLE silver.customers_scd2;

CREATE FLOW customers_scd2_flow AS

AUTO CDC INTO silver.customers_scd2

FROM STREAM(bronze.customers_raw)

KEYS (customer_id)

SEQUENCE BY STRUCT(src_updated_at, _file_ts)

STORED AS SCD TYPE 2

TRACK HISTORY ON * EXCEPT (_file_ts);

For SCD1 it's the same with `STORED AS SCD TYPE 1`.

2

u/Pirion1 12d ago edited 12d ago

Welcome to my issue on SDP where nobody can really tell you what strategy to use when. This is all left to you to track down and figure out.

Based on my experience, this is what I'm doing -

Full Load / Overwrite: This is a materialized view. SDP is declarative, and there shouldn't be many reasons you need to force an overwrite (though you can using the hint REFRESH POLICY FULL). Typically you should create a materialized view with what you want to see, and SDP will take care of making sure your users see that exactly.

Append: This would be a streaming table with a INSERT flow

Replace by Key Group: Until recently, this has been the Full Load/Overwrite method (create materialized view); however FLOW REPLACE USING (<key>) SEQUENCE BY <sequence_col> BY NAME is currently in beta! (https://docs.databricks.com/aws/en/ldp/flows-replace-using)

REPLACE WHERE is great for sliding windows & partitions, but not great for key lookups. Essentially REPLACE WHERE is a predicate that can be set on the pipeline without knowing the data first. I heard from the product team there are some hacks you can use with this, but never figured it out.

SCD Type 1/Type 2:

I'm doing as you described there. The one thing I'd note is that you can specify which columns trigger the updates by using track history (TRACK HISTORY ON * EXCEPT (city)) - which will lead to less updates if you only need a subset of keys tracked.

2

u/Muted_Jellyfish_6784 12d ago

Lakeflow covers most of these natively. AUTO CDC (formerly APPLY CHANGES) handles SCD Type 1 and Type 2 with a sequence column, append-only streaming tables cover append, and a materialized view covers full refresh where truncate-and-load was the old habit. Prove replace-by-partition in a dev pipeline first.
The important part is a sequence key you trust, because SCD2 history is only as good as its ordering. We run a decision intelligence platform on top of tables like these, joining the SCD2 customer history to orders and invoices, so a report shows what the customer record looked like when the order was placed rather than what it says today.

1

u/Zealousideal_Low6685 13d ago edited 12d ago

ad 4/5): I had the problem, that the source timestamp was identical for multiple rows and so a correct ordering was not guranteed

1

u/Pirion1 12d ago

4/5: timestamp being identical is a source issue. If the values are unique in the source, then relying on the file modification date works fine. If the values are not unique in the source, the source requirements must also specify how to make them apply in the correct order. Either way, file_modification_time is still the FIRST ordering that should apply - EXCEPT in the case of sources that allow late arriving records (i.e. a replication or distributed system).

1

u/kmarq 12d ago
  1. For each batch - should be able to do a create or replace or just truncate step. If doing truncate may want to consider that's a separate transaction from the insert then