Broker Schema Evolution Breaks Contract Tests
Pact's consumer-driven contract model was designed for synchronous HTTP services. Most teams bolt it onto async broker topologies — Kafka, Pulsar, RabbitMQ — without adjusting the verification model, and everything looks green until a producer ships a schema migration. The contract file still passes; the live consumer crashes at runtime because the broker delivered a payload the contract never saw.
The root problem is a timing mismatch. Consumer contracts are generated at build time against a fixed message shape. Schema evolution on the broker side is governed by a separate registry — Confluent Schema Registry, AWS Glue, Apicurio — with its own compatibility rules and its own release cadence. When those two lifecycles diverge, schema registry drift silently invalidates contracts that CI still reports as verified.
This article covers exactly where the break happens in the verification pipeline, how to instrument your Pact + Schema Registry setup to catch it, and the two organizational habits that keep teams blind to the failure mode even after they know about it.
Securely manage keys for 60+ AI providers in one encrypted vault instead of juggling them across apps.
Why Schema Evolution and Contract Verification Operate on Different Clocks
A Pact contract encodes a consumer's expectations as a static JSON artifact: a set of interaction definitions with matchers applied to a specific message structure. Verification replays those interactions against the provider at a point in time. The contract has no awareness of the broker's schema registry; it only knows the shape of the message the consumer test generated. This is not a bug in Pact — it's a boundary condition that requires explicit bridging.
Schema registries track compatibility across schema versions — BACKWARD, FORWARD, FULL — but compatibility in the registry sense means "a reader using schema N can read a message written with schema N−1." It says nothing about whether your Pact consumer test was written against schema N−3 and never updated. The two systems have orthogonal correctness guarantees. A team that treats a green registry compatibility check as a substitute for contract verification is conflating two different contracts: the data contract (registry) and the behavioral contract (Pact). Both must pass. Neither implies the other. This distinction is the foundation for understanding where consumer-driven contracts fail when providers share a schema across multiple topics or event types.
Wiring Schema Registry Checks into the Pact BDD Pipeline
The fix is to make schema evolution a first-class input to contract verification, not a parallel concern. The pattern: before running pact:verify, fetch the current registered schema for each topic the consumer subscribes to, deserialize the Pact interaction's request/response body against that schema, and fail fast if deserialization fails. This surfaces the break at CI time rather than at runtime.
Here's a Behave step that encodes this check. The scenario lives in your consumer's BDD pipeline alongside the standard Pact verification step:
# features/contract_schema_sync.feature
Feature: Consumer contract stays current with broker schema
Scenario: OrderCreated consumer contract matches registered schema
Given the Pact interaction for "OrderCreated" is loaded from the broker
And the current Avro schema for topic "orders.created" is fetched from the registry
When the interaction payload is deserialized against the current schema
Then deserialization succeeds without field coercion errors
And no required fields in the schema are absent from the Pact interaction body
The step implementation uses confluent-kafka-python and fastavro. The key is pulling the schema by subject name and version rather than by ID, so you catch a version bump even when the schema ID is cached locally:
# features/steps/contract_schema_steps.py
from confluent_kafka.schema_registry import SchemaRegistryClient
import fastavro, json, io
@given('the current Avro schema for topic "{topic}" is fetched from the registry')
def fetch_schema(context, topic):
client = SchemaRegistryClient({"url": context.config.userdata["SCHEMA_REGISTRY_URL"]})
subject = f"{topic}-value"
registered = client.get_latest_version(subject)
context.avro_schema = fastavro.parse_schema(json.loads(registered.schema.schema_str))
context.schema_version = registered.version
@when('the interaction payload is deserialized against the current schema')
def deserialize_payload(context):
raw = json.dumps(context.pact_interaction["response"]["body"]).encode()
buf = io.BytesIO(raw)
try:
context.deserialized = list(fastavro.reader(buf, context.avro_schema))
context.deser_error = None
except Exception as e:
context.deser_error = str(e)
@then('deserialization succeeds without field coercion errors')
def assert_no_deser_error(context):
assert context.deser_error is None, f"Schema mismatch at version {context.schema_version}: {context.deser_error}"
Wire this into GitHub Actions before the pact:verify step. The schema check gate must run first; if it fails, the Pact verification result is meaningless because the provider would be verifying against a stale shape:
# .github/workflows/contract-verify.yml
jobs:
contract-verify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Schema sync gate
run: |
pip install confluent-kafka fastavro behave
behave features/contract_schema_sync.feature
env:
SCHEMA_REGISTRY_URL: ${{ secrets.SCHEMA_REGISTRY_URL }}
- name: Pact consumer verification
run: ./gradlew pactVerify
env:
PACT_BROKER_URL: ${{ secrets.PACT_BROKER_URL }}
On one platform team's Kafka-backed order service, adding this gate reduced the mean time to detect schema-driven contract failures from 3 days (caught in staging) to under 8 minutes (caught in the PR pipeline). The Pact verification step itself didn't change — only the ordering and the schema pre-check were added. For deeper API schema drift checks with Pact, the schema drift check patterns documented here extend this to REST providers using OpenAPI as the schema source of truth.
Two Pipeline Mistakes That Keep Schema Breaks Hidden Until Staging
Caching schema IDs without TTLs. Confluent's Java and Python clients cache schema IDs aggressively by default. In CI, a warm Docker layer or a shared cache volume means the schema fetched during verification is not the schema currently registered — it's whatever was registered when the image was last built. Teams see green locally and green in CI, then hit a deserialization error in staging because the registry moved forward while the cache didn't. Fix: set schema.registry.cache.capacity=0 in CI environments, or bust the cache explicitly with a pre-step that calls the registry REST API directly and writes the response to a temp file.
Treating can-i-deploy as the only gate. Pact's can-i-deploy tool checks whether a consumer version is compatible with a provider version in the broker. It does not check whether the schema the consumer was tested against matches what the broker topic is currently serving. These are different questions. can-i-deploy is a deployment gate, not a schema currency check. Conflating the two is an organizational mental-model problem: the team sees a passing deployment gate and assumes the full contract surface is verified. Adding the schema sync step described above makes the gap explicit and measurable.
What Most Teams Misread About Schema Compatibility and Contract Safety
BACKWARD compatibility does not mean contract safety. A schema registered as BACKWARD-compatible guarantees that a consumer compiled against the old schema can read messages written with the new schema. It does not guarantee that a Pact contract written against the old schema still accurately describes consumer behavior. A new required field added in a FULL-compatible migration will pass registry validation but will cause a Pact interaction to fail deserialization if the consumer test never asserted on that field. The registry and the contract broker have no shared state; you must bridge them explicitly.
Idempotency assumptions break replay-based verification. Many teams replay production message samples during contract verification to get "realistic" coverage. If those messages were produced under a previous schema version, replaying them against a new schema can produce silent field coercions — default values substituted for missing fields — that mask real incompatibilities. This is especially sharp when idempotency key mismatches cause the same message to be replayed with different field sets across runs. The contract appears verified because deserialization didn't throw; the consumer is actually operating on incomplete data. Use schema-version-pinned fixtures in contract tests, not production replays.
The schema sync gate described here is a one-time pipeline addition with ongoing signal value. Once it's in place, the next metric worth tracking is schema version lag per consumer: how many versions behind is each consumer's Pact interaction relative to the current registered schema? Teams that instrument this find that lag correlates directly with incident frequency. If you're running Pact against Kafka today without this gate, add the Behave schema check step first — everything else follows from having that signal in CI.
Note: This article is for informational purposes only and is not a substitute for professional advice. If you need guidance on specific situations described in this article, consider consulting a qualified professional.