Core Idea

A contract is a formal specification that defines the interface and behavioral expectations between two software components—how they communicate and what each party must uphold.

Definition

A contract is a formal specification defining the interface and behavioral expectations between two software components. Contracts establish explicit agreements about how services communicate—specifying what requests must contain, what responses will provide, and what guarantees each party must uphold.

The concept draws from the legal metaphor and was formally articulated by Bertrand Meyer through “Design by Contract” for the Eiffel language.

Key Characteristics

  • Formal specification: Documented, often machine-readable, defining data structures, protocols, preconditions, postconditions, and invariants; expressible via OpenAPI, Protocol Buffers, or GraphQL schemas
  • Versioned agreements: Must be versioned as systems evolve to allow compatibility between implementations and support gradual migration
  • Preconditions and postconditions: Specify what must be true before an operation and what is guaranteed after—catching errors early at component boundaries
  • Consumer-driven evolution: In microservices, consumers often drive contract changes (tools like Pact); producers gain visibility into how their services are actually used
  • Testing and verification: Contracts enable isolated testing without full integration tests, reducing reliance on slow, brittle end-to-end tests

Why It Matters

  • Independent evolution: Teams can change implementations freely as long as they honor the contract; without explicit contracts, changes break consumers unexpectedly
  • Reduced integration risk: Automated contract testing in CI/CD pipelines catches incompatibilities early
  • Team autonomy: Contracts provide clear boundaries of responsibility—the foundation for coordinated evolution without tight coordination overhead in microservices

Sources

Note

This content was drafted with assistance from AI tools for research, organization, and initial content generation. All final content has been reviewed, fact-checked, and edited by the author to ensure accuracy and alignment with the author’s intentions and perspective.