Your first load

Part of the free LogicWiz Data Engineering course, Module 1: The job and the map. The SQL and Python run in the app, on real Postgres and real Python in the browser.

It is 9:10 on your first Monday as the data engineer at SwiftKart, a global online store. Tess from finance opens her dashboard: it says the store took 68 orders in the first week of June. The store's own database says 40. No job failed overnight. Nora, your lead, hands you the case with one piece of advice: "Count both sides before you touch anything."

You'll learn to

  • Load a file and count it against its source
  • Tell a true duplicate from a real change
  • Turn complete, correct and on time into numbers with targets
  • Say what a data contract is and who checks it

Learn

Your first case at SwiftKart

SwiftKart sells earbuds, tote bags and running shoes to customers around the world. Every order is written into the store's database the moment a customer pays.

Three people turn up in almost every lesson. Nora is the staff data engineer and your lead. Tess is the finance analyst: when data is wrong, her dashboard shows it first. Dana is the backend engineer who owns the store's app and its Postgres database.

Your job sits between Dana and Tess. A data engineer moves data from where it is created to where it is used, and makes sure it arrives whole.

From Dana's database to Tess's dashboard

Four steps from left to right: the store database owned by Dana, the daily files it exports, your load that reads them into a table, and the dashboard Tess reads.

Read it left to right. Everything between the first box and the last one is yours to get right.

Count both sides

Start with the easiest check there is. Dana's team gives you one full export of the orders table, data/orders.csv. Load it and count the rows. Then ask the store's database the same question.

You load the file with DuckDB, a small analytical database that runs inside Python and reads a CSV file straight into a table. The store's database is real Postgres. Run both cards.

Your side: load the export and count it

import duckdb

duckdb.sql("CREATE TABLE orders AS SELECT * FROM 'data/orders.csv'")
rows = duckdb.sql("SELECT COUNT(*) FROM orders").fetchone()[0]
print("rows loaded:", rows)

One line makes a table from the file, the next one counts it. You should see rows loaded: 80.

Dana's side: count the source

SELECT COUNT(*) AS orders_in_source
FROM orders;

This runs on the store's own Postgres database. 80 on both sides: no order was lost on the way. Comparing a load with its source like this is called reconciling.

Seven days, eight files

So where does 68 come from? Tess's dashboard does not read the full export. It reads a table built from the daily files.

Each morning the store drops one file into landing/orders/. It lists the orders that were created or changed that day. The first week of June should be seven files. There are eight. On 3 June the upload broke halfway, so the system sent the same file again as 2025-06-03_retry.csv.

The load reads every file in the folder into one table.

Load every daily file, then count

import duckdb

duckdb.sql("CREATE TABLE landed AS SELECT * FROM read_csv_auto('landing/orders/*.csv')")
total, distinct_ids = duckdb.sql(
    "SELECT COUNT(*), COUNT(DISTINCT order_id) FROM landed"
).fetchone()
print("rows:", total)
print("distinct orders:", distinct_ids)
print("repeats:", total - distinct_ids)

read_csv_auto with a * reads every matching file as one table. 68 rows, but only 40 different order ids (SQL says distinct): 28 rows are repeats. Tess's dashboard counted rows. To see where the repeats come from, follow one order, 1008, through the folder.

Order 1008 lands three times

Order 1008 is listed in the 2 June file as placed, in the 3 June file as shipped, and in the retry file as shipped again, so the loaded table holds three rows for one order.

  1. The 2 June file. Order 1008 was placed that day, so the file lists it once. One row lands in the table.

  2. The 3 June file. The order shipped, so the store lists it again with its new status. This second row is a real change, not a mistake.

  3. The retry file. The same file arrives a second time, and the load reads it like any other. The third row is a pure copy of the second.

  4. What should be left. Throw the copy away. Of the two real versions, keep the latest. One order, one row, status shipped.

The left box is the folder, the right box is the table the dashboard counts. Watch the right box grow.

Remember: rows are not orders. After every load, count the rows, count the distinct keys, and compare both with the source.

Nothing crashed. The number was wrong.

Now the 28 repeats add up. The retry file holds 7 rows, all pure copies. The other 21 are older versions of orders that changed later, like order 1008 before it shipped. 40 + 21 + 7 = 68.

