Why not copy everything?
ShopKart's orders table grows every day. Copying all of it nightly means more time, more cost and more load on the live app database. Usually only a few thousand rows changed. Each row has an updated_at column, so we can ask for just those.
๐Get Old Watermark
Lookup: last value saved in etl.watermark
๐Get New Watermark
Lookup: MAX(updated_at) now
๐Copy Changed Orders
updated_at > old AND <= new โ Parquet
๐พUpdate Watermark
Stored procedure saves the new value
One-time setup in the source database
-- In the ShopKart app database (T-SQL). Run once.
CREATE SCHEMA etl;
GO
CREATE TABLE etl.watermark (
table_name VARCHAR(100) PRIMARY KEY,
watermark_value DATETIME2 NOT NULL
);
INSERT INTO etl.watermark VALUES ('orders', '1900-01-01'); -- first run copies everything
GO
CREATE PROCEDURE etl.usp_update_watermark @table_name VARCHAR(100), @new_value DATETIME2
AS
UPDATE etl.watermark SET watermark_value = @new_value WHERE table_name = @table_name;
GO
-- Make the incremental query fast
CREATE INDEX ix_orders_updated_at ON dbo.orders (updated_at);The pipeline
{
"name": "PL_Incremental_Orders",
"properties": {
"description": "Copy only orders changed since the last run (watermark pattern).",
"parameters": {
"tableName": { "type": "string", "defaultValue": "orders" }
},
"activities": [
{
"name": "Get Old Watermark",
"type": "Lookup",
"typeProperties": {
"source": {
"type": "AzureSqlSource",
"sqlReaderQuery": {
"value": "SELECT watermark_value FROM etl.watermark WHERE table_name = '@{pipeline().parameters.tableName}'",
"type": "Expression"
}
},
"dataset": { "referenceName": "DS_AzureSql_Query", "type": "DatasetReference" },
"firstRowOnly": true
}
},
{
"name": "Get New Watermark",
"type": "Lookup",
"typeProperties": {
"source": {
"type": "AzureSqlSource",
"sqlReaderQuery": {
"value": "SELECT MAX(updated_at) AS new_watermark FROM dbo.@{pipeline().parameters.tableName}",
"type": "Expression"
}
},
"dataset": { "referenceName": "DS_AzureSql_Query", "type": "DatasetReference" },
"firstRowOnly": true
}
},
{
"name": "Copy Changed Orders",
"type": "Copy",
"dependsOn": [
{ "activity": "Get Old Watermark", "dependencyConditions": ["Succeeded"] },
{ "activity": "Get New Watermark", "dependencyConditions": ["Succeeded"] }
],
"policy": { "timeout": "0.02:00:00", "retry": 2, "retryIntervalInSeconds": 60 },
"typeProperties": {
"source": {
"type": "AzureSqlSource",
"sqlReaderQuery": {
"value": "SELECT * FROM dbo.@{pipeline().parameters.tableName} WHERE updated_at > '@{activity('Get Old Watermark').output.firstRow.watermark_value}' AND updated_at <= '@{activity('Get New Watermark').output.firstRow.new_watermark}'",
"type": "Expression"
}
},
"sink": { "type": "ParquetSink" }
},
"inputs": [ { "referenceName": "DS_AzureSql_Query", "type": "DatasetReference" } ],
"outputs": [
{
"referenceName": "DS_ADLS_Parquet",
"type": "DatasetReference",
"parameters": {
"folderPath": { "value": "@concat('raw/', pipeline().parameters.tableName, '/', formatDateTime(activity('Get New Watermark').output.firstRow.new_watermark, 'yyyy/MM/dd'))", "type": "Expression" },
"fileName": { "value": "@concat(pipeline().parameters.tableName, '_', pipeline().RunId, '.parquet')", "type": "Expression" }
}
}
]
},
{
"name": "Update Watermark",
"type": "SqlServerStoredProcedure",
"dependsOn": [ { "activity": "Copy Changed Orders", "dependencyConditions": ["Succeeded"] } ],
"typeProperties": {
"storedProcedureName": "etl.usp_update_watermark",
"storedProcedureParameters": {
"table_name": { "value": { "value": "@pipeline().parameters.tableName", "type": "Expression" }, "type": "String" },
"new_value": { "value": { "value": "@activity('Get New Watermark').output.firstRow.new_watermark", "type": "Expression" }, "type": "DateTime" }
}
},
"linkedServiceName": { "referenceName": "LS_AzureSql_ShopKart", "type": "LinkedServiceReference" }
}
]
}
}Things to notice:
- The two Lookups have no dependency on each other, so they run in parallel.
- The Copy waits for both (two dependencies = both must succeed).
- The query uses
>old and<=new: the boundary row isn't loaded twice. - The output folder comes from the new watermark's date, and the file name includes the
RunId, so a rerun never overwrites another run's file. - The watermark is updated only after the Copy succeeds.
Run it
We play the part of the two Lookups (yesterday's watermark and today's max), and the framework evaluates the rest:
from data_factory_testing_framework import TestFramework, TestFrameworkType
from data_factory_testing_framework.state import RunParameter, RunParameterType, DependencyCondition
tf = TestFramework(framework_type=TestFrameworkType.DataFactory, root_folder_path="shopkart-adf")
p = tf.get_pipeline_by_name("PL_Incremental_Orders")
run = tf.evaluate_pipeline(p, [RunParameter(RunParameterType.System, "RunId", "3f2a9c1e-run")])
for a in run:
print("โถ", a.name, a.type)
if a.name == "Get Old Watermark":
print(" query:", a.type_properties["source"]["sqlReaderQuery"].result)
a.set_result(DependencyCondition.Succeeded, {"firstRow": {"watermark_value": "2026-11-21T00:00:00Z"}})
elif a.name == "Get New Watermark":
print(" query:", a.type_properties["source"]["sqlReaderQuery"].result)
a.set_result(DependencyCondition.Succeeded, {"firstRow": {"new_watermark": "2026-11-22T06:30:00Z"}})
elif a.name == "Copy Changed Orders":
print(" query:", a.type_properties["source"]["sqlReaderQuery"].result)
o = a.all_properties["outputs"][0]["parameters"]
print(" folder:", o["folderPath"].result, "| file:", o["fileName"].result)
elif a.name == "Update Watermark":
ps = a.type_properties["storedProcedureParameters"]
print(" params:", {k: v["value"].result for k, v in ps.items()})โถ Get Old Watermark Lookup
query: SELECT watermark_value FROM etl.watermark WHERE table_name = 'orders'
โถ Get New Watermark Lookup
query: SELECT MAX(updated_at) AS new_watermark FROM dbo.orders
โถ Copy Changed Orders Copy
query: SELECT * FROM dbo.orders WHERE updated_at > '2026-11-21T00:00:00Z' AND updated_at <= '2026-11-22T06:30:00Z'
folder: raw/orders/2026/11/22 | file: orders_3f2a9c1e-run.parquet
โถ Update Watermark SqlServerStoredProcedure
params: {'table_name': 'orders', 'new_value': '2026-11-22T06:30:00Z'}The Copy's query is exactly the window we wanted, the file lands in raw/orders/2026/11/22, and the stored procedure receives the new watermark. In lesson 11 we turn this into automatic unit tests, including one that proves the watermark is not updated when the Copy fails.
What if something goes wrong?
| Situation | What happens with this design |
|---|---|
| Copy fails | Watermark unchanged โ next run copies the same window again (and the RunId file name avoids clashes) |
| Pipeline runs twice by mistake | Second run finds no rows between the same watermarks โ nothing new |
| A row is updated during the copy | Its new updated_at is above the saved watermark โ picked up next run |
| Clock differences / late commits | Some teams subtract a small overlap (e.g. 5 minutes) and de-duplicate downstream with MERGE |
| A row is deleted | Not seen: use soft deletes, change tracking or CDC |
Other ways to load incrementally
| Method | Best for | Notes |
|---|---|---|
| Watermark column (this lesson) | Tables with a reliable updated_at or increasing ID | Simple, works anywhere |
| Files by last modified | Folders of files | Copy source: modifiedDatetimeStart / End |
| Date-partitioned folders | Files already in yyyy/MM/dd folders | Pass the date as a parameter (lesson 9's tumbling window) |
| SQL Change Tracking | SQL Server / Azure SQL | Tracks which keys changed, including deletes |
| Change Data Capture (CDC) | Full history of changes | ADF can read CDC tables; also a native top-level CDC resource |
| Upsert in the sink | Applying changes to a SQL table | Copy sink write behavior: upsert on key columns |
Many tables: make it metadata-driven
Put one row per table in the watermark table (with its watermark column), Lookup all of them, and run this pipeline inside a ForEach with tableName as the parameter. That's why every name in the pipeline comes from pipeline().parameters.tableName.
๐ฏ Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1How do you implement an incremental load in ADF?Fresher+
Q2Why use two Lookups instead of copying 'everything after the old watermark'?1โ3 yrs+
Q3How do you handle deletes in incremental loads?1โ3 yrs+
Q4The source's updated_at isn't reliable (some updates don't touch it). What now?3+ yrs+
Key takeaways
- โCopy only what changed: faster, cheaper, gentler on the source.
- โFix the window first, move the marker last.
- โDesign for failure: reruns must be harmless.
- โKnow the alternatives: file dates, change tracking, CDC, upsert.
Frequently asked questions
What is an incremental load?
Copying only the rows that are new or changed since the last run, instead of the whole table every time. It's faster, cheaper and kinder to the source database.
What is a watermark?
A saved marker of how far you've loaded, usually the latest updated_at (or the highest ID) copied last time. The next run copies rows above the old watermark and up to a new one, then saves the new one.
Why read the new watermark before copying?
So the copy has a fixed upper bound. Rows that change while the copy runs are picked up next time instead of being half-included or skipped. Then you save exactly the value you copied up to.
What if rows are deleted in the source?
A watermark on updated_at can't see deletes. Use soft deletes (an is_deleted flag with updated_at), SQL change tracking, CDC, or a periodic full comparison.
Can ADF do this without a watermark table?
Yes: for files, filter by last modified time; for SQL sources with change tracking or CDC enabled, ADF can read changes; and ADF's native change data capture resource (top-level CDC) handles some sources with little setup.
๐ง Quick check: incremental loads
1. Order of steps in the watermark pattern?
2. The Copy fails. What should happen to the watermark?
3. Which condition avoids double-loading the boundary row?
4. Which changes does an updated_at watermark miss?
Checking your accessโฆ