Data Contracts and CI Enforcement
A data contract is a versioned file, owned by the team that produces a dataset, stating what every consumer may rely on. It has 3 parts. The schema lists each field with its type and whether it may be null. The guarantees state rules the values obey and delivery promises, such as how soon an event lands after the order completes. The evolution policy says which changes the producer may ship freely, which count as breaking, and how much notice consumers get first. The file sits in the producer's repository beside the code that emits the data, so a change to either is reviewed together.
A contract for one event
contract: checkout.order_completed
version: 1.2.0
owner: checkout-platform
schema:
- name: order_id
type: string
required: true
unique: true
- name: customer_id
type: string
required: true
- name: amount_cents
type: bigint
required: true
min: 1
- name: currency
type: string
required: true
allowed: [USD, EUR, GBP]
- name: completed_at
type: timestamp
required: true
- name: coupon_code
type: string
required: false
guarantees:
freshness_minutes: 60
daily_rows_min: 400000
amount_cents_meaning: total charged after discounts, before tax, in minor currency units
evolution:
additive: [add optional field]
breaking: [remove field, rename field, change type, make field required, change allowed values]
breaking_requires: new major version and 30 days noticeThe version follows a major.minor.patch rule. An additive change, such as a new optional refund_reason field, raises it to 1.3.0 and asks nothing of consumers. A breaking change raises it to 2.0.0 and starts the 30-day notice. This team counts any edit to allowed as breaking, added values included, because a consumer that looks up exchange rates by currency fails on a code it has never seen. The amount_cents_meaning line matters as much as the types: a producer who starts including tax keeps every type valid and still moves every revenue figure downstream.
Enforcing it at 2 points
Continuous integration, or CI, is the set of automated checks that run on every proposed code change before it may merge. The contract check in the producer's CI builds a sample event from the changed code, usually inside a unit test, and diffs its fields against the contract file. If a pull request renames amount_cents to amount, the diff shows a required field missing and an undeclared field added. The check classifies that as breaking and fails the build unless the same pull request raises the contract to 2.0.0, which puts the rename in front of reviewers and starts the clock for consumers.
- Code change (source, producer repo)
- CI contract check (quality gate, CI runner); on error: fail build on breaking diff
- Checkout service (transform, production service)
- Order events (queue, event queue)
- Contract gate (quality gate, pipeline task); on error: quarantine batch
- orders_curated (silver, curated table); freshness: 60 min
- Finance and fraud (consumer, downstream readers)
- Code change -> CI contract check
- CI contract check -> Checkout service
- Checkout service -> Order events
- Order events -> Contract gate
- Contract gate -> orders_curated
- orders_curated -> Finance and fraud
- Build time: Code change, CI contract check
- Run time: Checkout service, Order events, Contract gate, orders_curated, Finance and fraud
A checkout engineer adds JPY to the allowed currencies and raises the contract from 1.2.0 to 1.3.0 in the same pull request. What does the CI contract check do under this contract's evolution policy?
- Passes the build, because adding a value only widens what consumers receive, which the policy treats as additive.
- Fails the build, because any change to allowed values is breaking and needs 2.0.0 plus 30 days notice.
- Passes the build, and the pipeline gate later quarantines every batch containing a JPY order.
- Fails the build, because every edit to a contract file needs a new major version, whatever it changes.
CI sees code, so it misses every problem the code did not cause. Suppose a payment partner starts routing orders through checkout with currency set to JPY by configuration. No code changed, so CI has nothing to check. The gate runs on each batch of real rows against the guarantees, so it catches the unknown currency, a day that lands 180,000 rows, or a replay that repeats order_id values. A failing gate quarantines the batch, and consumers keep reading the last good partition: the block authority from the beginner lesson, now driven by the contract.
SELECT
count(*) AS row_count,
count_if(order_id IS NULL) AS null_order_id,
count(*) - count(DISTINCT order_id) AS duplicate_order_id,
count_if(amount_cents < 1) AS bad_amount,
count_if(currency NOT IN ('USD', 'EUR', 'GBP')) AS bad_currency
FROM staging.order_completed
WHERE batch_date = DATE '2026-10-23'A refunds integration starts writing refunds into the order event stream as negative amount_cents values. Checkout's code has not changed. Which enforcement point stops these rows from reaching orders_curated?
- The CI contract check, because negative values violate the min: 1 rule declared in the contract file.
- Neither point, because amount_cents is still a bigint and the schema in the contract is unchanged.
- Both points together: CI flags the broken rule, then the gate quarantines the batch it describes.
- The pipeline gate, because its bad_amount count rises above 0 and it quarantines the batch.
About this lesson
This section is part of the Data Quality: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.