Delta loading with a modified date you do not fully trust
On Monday morning the wholesaler's sales department calls. Van Dalen B.V. ordered through the web shop on Saturday at 13:40. The order is right there in the accounting package. The report does not show it. Every load over the past nights finished without an error.
The cause is the modified date. With a delta load, Yres asks the source only for the rows changed since the watermark. That works as long as the date is right. In practice it is often slightly wrong, because rows arrive later than their date says.
Yres therefore lets you set a look-back window per table. Below you follow data engineer Sanne through the order lines: why the order went missing, what the window changes and where it stops.
Published: · Last updated: · By Daniel Sanders, architect of Yres and founder of Plainwater
Why is a modified date not always right?
A modified date records when a row got its time. If the row only reaches the source later, its date already lies behind the point where the last load stopped.
Sanne loads the order lines from the accounting package with the load mode changes only, familiar from our article on the seven load modes. That is 40 million rows, each with a modified date. Every night at 02:00 Yres asks the source for everything changed since the watermark.
That watermark is the highest modified date Yres has seen from this table so far. Yres reads it from the source itself, so it is independent of the data warehouse clock. After the load of Sunday 02:00 it stands at Saturday 23:51. That is when a warehouse worker edited the last line that evening.
Here is what happened to Van Dalen. Every night at 03:10 the web shop puts its orders into the accounting package with a batch import. That import takes over the web shop's timestamp. So Van Dalen's order lines got Saturday 13:40 as their modified date. They only entered the package on Sunday at 03:10, well after the 02:00 load had finished.
An import with its own time is one of five ways a modified date lies:
- An import. Another system delivers rows with its own timestamp, like the web shop with Van Dalen.
- Two clocks. Two systems each have their own clock. A few minutes of difference between them is perfectly normal.
- Stamped before saving. A row is opened and stamped, but only saved a quarter of an hour later.
- A correction script. An administrator edits fields directly in the database. The modified date does not move along.
- Only a creation date. A new row gets a creation date. The modified date field stays empty until the first edit.
In each of those cases a row exists that belongs to a load that has already passed.
What goes wrong with a hard question
Follow Van Dalen's lines through the nights:
- Sunday 02:00. The load runs. The lines do not exist in the package yet.
- Sunday 03:10. The import adds the lines to the package, dated Saturday 13:40.
- Monday 02:00. Yres asks the source for everything from Saturday 23:51 onwards, inclusive. Saturday 13:40 falls outside that, so the lines do not come along.
- Tuesday 02:00. The watermark now stands at Sunday evening. The lines lie even further from the question and never come along again.
The problem is that this happens silently. The monitoring screen shows a successful load with that night's row count. No error or warning appears anywhere, because the source simply answered the question it was asked.
Only when someone puts the revenue from the report next to the revenue from the package does the difference show. That can take weeks. By then every day of those weeks is missing a handful of web shop orders.
Recovery is harder than it sounds. A full reload of 40 million rows takes hours. Rolling back to an earlier moment, as described in our article on rollback, does move the watermark back with it. But it repairs only this one period. The next night it happens again, because the problem sits in the question to the source.
What does a look-back window do?
A look-back window makes the question to the source start earlier: at the watermark minus a period you set per table. Rows dated just behind the watermark then come along after all.
You set the window as a number with a unit, in minutes, hours, days, months or years. Sanne sets one day on the order lines. The question stays 'from and including', just as without a window.
On Monday night it then goes like this:
- The question. The watermark stands at Saturday 23:51. Yres asks the source for everything from Friday 23:51.
- The missed lines. Van Dalen's lines are dated Saturday 13:40. They fall inside the window and come in as new rows.
- The lines already in. All of Saturday's other lines come along again too.
- The comparison. The engine compares every incoming row with the version already there. With the same key and the same content no new version is added. Only lines that really changed get one.
- The watermark. After a successful load the watermark moves to the highest modified date now in the history table.
That comparison uses the two fingerprints from our article on keeping history at hundreds of tables. Reading back therefore costs only the traffic from the source to the landing zone plus the comparison. It costs no storage and creates no duplicate history.
If the load fails halfway, the watermark stays put and the monitoring screen reports it. The next night Yres asks for the same period again, with the window included.
Two details are worth knowing:
- A date without a time. If the modified column holds only a date, the question counts in whole days. A window of six hours then amounts to one day back.
- An empty history table. On the first load there is no watermark yet. Yres then fetches everything and the window plays no part.
| Situation | Hard question from the watermark | With a window of one day |
|---|---|---|
| Web shop order of Saturday 13:40, imported Sunday 03:10 | Never comes along again | Comes in on Monday night after all, as a new row |
| Line opened at 01:55, saved at 02:12 while the load is running | The date is before the watermark, so the line is missed | Falls inside the window and comes in the next night |
| Two application servers with clocks minutes apart | Lines from those minutes are missed | Well within the window |
| Saturday's lines that were already loaded | Do not come along again | Come along again, with the same fingerprint and therefore no new version |
| Field changed without the modified date moving | Missed | Missed as well, because the window does not help here |
| Load fails halfway | The watermark stays put and the period is asked for again the next night | The same, with the window included |
| Cost per night | Only the changes | The changes plus one day of traffic and comparison |
Two modified columns and the delta snapshot
Later in the year Sanne also connects the web shop's orders directly, through the API. That table has two dates. The creation date is filled for every order. The modified date stays empty until someone edits the order.
With only the modified date as the watermark column she misses every new order that has never been edited. With only the creation date she misses every change.
Yres therefore lets you specify two watermark columns per table, of the same data type. Per row the higher of the two counts. If one of them is empty, the other counts. A new order thus comes along on its creation date and a changed order on its modified date. The watermark then moves to the highest value seen across both columns.
The window simply works on top. You can specify two columns for all database sources and for a number of API sources, including AFAS and Salesforce.
If a source has only a creation date, it is not a delta source for rows that can change. New rows come in, changes never do. For a log of events that never change that is fine. For orders the full snapshot fits better, with the fingerprint as the comparison.
The window plays a second role. With the delta snapshot, another of the seven load modes, the engine closes rows that are no longer in the loaded data. It does that only for rows whose modified date falls inside the fetched window. So the window also decides how far back a deletion is seen.
A timesheet line from a week ago that is deleted today falls outside a window of one day. With a window of a week the engine does catch it.
When do you use it?
The look-back window belongs on every table that fetches only changes from a source where rows can arrive later than their date says. Three cases keep coming back in practice.
A package fed by an import. The wholesaler's accounting package receives orders from the web shop every night, with the web shop's time. The window has to bridge at least the gap between the moment an order gets its time and the moment it is in the package. Sanne chooses a day. If the import skips a night, that is too little and two days do cover it.
A source with several clocks. The accounting package runs on two application servers whose clocks are a few minutes apart. A line that got 01:58 on one server can be saved later than a line that got 02:01 on the other. The 02:00 load sees the second line and moves the watermark to 02:01. The first is saved only after that and without a window never comes along. A window of an hour is enough here.
A source with a date without a time. Some packages keep only a calendar day as the modified date per row. Without a window Yres already asks for the whole day of the watermark. A window of one day on top catches rows that are sent after the fact with yesterday's date.
When it works
- A table that fetches only changes from a source with imports, several clocks or rows that arrive after their date
- The delta snapshot, where the window also decides how far back deletions are seen
Where it stops
- A field that changes without the modified date moving stays out of reach
- Anything further back than the window stays out of sight. An import that skips a night already needs two days
- With file sources the window works differently. There the file's modified time counts, measured from the moment of loading
- Not every API source knows the window. Exact Online works with its own synchronisation number per change and does not read back
When there is little point
- For a table small enough to fetch in full every night. There is no watermark then and the fingerprint does the work
- For a source where deletions from long ago have to become visible. No window is large enough, so you choose the full snapshot
Frequently asked questions
What if the same line comes in twice within one load?
That happens, for instance, when a source returns the same row in two batches. The version with the highest modified date wins and the other is closed immediately. So even within a single load no duplicate current versions arise.
Why not simply reload everything every night?
For small tables that is the best choice. The full snapshot fetches everything and closes deleted rows. With 40 million order lines that does not work, because fetching everything takes hours. An API with a request limit often does not even allow it. The window is then the price of fetching only changes safely: one extra day of traffic instead of 40 million rows.
How large should the window be?
As large as the biggest delay you know of, with a margin. A nightly import that runs hours after the load needs a day. Clock differences of a few minutes need an hour. If in doubt, take a day. A window that is too large only costs traffic. A window that is too small costs rows you never see again. With the delta snapshot you choose the window to match the period in which the source still applies corrections.
What if I set the window only after orders have already been missed?
The window works from the next load onwards and reads back from the watermark. Anything further back does not come in by itself. For that period you roll the table back to a moment before the first missed order. The watermark moves back with it and the next load fetches the period again, now with the window included.
Talk to a data architect
In 30 minutes a data architect looks at your current data environment with you and at where it pinches today.
Book a call →
