Consumer-Driven Contracts
A way to evolve services safely: each consumer states what it expects from a provider as an executable contract, and the provider runs all those contracts as tests.
Also known as: Consumer-Driven Contract Testing, CDC Testing, Contract Testing, Consumer-Driven Contract
Category: Software Development
Tags: software-development, testing, software-architecture, api-design, microservices, compatibility, distributed-systems
Explanation
In a system made of many services, the provider of an API rarely knows which parts of it are really used. Consumer-driven contracts turn that around. Every consumer writes down its expectations: "when I send this request, I need a response with at least these fields". Those expectations become automated tests that the provider runs in its own build. If a change would break a consumer, the provider's build fails before anything is deployed.
**How it works**:
1. The consumer team writes tests against a mock of the provider. The tool records each interaction as a contract
2. The contract is published to a shared place, often a broker
3. The provider's pipeline replays every contract against the real provider and checks the responses
4. A deployment only goes ahead when the contracts of all the consumer versions in production pass
Pact is the best-known tool for this.
**What you gain**:
- **Freedom to change**: The sum of all contracts shows exactly what is in use. Everything else can change or go away without breaking anyone
- **Fast feedback**: You find integration problems in a unit-test-speed build, not in a shared test environment
- **Independent deployment**: Teams release on their own schedule, without big integration test phases
- **A real conversation**: The contract makes the dependency between two teams explicit
**How it fits with other ideas**:
- It makes Hyrum's Law manageable: the dependencies are written down instead of hidden
- It complements the Tolerant Reader pattern. Consumers ignore what they don't need, and contracts list what they do need
- It applies Postel's Law with evidence. The provider knows precisely how conservative it has to stay
**Limits**:
- It needs cooperation, so it suits services inside one organization. For a public API with unknown consumers it doesn't work
- Contracts check the shape and examples of interactions, not the full behavior
- There's a cost: tooling, a broker, pipeline integration and discipline
- Consumers that put too much in their contracts freeze the provider again
Related Concepts
← Back to all concepts