Consumer-driven contracts (contract testing)
Each consumer of an API writes down what it actually uses, and the provider’s build checks every change against those expectations before release.
End-to-end tests across many services are slow and flaky, so teams skip them and find breaking changes in production. Contract tests split the check in two: the consumer’s tests record the requests it makes and the responses it relies on, and the provider replays those contracts in its own CI. Martin Fowler described the pattern; Pact is the most common tool.
The result is a list of who uses which field, so the provider knows what it can change safely and what needs API versioning. It is the code-level version of a customer-supplier relationship and one of the best defenses against a distributed monolith.
Sources
- Martin Fowler: Consumer-Driven Contracts: A Service Evolution Pattern martinfowler.com
- Pact Docs: Introduction docs.pact.io
Related terms
- API versioning: Giving an API contract explicit versions so consumers can upgrade on their own schedule, and retiring old versions with notice.
- Customer-supplier (upstream and downstream): A relationship between two teams where the upstream team plans its changes with the needs of the downstream team that depends on it.
- Distributed monolith: Services that look separate but have to be changed, tested and deployed together, so you pay for microservices and still have a monolith.
- CI/CD: Continuous integration and continuous delivery: every change is built and tested automatically and can be released at any time.
- Tolerant reader: A consumer of an API or event reads only the fields it needs and ignores everything else, so the provider can add fields without breaking it.
