# Data contract

## What it is

A data contract records what a producer and its consumers can rely on when data
crosses a boundary. It can describe fields, identifiers, units, allowed values
and the grain of a record. It can also identify owners, freshness requirements,
quality checks, and how changes will be introduced. The contract gives teams a
shared basis for deciding whether a new delivery can be used safely.

A schema is often part of the contract. The schema might require a decimal field
and a timestamp, for example. The contract can say that the decimal is expressed
in a named currency, that the timestamp is the moment the source recorded a
completed sale, and that every row represents one sale. The schema makes a
portion of that agreement checkable by software. The unit, historical meaning
and freshness may need additional checks or documented policies.

An effective contract connects its promises with evidence. Types can be tested
as part of a build. An example payload can show how a cancellation is
represented. A test against the published dataset can check that each expected
date is present. The agreement becomes easier to maintain when the parties can
find the definition, verify the behaviour, and identify who can approve a
change.

## Why it matters

A producer can make a breaking change while the shape of the payload still
validates. Suppose a report reads an amount that has always been stored in
cents. The producer decides to write whole units of the currency to simplify a
new integration. The values can still fit the same numeric type. An existing
consumer divides them by one hundred and publishes numbers that look plausible
at a glance. The contract needs to specify the unit and the transition policy,
so a review can distinguish a formatting adjustment from a different business
interpretation.

Sparse fields raise a similar issue. A field can be missing because a producer
has not collected it yet. An explicit null could signal that the value was
checked and is unknown. An empty string might mean a user cleared a label. Those
distinctions affect transformations and reporting. Preserve them where consumers
depend on them, and include an example of each permitted state. An example
should establish what the recipient can infer, especially when a new field is
optional during a rollout.

The agreement also reduces uncertainty when data arrives late or fails a check.
A daily report may use a stable published snapshot if its inputs have not
arrived. An automated stock decision might require current data and need to stop
or route the request elsewhere. Freshness is useful when it is written as an
observable condition and paired with a recovery path. Name the expected
schedule, how delay is detected, and who decides when to release an adjusted
dataset.

## What it looks like in practice

Consider a feed that provides completed sales to a finance system. It records
the sale identifier, time of completion, currency and amount in minor units. One
version of the contract defines when a sale appears, how a later cancellation is
represented and whether repeated deliveries are possible. It also specifies
which business day a sale belongs to when stores operate across timezones. Put a
normal sale, a cancellation and a delayed record in the examples so those
decisions are visible to both sides.

Run checks at the boundary that can observe each promise. A producer test can
ensure the amount is encoded in minor units and the timestamp is present. A
consumer fixture can verify that a cancellation reverses the intended sale. A
published-data check can detect duplicate identifiers or a missing day. Each
failed check should say which promise was broken and point to its owner. Changes
to units, meaning or record grain deserve review alongside changes to field
names and types.

Engineer's
[database-to-API contract case study](/portfolio/generated-the-api-contract-from-the-database-out/)
describes generating an OpenAPI description, a typed TypeScript client and
frontend test fixtures from connected sources of truth. The work also checks
details that can drift, including the distinction between optional fields and
explicit null values. A data-feed contract can use the same idea of exercising
both sides with concrete examples. Generation removes some manual copies, and
verification gives the producer and consumers evidence that the published
behaviour still matches the agreed meaning.

## Where it goes wrong

A contract stored beside the code can become stale when the build and the
published data carry no check connected to it. Choose a small set of promises
important enough to enforce and attach each to an owner and a test. A generated
schema can make a change visible in review. A measured dataset check can show
when the running system still fails a requirement even though the build accepts
its source.

Compatibility also depends on which consumers still exist. A new application can
handle an added field by using a default when it reads older records. An older
consumer may reject unexpected fields or rely on a meaning that changed without
a new field. Record the support window, test the relevant producer and consumer
combinations, and coordinate a revised dataset before removing the old
representation. Keep a useful version identifier and a route for discovering
which version a particular delivery used.

A contract gives a team a way to respond to failures. It also needs a
proportionate response. A harmless optional label might justify a warning, while
a missing currency can require isolation of the records before a financial
report runs. Treat exceptions as observable decisions with an expiry and an
owner. Test a late delivery and a refused record explicitly. The resulting
evidence makes it easier to restore confidence in a dataset after its producer
or consumers change.

## Further reading

- [The Open Data Contract Standard](https://bitol.io/odcs/). Introduces an open
  format for schema, quality, support and other data agreements.
- [The Open Data Contract Standard specification repository](https://github.com/bitol-io/open-data-contract-standard).
  Provides versioned definitions and examples.
- [Confluent data contracts](https://docs.confluent.io/platform/current/schema-registry/fundamentals/data-contracts.html).
  Describes contracts and rule enforcement around serialized streams.

<https://engineer.company/glossary/data-contract/>