Look at what did not happen. No error. No failed job. No alert. The load did what it was told, and the dashboard was off by 28.

This is how pipelines usually break: quietly. A file arrives twice. A filter drops rows. A join multiplies them. A column gets renamed.

Where the 68 rows came from

One bar of 68 rows split into 40 latest rows, one per order, 21 older versions of orders that changed, and 7 copies from the retry file.

Read the bar left to right: only the green part is orders. The dashboard counted the whole bar.

"It ran without errors" is not "it worked". Equal row counts do not prove it either: one lost row and one doubled row cancel out. Reconcile distinct keys and totals as well as rows, and put the alert on those numbers, not on whether the job finished.

Three promises, written as numbers

Nora puts the job in one sentence: you are paid for three promises. The data is complete: every row from the source arrives. It is correct: each row arrives once, with the right values. It is on time: ready when people need it.

A promise nobody measures is a hope. So each one becomes a number you compute on every run, called an SLI (service level indicator). Its target is an SLO (service level objective), such as "ready by 06:00 UTC on 97% of days in a quarter".

Tess's 68 broke the second promise. The SLI that catches it: rows minus distinct order ids, target 0.

SwiftKart's first SLOs

PromiseThe number (SLI), every runThe target (SLO)Alert when
Completeloaded rows / source rows, per day100% for orders, 99.9% for app eventsbelow target after the 06:00 run
Correctduplicate orderids; captured revenue against the source0 duplicates; totals equal to the centany duplicate, any difference
On timethe time dailyrevenue is ready against 06:00 UTC, or the age of its newest rowready by 06:00 on 97% of days per quarternot ready at 06:00, or half the budget spent

Each SLI is a query you can schedule. App events get a looser target than orders because phones go offline and some events never arrive: the target follows what the reader needs and what the source can deliver. The on-time promise has its own name, freshness: how old the data is when someone reads it.

On time means ready for Tess

Take the third promise. Finance reads daily_revenue first thing, so the SLO says it must be ready by 06:00 UTC.

On a normal night the store's export starts at 02:00. The load and the transform follow, and the checks pass at 04:45. That leaves 75 minutes to spare.

Now suppose the export finishes 90 minutes late.

A normal night and a late night

Eight lanes in minutes after midnight UTC: on a normal night export, load, transform and checks finish at 04:45, 75 minutes before the 06:00 deadline; on a late night the export finishes 90 minutes late and the table is ready at 06:15.

Read each lane left to right, in minutes after midnight UTC. The top four lanes are the normal night, the bottom four the late one. The clock stops when the checks pass, not when your own step ends: freshness is measured where the reader stands.

How many late mornings are allowed

The SLO says 97% of days, not all of them. The part it lets you miss has a name: the error budget.

Work it out for one quarter of 90 days:

  • Allowed to miss: 100% − 97% = 3%
  • 3% of 90 days = 2.7 days

So two late mornings are inside the promise. A third breaks it.

The budget settles arguments. While some is left, the team ships changes. When it is spent, risky changes wait and reliability work comes first. And why not promise 100%? Nobody can keep it, and a budget of zero leaves no room to change anything.

Late days allowed in a 90-day quarter

Bars show how the error budget shrinks as the SLO rises: 4.5 days at 95%, 2.7 at 97%, 0.9 at 99% and 0.09 at 99.9%.

Each bar is 90 × (1 − SLO). Every extra nine cuts the budget by ten, and keeping it costs more each time: earlier exports, spare capacity, faster on-call. Pick the target from what the reader needs.

Remember: error budget = (1 − SLO) × the window. 97% of a 90-day quarter leaves 2.7 days. Watch how fast it burns: two late days in the first week is a warning long before the SLO is broken.

Dana's half of the deal

Your promises depend on a system you do not own. Say Dana renames amount to amount_usd on a Friday. Your row counts stay the same. The table is on time. Monday's revenue is wrong.

The fix is a written, versioned agreement between the team that produces the data and the people who read it: a data contract. It says four things: the columns and their types, what one row means (units, currency, time zone), when the data lands, and how a change gets announced.

