Basis Lab

Moving Data Without Losing Any

Two Writes, and the Order Between Them Is the Whole Design

Last timeMaking a Step Safe to Repeat

A restart that resumes needs a recorded position. Save it before the work and you lose records; save it after and you repeat them. There is one way out.

What a position actually is

The last lesson made a step safe to repeat, which makes a restart harmless. It does not make a restart cheap. A worker that comes back and reprocesses six hours of history is correct and useless, and the cost is not only time: it is load on the source, on the destination, and on whatever else shares them.

So the worker needs to record where it got to. The record exists to answer exactly one question, and keeping that question in view settles most of the design decisions that follow: if this process died right now and came back, where should it start?

A position therefore has to name a point in the data such that everything before it is finished and everything after it is not. That phrasing rules out two of the three obvious candidates.

An identifier will not do, because identifiers are not ordered. Knowing that record 8841 is done says nothing about 8840 unless the identifiers were handed out in processing order, which is a property almost nothing guarantees.

A timestamp will not quite do either, and this is the one that catches people, because a timestamp looks ordered. Two records can carry the same timestamp. A worker that records the last timestamp it saw and resumes strictly after it loses every record that shared that moment, and a worker that resumes at it repeats them. Second resolution makes this common, millisecond resolution makes it rare and not absent, and at volume rare means daily.

FIG 1The cost of coming back with no position, worked at a thousand records a second
stepwhat goes into the countrestarting with no positionrestarting from a recorded positionwhat happened
1records arriving at the source1,000 a second1,000 a secondA property of the business rather than of the restart, so it is the same in both columns and there is nothing here to decide.
2how far back each worker has to startsix hours, which is 21,600 secondsone batch, which is 200 recordsThe only row the position changes. Six hours is what a worker with no record of its progress has to assume, because assuming less means losing whatever it skips.
3workers restarting at the same moment44A deploy or a node replacement takes them all together, which is why the count is multiplied by four rather than by one.
4records reprocessed1,000 times 21,600 times 4, so 86,400,000200 times 4, so 800Eighty-six million against eight hundred, out of one row of difference. Both are the same correct work, done twice.
5reads at the source and writes at the destination86,400,000 of each800 of eachIf the previous lesson was done, not one of these corrupts anything. They are load, arriving all at once, on the two systems least able to absorb it.
5 steps
Only the second row differs between the columns and it is the row a recorded position decides; every other row is the same pipeline. Eighty-six million correct reads and eighty-six million correct writes, landing together on the source and the destination, is why a position is worth building rather than merely tidy. The work is correct and it is still an outage.

What does work is a sequence number from the source, or a timestamp paired with a tie-breaker, or a composite of the two. The shape matters less than the property: for any point, the data falls cleanly into done and not done, with nothing ambiguous on the boundary.

Before or after, both wrong

Now the part that is genuinely a design problem rather than a detail. There are two things to write: the result of the work, and the new position. They are two separate writes, and a process can die between any two writes.

The lesson stops here

2 more paragraphs to go

You have read the opening. The rest of the argument, the problems that check whether it landed, and the lines worth keeping at the end all come with a plan.

The first lesson of every course in the library reads the whole way through, free, so you can see exactly what the rest of them are.

See the planThe contents

This is the reading half

Starting the course gives you your own copy of it. Every idea on every page has problems standing under it, marked with a reason rather than a tick, and any sentence you do not believe can be opened and argued with. None of that can happen on a page nobody owns.

The contents

The rest of this course

  1. 01Nothing Crashed, Nothing Alerted, and the Total Is Short by Nine Hundred
  2. 02The Sender Cannot Tell Which Half of the Journey Failedopening only
  3. 03Run It Twice on Purpose and See What Breaksopening only
  4. 04Two Writes, and the Order Between Them Is the Whole Designyou are here
  5. 05The Row Was Saved at 10:04 and Became Visible at 10:06opening only
  6. 06Start the Stream Before You Read the Old Rowsopening only
  7. 07Tuesday's Number Arrived on Friday and Tuesday Is Already Publishedopening only
  8. 08The Check Takes Four Minutes a Day and Almost Nobody Runs Itopening only

Read alongside