Contracts between modules and services: sync vs async, provider vs consumer ownership, consumer-driven contract testing with Pact, and backward-compatible versioning.
A contract is the only surface others may depend on: a module's public interface, an HTTP or gRPC API, or an event schema. I choose synchronous calls when the caller needs an answer to continue, and asynchronous messages when it doesn't, because messages decouple availability and absorb load spikes.
The provider owns the contract but designs it from consumer needs; for events, the publisher owns the schema. To change safely I use consumer-driven contract tests (Pact): each consumer records what it relies on, and the provider's pipeline verifies those expectations before deploying.
Versioning rule: additive changes by default, tolerant readers, and an explicit new version with a deprecation window only for breaking changes.
The trade-off: sync is simpler but couples availability; async is resilient but harder to trace and reason about.
"I treat every boundary as having one contract and nothing else exposed. Inside a modular monolith that's a
Contractsproject with interfaces and records; between services it's an OpenAPI or protobuf definition for request/response, and an AsyncAPI or JSON Schema definition for events. The first decision is sync or async. If the caller genuinely needs the answer to continue, like checking stock before showing a price, I use HTTP or gRPC with timeouts and resilience. If it's telling others something happened, or work can complete later, I publish an event or send a command over Azure Service Bus, so the provider can be down for a minute without the caller failing.Ownership matters. The provider owns its API, but a good provider designs it from what consumers actually need, not from its database tables. For events, the publisher owns the schema and consumers adapt. For example, at [company] the Pricing team renamed a field in their API and [three] consumers broke in production on a Friday. I introduced consumer-driven contract tests with PactNet: each consumer published a pact describing the fields and responses it used to a Pact Broker, and Pricing's pipeline ran
can-i-deploybefore release. In the next [six] months we had [zero] contract-break incidents, and Pricing could delete fields confidently because they knew which ones were unused.The mistake I see is sharing a NuGet package of DTOs and calling that the contract, which couples release cycles, or versioning everything as v2 at the first change. My senior line is: make changes additive, let consumers prove what they depend on, and reserve a new version for the rare change you can't make additive."
| Synchronous (HTTP/gRPC) | Asynchronous (events/commands) | |
|---|---|---|
| Caller needs answer now | Yes | No |
| Availability coupling | Caller fails if provider is down | Provider can be down; messages wait |
| Latency | Adds to request path | Off the request path |
| Load levelling | No | Yes (queue absorbs spikes) |
| Debugging | Easier | Needs tracing and correlation |
| .NET tools | HttpClient + resilience handler, gRPC |
Azure Service Bus, MassTransit/Wolverine/NServiceBus, outbox |
| Contract | Owner | Consumers |
|---|---|---|
| Module interface | Module team | Other modules via Contracts project |
| HTTP/gRPC API | Provider | Influence via consumer-driven tests |
| Event | Publisher | Adapt; tolerant readers |
| Command | Receiver (it defines what it accepts) | Senders |
| Change | Breaking? | How |
|---|---|---|
| Add optional field / endpoint | No | Just ship |
| Add required request field | Yes | Make optional with a default, or new version |
| Rename or remove field | Yes | Expand-and-contract: add new, migrate consumers, remove old |
| Change meaning of a field | Yes (silently!) | Never; add a new field |
Adding PactNet consumer-driven tests and a can-i-deploy gate between [Pricing] and its [three] consumers removed contract-break incidents ([four] in the previous half-year to [zero]) and let Pricing retire [11] unused fields safely.
Checks whether the candidate can choose integration styles deliberately, assign contract ownership, and evolve contracts safely with evidence.
Green flags
Red flags
A contract is more than a shape. It includes:
Most production breaks are semantic ("amount was in cents, now in dollars"), which schema checks alone don't catch.
Sync request/response:
Checkout ββGET /pricesβββΊ Pricing (checkout waits; Pricing down = checkout degraded)
Async event:
Ordering ββOrderPlacedβββΊ Service Bus topic βββΊ Billing, Shipping, Loyalty
(each consumes at its own pace)
Long synchronous call chains multiply failure: three services at 99.9% each give roughly 99.7% for the chain. Prefer async for anything that doesn't need an immediate answer, and keep sync chains to one or two hops.
In a provider-driven model the provider publishes a spec and hopes consumers use it correctly. In a consumer-driven model, each consumer writes tests that generate a pact (the requests it sends and the parts of the response it reads). The provider verifies all pacts against its real implementation.
Consumer CI: unit test with Pact mock βββΊ pact file βββΊ Pact Broker
Provider CI: fetch pacts βββΊ replay against provider βββΊ publish results
Deploy gate: pact-broker can-i-deploy --pacticipant Pricing --version $SHA --to production
This tells the provider exactly which fields are used, so unused ones can be removed. Pact supports HTTP and message (event) contracts.
System.Text.Json ignores unknown properties by default; don't turn on UnmappedMemberHandling.Disallow for contracts from other teams.Asp.Versioning.Http with URL segment (/v2/) or header; publish both versions in OpenAPI; set Sunset/Deprecation headers.type: com.shop.order.placed.v2); publish both during migration.The same rules apply at lower cost: a Contracts project per module, records for requests and results, and architecture tests preventing references to internals. Because it's one repo and one deploy, breaking changes are a refactor, but treating the contract seriously keeps extraction possible.
WebApplicationFactory.Asp.Versioning.Http to a Minimal API and expose v1 and v2 side by side in OpenAPI.A consumer-driven contract test with PactNet: the consumer states exactly what it relies on.
namespace Checkout.Tests;
using System.Net;
using System.Net.Http.Json;
using PactNet;
using PactNet.Matchers;
public sealed record PriceDto(string Sku, decimal Amount, string Currency);
public class PricingClientPactTests
{
private readonly IPactBuilderV4 _pact = Pact.V4("Checkout", "Pricing", new PactConfig())
.WithHttpInteractions();
[Fact]
public async Task Gets_price_for_sku()
{
_pact.UponReceiving("a price request for an existing SKU")
.Given("SKU ABC-1 exists")
.WithRequest(HttpMethod.Get, "/v1/prices/ABC-1")
.WillRespond()
.WithStatus(HttpStatusCode.OK)
.WithJsonBody(new
{
sku = "ABC-1",
amount = Match.Decimal(19.95m), // only the fields Checkout reads
currency = Match.Regex("AUD", "^[A-Z]{3}$")
});
await _pact.VerifyAsync(async ctx =>
{
using var http = new HttpClient { BaseAddress = ctx.MockServerUri };
var price = await http.GetFromJsonAsync<PriceDto>("/v1/prices/ABC-1");
Assert.Equal("AUD", price!.Currency);
});
}
}
# Provider pipeline: verify all consumer pacts, then gate the deploy.
pact-broker can-i-deploy --pacticipant Pricing --version "$GIT_SHA" --to-environment production
Deprecation/Sunset headers, track usage, then remove.Contract testing shifts the conversation from "please don't break us" to evidence: the provider can see exactly which consumer depends on which field and decide quickly. I pair that with ownership rules written into the architecture docs: the provider owns APIs, the publisher owns events, and the receiver owns commands. That avoids the common stalemate where nobody feels allowed to change anything.
Pact needs some investment (a broker, provider states, CI wiring), so for one internal consumer in the same repo I may rely on integration tests instead. It pays off once there are several consumers or several teams.
| Option | When it fits | Cost | When not to |
|---|---|---|---|
| Sync HTTP/gRPC | Caller needs an immediate answer | Availability coupling | Notifications, long-running work |
| Async events | Fan-out, decoupling, load levelling | Eventual consistency, tracing | Caller needs answer now |
| Consumer-driven contract tests | Several consumers or teams | Broker, CI setup | Single consumer in same repo |
| Explicit versioning | Unavoidable breaking change | Running two versions | Additive changes would do |
An integration event is a public API owned by the publisher. Within a version I only make backward-compatible changes: add optional (nullable or defaulted) fields. I never add a required field mid-version (messages already in queues won't have it), and never rename, remove or retype. Consumers are tolerant readers: they ignore unknown fields and default missing optional ones. A breaking change becomes a new event type (orders.order-placed.v2), published alongside v1 until consumers move, then deprecated with a date.
I wrap events in a CloudEvents envelope (id, source, type, time, dataschema). Each publisher ships its own contracts package or JSON Schema; one shared events library for every service forces lockstep upgrades.
The trade-off: discipline and some duplication in exchange for independent deployments.
A logical boundary decides what code may depend on: a module, an assembly, a namespace, a database schema. A deployment boundary decides what ships and runs together: one App Service, one container, one Function app.
They don't have to match. A modular monolith has many logical boundaries in one deployable. Microservices add deployment boundaries so units can be deployed independently, which only works if contracts stay backward compatible.
The quiet killer is shared libraries: a NuGet package with shared domain models or contracts that every service must upgrade together turns separate deployables back into a distributed monolith.
Monorepo vs polyrepo is about workflow, not architecture: a monorepo makes cross-cutting changes easy; a polyrepo forces versioning. The trade-off is coordination cost vs isolation.
A consumer-driven contract test checks one consumer-provider pair in isolation. The consumer's tests run against a Pact mock and record what it actually uses; the provider replays those expectations against its real code (dependencies stubbed) in its own CI. Because the provider must keep passing every consumer's contract, it can evolve and version its API without silently breaking anyone, which preserves team autonomy.
An end-to-end test deploys many services together and drives a real user journey. It catches wiring, configuration and infrastructure problems contract tests can't, but it needs a shared environment, runs for minutes, fails for unrelated reasons and slows every deployment pipeline that waits on it.
So: a contract test for every consumer-provider pair as the main safety net, and only a handful of end-to-end smoke tests for the few journeys that make money. The trade-off is fast, precise feedback versus realistic but slow, flaky coverage.
How to tell it (STAR)
**S:** At [company] a field rename in Pricing broke [three] consumer services in production on a Friday afternoon. **T:** I was asked to stop contract breaks without slowing teams down. **A:** I set up PactNet consumer tests with the first consumer myself, ran a session with the other teams to add theirs, added a Pact Broker and a `can-i-deploy` gate to Pricing's pipeline, and wrote a short expand-and-contract guideline. **R:** Contract-break incidents went from [four] in the previous six months to [zero], and Pricing safely removed [11] unused fields.
Last updated 23 Nov 2026.