Skip to content
mentoor

Topic 2 of 5 · 5 min read

Data contracts between producers and consumers

Goal: Write a data contract for a table and name a producer change that would break it.

A data contract is a written, checkable agreement between the team that produces a table and the teams that read it: which columns, which types, what they mean and how fresh they are. The key word is checkable. A contract nobody enforces is documentation that drifts.

Check yourself · 1 of 3

What makes a data contract different from documentation?

Choose one answer

Why it matters

Most data breakages are not bugs in the pipeline. A producer renames cust_id to customer_id, changes cents to euros, or starts sending nulls, and every consumer learns about it from a broken dashboard. The producer did nothing wrong by their own tests; they did not know who depended on the shape.

A contract moves that conversation earlier. The change fails in the producer's pull request, not in the consumer's report next morning.

What goes into one

  • Schema: column names, data types, nullability, primary key.
  • Meaning: what each column is, in what unit, and the grain (one row per what).
  • Quality rules: not null, unique, accepted values, valid ranges.
  • Service levels: freshness and how late data may arrive.
  • Ownership and change rules: who owns it, how much notice a breaking change needs.

Two common formats. In dbt, you set contract: {enforced: true} on a model and list every column with its data_type. dbt checks the built table against that list and fails the build when they differ. The Open Data Contract Standard (ODCS) is a YAML format that describes schema, quality and service levels for any platform, and tools can validate data against it.

Before and after

Weak: a description in a wiki saying "orders has the usual columns".

Fixed, as a dbt contract:

models:
  - name: orders_daily
    config:
      contract:
        enforced: true
    columns:
      - name: order_id
        data_type: bigint
        constraints:
          - type: not_null
      - name: amount_eur
        data_type: numeric(12,2)
        description: Order total in euros, after discounts, before tax.
      - name: order_date
        data_type: date

A producer who renames amount_eur or changes it to text now gets a failed build.

What goes wrong

  • Contract covers shape, not meaning. The type stays numeric, but the unit switches from euros to cents. Add descriptions, accepted ranges and a changelog rule.
  • Contract only on paper. If CI does not run it, it is a wiki page with extra steps.
  • Written by the producer alone. Ask the consumers which columns they actually read. Contract the columns they depend on first.
  • Everything is frozen. A contract needs a version and a deprecation period, so planned changes can ship with notice.

Examples

A rename caught in the pull request

The orders team renames cust_id to customer_id. Their dbt model has an enforced contract listing cust_id. CI fails with a column mismatch. They add customer_id, keep cust_id for one release and tell consumers. No dashboard breaks.

What a type check misses

A producer changes amount from euros to cents. The column stays numeric(12,2), so the contract passes. Revenue is suddenly 100 times larger. A rule such as amount_eur between 0 and 100000, and a description stating the unit, would have flagged the jump.

Key points

  • A data contract is an agreement on schema, meaning, quality and freshness that is checked automatically.
  • With dbt, setting contract enforced and listing columns with data types makes the build fail on a mismatch.
  • The Open Data Contract Standard describes the same agreement in a YAML file that works across platforms.
  • A type check does not catch a change of unit or meaning, so descriptions and value rules belong in the contract.
  • Breaking changes need a version and a notice period, so consumers can move before the old shape is removed.

Common mistakes

  • Treating a data contract as a wiki page with no check in CI.
  • Listing column types but not units, grain or allowed values.
  • Writing the contract without asking consumers which columns they use.
  • Changing a contracted column in place instead of adding a new version and a deprecation date.

Check yourself · 2 of 3

Which producer change would a dbt contract with enforced data types catch?

Choose one answer

Check yourself · 3 of 3

A team must rename a contracted column. What is the safest way to ship it?

Choose one answer

Practice items, not counted towards your mastery.

Start here