A contract only counts when a machine checks it. In dbt, the SQL build tool of Module 8, a model marked contract: {enforced: true} fails the build when a column's name or type differs from the declared one.

A contract and a freshness check, as dbt configuration

Runs with dbt:

models:
  - name: fct_orders
    config:
      contract:
        enforced: true
    columns:
      - name: order_id
        data_type: bigint
        constraints:
          - type: not_null
      - name: customer_id
        data_type: bigint
      - name: order_ts
        data_type: timestamp
      - name: amount_usd
        data_type: numeric(12, 2)

sources:
  - name: shop
    config:
      loaded_at_field: updated_at
      freshness:
        warn_after: {count: 6, period: hour}
        error_after: {count: 12, period: hour}
    tables:
      - name: orders

The top half is the contract: four columns with their types, so dbt build fails before a wrong shape reaches readers. The bottom half is the on-time check: dbt source freshness warns when the newest updated_at is over 6 hours old and fails past 12.

Numbers to remember

WhatNumberGood to know
Error budget, 97% over a 90-day quarter2.7 daystwo late mornings fit, a third breaks the SLO
Error budget, 99% over a 90-day quarter0.9 daysnot one full late morning
Hourly feed, 99.5% over 30 days720 hours × 0.5% = 3.6 hoursthe same formula on another window
Freshness, as Google's Site Reliability Workbook writes it"X% of data processed in Y"it calls freshness the main SLO of a pipeline
dbt freshness under config:since dbt v1.9loadedatfield moved there in v1.10
A contract written as YAMLOpen Data Contract Standard (ODCS)a Linux Foundation project

Other tools check the same promises after every load: dbt tests, Great Expectations, Soda, and Databricks pipeline expectations (warn, drop the row, or fail the update). Schema registries do a contract's job for event streams, and dbt also reports a removed column or a changed type against the previous state.

What changed at SwiftKart

By lunch you can tell Tess what happened. The store took 40 orders that week. Her dashboard counted 68 rows: 7 copies from a file that arrived twice, and 21 older versions of orders that changed later.

You drop the retry file, keep each order's latest row, and add one check after every load: rows minus distinct order ids must be 0. Nora writes the 06:00 target on the team page, next to its 2.7 days of budget. Dana agrees to a contract on the orders export.

One question is still open. Where do those files come from, and where does the data go after your table? The next lesson follows one order the whole way.

In one line

Count every load against its source (rows, distinct keys, totals), turn complete, correct and on time into numbers with targets, and know your error budget: (1 - SLO) times the window.

Interview corner

How do you know your pipeline is correct?

I define SLIs and check them on every run: completeness as loaded rows over source rows for the same window, correctness as zero duplicate keys and totals that reconcile to the cent, and freshness as the time the table is ready against its deadline. Each has an SLO, for example ready by 06:00 UTC on 97% of days per quarter, and an alert. Checks run after every load and fail the run or quarantine rows, so bad data is caught by the pipeline, not by finance.

Define the SLOs for a daily revenue table, and its error budget.

Complete: 100% of captured payments for the day, reconciled against the payment source. Correct: zero duplicate order ids and revenue equal to the source total. On time: ready by 06:00 UTC on 97% of days per quarter, measured when checks pass. The budget is (1 - 0.97) x 90 = 2.7 days, so two late mornings are allowed; I alert when half is spent, and when it is gone risky changes wait until reliability is fixed. I would not promise 99% here, because a 0.9-day budget means a single late export breaks it.

What is a data contract, and how do you enforce one?

It is a versioned agreement between a data producer and its consumers: the schema with types and keys, the meaning of a row and its units, the delivery SLOs, and who owns changes and how they are announced. It matters only when checked by a machine: a dbt model contract fails the build when names or types drift, a schema registry rejects incompatible event schemas, and CI on the producer's repository runs the consumers' expectations. Breaking changes get a new version and a deprecation window instead of a silent rename.

Why not set every SLO at 100%?

Because no pipeline that depends on other teams' systems can promise never to be late or incomplete, and a 100% target leaves an error budget of zero: every incident is a breach, and every change becomes a risk nobody may take. The target should come from what the consumer needs and what the dependencies can deliver. Each extra nine costs a lot more (earlier exports, redundancy, faster on-call), so I price it before I promise it.