# Pipelines that break: four ways to sync a live public feed, scored against a rebuildable truth

When a source revises rows, deletes them and changes their ids, which way of syncing it into a warehouse keeps the
copy right, and what does each way cost in data read? This study records a live public feed (the USGS earthquake
catalogue) every 240 seconds, replays every recorded read through four sync designs run by a real orchestrator, and
scores each design's table against the recorded truth after every read. A second arm plants a one-hour outage (it
skips an hour of the recorded reads) and the backfill after it. The read failures are planted too, at a seeded rate.
A third part loads one event type from a public archive whose schema changed over the years,
three ways. The page is `index.html`; `results.html`, `outage.html` and `method.html` hold the detail.

## Data

- USGS earthquake catalogue, FDSN event service, GeoJSON, `https://earthquake.usgs.gov/fdsnws/event/1/query`, with
  `includedeleted=true` so deletions are visible. Every read of the window, events with an origin time in the
  31 days before the recording, is stored in
  `data/source-log.jsonl.gz` (each read is stored as the change from the read before, and `tools/pack_log.py`
  checks that every read rebuilds exactly). A second recording of the same window, added after the results and
  declared in `CHANGES-r4.md`, is in `data/source-log-r4.jsonl.gz`. Credit: U.S. Geological Survey. USGS-authored
  data are in the public domain.
- GH Archive (`https://www.gharchive.org/`), push events from five hours between 2012 and 2025, 2,000 events per hour,
  reduced to each event's field names, value types and counts (no names, ids or text), in `data/gharchive/`. `tools/gharchive_sample.py` rebuilds a sample from the
  archive's hourly files.

## The four designs

- Full reload: read the whole window every time and replace the table.
- Time partitions: read only the slice of origin time that has newly closed, and overwrite that slice.
- Incremental by updated time: read rows updated since the last high-water mark, upsert by id, skip deletions.
- Change feed: the same read with deletions included and a 30-minute lookback, apply deletions, and fold an event's
  old ids into its current one.

The orchestrator is Dagster (`dag/definitions.py`): one asset per design, partitioned by read, with a retry policy
for transient faults only, asset checks (one row per id, row contract, no deleted rows, no old ids, count against the
source, freshness), a sensor that launches each read, and a backfill that re-runs skipped reads. It stands in for a
production scheduler such as Airflow, Dagster or Prefect on a managed service. The warehouse is DuckDB, one file per
design.

## Reproduce

Python 3.12. `pip install -r requirements.txt`.

```
python3 -B tests/test_pipeline.py
python3 -B tools/run_study.py data/source-log.jsonl.gz /path/to/new/out /path/to/new/work
python3 -I -B tools/drift.py data/gharchive results/drift.json
python3 -I -B tools/summarize.py results results/summary.json
python3 -B tools/trailing_reread.py data/source-log.jsonl.gz results/summary.json results /path/to/new/work
python3 -B tools/trailing_windows.py data/source-log.jsonl.gz results/summary.json results/trailing_reread.json results /path/to/new/work
python3 -B tools/second_recording.py data/source-log.jsonl.gz results/summary.json data/source-log-r4.jsonl.gz results /path/to/new/work
python3 -I -B site/render.py
```

`run_study.py` writes `ticks.jsonl` and `source.jsonl` to the out folder; gzip them into `results/` before
`summarize.py`. The planted transient faults are seeded, so a replay reproduces exactly. Set `PIPELINE_FAULTS=off`
to replay without them.

To record your own feed: `python3 -I -B tools/record_source.py /path/to/new/dir <stop at, UTC ISO> 240`, then
`python3 -I -B tools/pack_log.py /path/to/new/dir data/source-log.jsonl.gz`. To run the pipeline live against the
service: `DAGSTER_HOME=/path/to/new/home PIPELINE_DATA=/path/to/new/data dagster dev -f dag/definitions.py`, then
`DAGSTER_HOME=/path/to/new/home python3 -B tools/live_runs.py /path/to/new/data results/live_runs.json`.

## Files

- `tools/source.py`: reads the service and serves a recorded read under the service's own query rules.
- `tools/contract.py`: the row contract. Rows that break it go to a quarantine table, never into the main table.
- `tools/designs.py`: the four designs' extract and load steps.
- `tools/score.py`: compares a table with the truth for one read and names each wrong row's error.
- `tools/replay.py`, `tools/run_study.py`: the replay, the planted faults and the outage arm.
- `tools/drift.py`: the strict, lenient and versioned loaders on the archive sample.
- `tools/live_check.py`: re-reads the service to check that the replay's rows are still there and still match.
- `tools/summarize.py`: writes `results/summary.json`, including the predictions scored as declared.
- `tools/trailing_reread.py`: the design added after the results, a slice load that re-reads its last day, with
  plain time partitions and the change feed re-run as controls. Writes `results/trailing_reread.json`.
- `tools/trailing_windows.py`: the 3-day and 7-day re-reads added after the results, with the 1-day re-read, plain
  time partitions and the change feed re-run as controls. Writes `results/trailing_windows.json`.
- `tools/second_recording.py`: the four designs over the second recording, added after the results, with the first
  recording re-run as the control. Writes `results/second_recording.json` only when the control matches.
- `DESIGN-r1.md`, `protocol-r1.json`, `predictions-r1.json`: the design and the seven predictions as declared.
  `CHANGES-r1.md`: the two declared changes. `CORRECTIONS.md`: one claim in `CHANGES-r1.md` corrected, and what the
  service's history query returns. `CHANGES-r2.md` and `CHANGES-r3.md`: the re-reads added after the results, each declared before it
  ran. `CHANGES-r4.md`: the second recording, declared before it started.
- `governance.json`: the owner of each table and check, what the pipeline does when a check fails, and what the
  owner intends once it is built.
- `assumptions.json`: the prices and effort behind the page's "What it is worth" section, each with a range.
- `site/render.py`, `site/page.css`: the pages.
- `vendor/viz-design-system/`: the chart kit (version 2.3.0) with its licences.

## Continuous integration

`.github/workflows/ci.yml` runs the tests on every push, including a replay of the first reads of the recorded log.
Its first run in this code's own repository passed. `python3 -B tests/test_pipeline.py` runs the same tests locally.

Some files are packed in pipelines-that-break-data-1.zip; unzip it here to restore the full export, which the manifest lists file by file.
