Incremental SQL table synchronisation using watermarks
How to build reliable incremental SQL Server table syncs with watermarks: choosing the column, why rowversion beats dates, avoiding missed rows, handling deletes and failures.
Copying a whole table every time you sync it is simple, but it doesn't scale. When a table has hundreds of millions of rows and only a few thousand change each hour, you want to copy just those. The usual way to do that is a watermark: a value that records how far the last sync got, so the next one can pick up from there.
Watermarks are easy to get almost right. This guide covers how to choose the watermark column, the classic ways rows get missed and how to avoid them, how to handle deletes, and how to make a sync safe to rerun after a failure.
How a watermark works
The idea is simple. The table has a column that increases whenever a row is inserted or changed. Each sync run:
1. Reads the last watermark it recorded.
2. Copies every row whose watermark column is greater than that value.
3. Records the highest value it copied, ready for next time.
Everything depends on choosing a column that really does increase with every change, and on reading it in a way that can't miss rows.
Choosing the watermark column
The two common candidates are a modified-date column and a `rowversion` column.
**A modified date**, such as `ModifiedAt`, is easy to understand, but it has several weaknesses:
- It is only updated if every code path that changes the row remembers to set it. One forgotten update statement and changes are silently missed.
- Clock changes and time zones can make values jump backwards.
- Several rows can share the same timestamp, which needs careful handling at the boundary.
**A `rowversion` column** is maintained by SQL Server itself. Every insert and update gives the row a new value that is unique and increasing within the database, whatever code made the change. You can't forget to set it, and clocks don't matter. For a new sync, it is almost always the better choice:
```sql
ALTER TABLE dbo.Orders ADD RowVer rowversion;
CREATE INDEX IX_Orders_RowVer ON dbo.Orders (RowVer);
```
The index matters: without it, every sync run scans the whole table looking for changed rows.
Don't miss rows still being written
There is a subtle trap with any watermark. A row can be given its watermark value at the start of a long transaction, but not be visible until that transaction commits. If a sync runs in between, it may record a watermark higher than that row's value, and the row will never be picked up.
With `rowversion`, SQL Server provides a way round this. `MIN_ACTIVE_ROWVERSION()` returns the lowest rowversion still in use by an open transaction. Copy only rows below that, and nothing in flight can be skipped:
```sql
DECLARE @from binary(8) = (SELECT LastRowVer FROM sync.Watermarks WHERE TableName = N'dbo.Orders');
DECLARE @to binary(8) = MIN_ACTIVE_ROWVERSION();
SELECT OrderId, CustomerId, Status, Amount, RowVer
FROM dbo.Orders
WHERE RowVer > @from AND RowVer < @to;
```
Capture the upper bound first, as here, then use it both for the copy and as the new watermark. With a date column, the usual safeguard is an overlap window: each run goes back a few minutes before the last watermark, and the upsert logic copes with seeing some rows twice.
Apply changes as an upsert
Because a watermark picks up inserted and updated rows together, apply them as an upsert: update rows that already exist on the target and insert the ones that don't, matched by primary key. Load the changed rows into a staging table first, then apply them, so the source is only read once per run. Our guide to [SQL table synchronisation](/guides/sql-table-synchronisation) covers the insert, update and delete statements in detail.
Save the watermark only when the copy succeeds
Store the new watermark in the same transaction that applies the changes to the target, or only after that transaction commits. If the run fails halfway, the watermark stays where it was, and the next run simply copies the same range again. Because the changes are applied as an upsert, reprocessing the same rows is harmless. That makes the whole sync safe to rerun, which is exactly what you want when something goes wrong at 3am.
Handle deletes separately
A watermark only sees rows that exist. A deleted row has no watermark value, so it is never picked up, and the target keeps it forever. There are a few ways to handle deletes:
- **Soft deletes.** The application marks rows as deleted rather than removing them, so the change shows up through the watermark like any other update.
- **A deletes log.** A trigger on the source writes the keys of deleted rows to a separate table, which the sync reads and applies.
- **Change Tracking.** SQL Server's Change Tracking records inserts, updates and deletes by key, and can replace or complement a watermark.
- **Periodic reconciliation.** A regular comparison of keys between source and target removes any rows that no longer exist on the source.
Monitor the sync
An incremental sync can fail quietly, for example by copying nothing because the watermark is wrong. Record each run's start and end time, the range it covered and how many rows it copied. Alert if a run fails, if no run has succeeded for longer than expected, or if row counts look unusual. A periodic full comparison of row counts between source and target catches any drift the watermark has missed.
Troubleshooting Scenarios
Some updates never reach the target
The modified-date column isn't updated by every code path. Switch to rowversion, which SQL Server maintains itself.
Rows occasionally go missing under heavy load
They were in uncommitted transactions when the sync ran. Use MIN_ACTIVE_ROWVERSION as the upper bound, or an overlap window for date watermarks.
Each sync run scans the whole table
The watermark column isn't indexed. Add an index on it.
Deleted rows stay on the target
Watermarks can't see deletes. Add soft deletes, a deletes log, Change Tracking or periodic reconciliation.
FAQ
What is a watermark in data synchronisation?
It is a recorded value, such as a rowversion or timestamp, marking how far the last sync got, so the next run copies only newer changes.
Should I use a datetime or rowversion column as a watermark?
Rowversion where possible. SQL Server updates it on every change, so it can't be forgotten, and it isn't affected by clocks.
What does MIN_ACTIVE_ROWVERSION do?
It returns the lowest rowversion still in use by an open transaction, so a sync can avoid skipping rows that haven't committed yet.
How do I sync deletes with a watermark?
You need something extra, such as soft deletes, a deletes log, Change Tracking or regular reconciliation by key.
How do I make a watermark sync safe to rerun?
Apply changes as an upsert and save the new watermark only when the copy has committed.