What are Data Contracts in Confluent Schema Registry, and how do CSFLE and migration/data rules fit in?
answer
- Data Contract = schema + tags + metadata + rule sets
- rule types: data-quality (CEL), transform, migration (UPGRADE/DOWNGRADE), encryption
- CSFLE = client-side field encryption, broker sees ciphertext
- KEK in KMS + DEK envelope encryption on tagged fields
- migration rules bridge compatibility groups (breaking changes, no big-bang)
basics
~20 sA Data Contract enriches a schema with metadata, tags, and rules. Rules include data quality/transformation rules, migration rules (to transform data across incompatible major versions), and encryption rules — CSFLE (Client-Side Field Level Encryption) encrypts tagged sensitive fields at the producer before they ever hit the broker.
solid answer
~40 sA **Data Contract** is the schema plus governance metadata: field-level **tags** (e.g. PII), arbitrary **metadata**, and **rule sets**. Rules come in types: **domain/data-quality rules** (e.g. CEL expressions validating field values), **transformation rules**, **migration rules** (UPGRADE/DOWNGRADE transforms that let consumers and producers bridge an otherwise-incompatible major version change), and **encryption rules** that implement **CSFLE — Client-Side Field Level Encryption**. With CSFLE you tag sensitive fields and attach an encrypt rule bound to a KMS-managed key (AWS/GCP/Azure KMS or local); the serializer encrypts those fields on the client before publishing, so the broker and anyone without the key sees ciphertext. Rules execute in the serializer/deserializer pipeline. Migration rules pair with the COMPATIBILITY 'compatibility group' so you can do breaking schema changes by transforming payloads between version groups rather than forcing one big-bang cutover.
go deeper
Know a Data Contract adds rules/metadata to a schema and CSFLE encrypts sensitive fields.
Know the rule categories and that CSFLE is client-side field-level with a KMS key.
Explain envelope encryption (KEK/DEK), where rules run in the serdes, and migration rules vs normal compatibility.
Architect governance: KMS key lifecycle/rotation, compatibility groups for breaking evolution, perf/searchability trade-offs, DLQ on rule failure.
## From schema to Data Contract Classically a registry entry was just a schema (Avro/Protobuf/JSON). A **Data Contract** extends that into a richer governance artifact attached to the subject's schema, comprising: - **Tags** — labels on fields/records (e.g. `PII`, `SENSITIVE`), usable by lineage/catalog and by rules. - **Metadata** — arbitrary key/value properties (ownership, sensitivity, etc.), with optional sensitivity marking. - **Rule sets** — ordered rules executed during serialization/deserialization. ## Rule categories 1. **Data quality / domain rules** — validate field values, often expressed as **CEL (Common Expression Language)** boolean expressions (e.g. `size(message.name) > 0`). On violation the rule can fail the message or route it (e.g. to a DLQ) via the rule's `onFailure` action. 2. **Transformation rules** — modify field values in flight (e.g. CEL_FIELD transforms). 3. **Migration rules** — `UPGRADE` / `DOWNGRADE` transformations that convert payloads between **compatibility groups**. They let you make an otherwise **breaking** change (a new major version) while older and newer clients keep interoperating: a consumer reading an old-version record can apply the UPGRADE rule to read it as the new shape, and vice versa. This is the mechanism for incompatible evolution without a synchronized cutover. 4. **Encryption rules → CSFLE**. ## CSFLE — Client-Side Field Level Encryption **CSFLE** encrypts specific **tagged fields** on the **client** (producer) before serialization output leaves the process, and decrypts on the consumer — so the **broker never sees plaintext** for those fields. Mechanics: - Tag the sensitive fields (e.g. tag `PII`). - Attach an **encryption rule** that targets fields with that tag and references a **KEK (key encryption key)** in a **KMS** — AWS KMS, GCP KMS, Azure Key Vault, HashiCorp Vault, or a local key for testing. - The serializer generates/uses a **DEK (data encryption key)**, encrypts the tagged fields (envelope encryption), and only authorized consumers (with KMS access) can decrypt. - Because it's **field-level**, non-sensitive fields stay queryable/cleartext while PII is protected end-to-end, satisfying regulations even from the broker operator. ## How rules execute Rules live in the contract's rule set and are invoked by the **serializer/deserializer** (the Schema Registry serdes run the rule engine). Producers run write-path rules (validation, encryption, transforms); consumers run read-path rules (decryption, migration upgrades, validation). ## Edge cases & design considerations - **Key management**: losing the KMS key means losing access to encrypted historical data; rotation and access policy are core design concerns. - **Migration rules** require defining **compatibility groups** (via a metadata property the compatibility level keys on) so the registry knows which versions a rule bridges. - **Performance**: per-message client-side crypto and rule evaluation add CPU/latency; DEK caching mitigates KMS round-trips. - **Searchability**: encrypted fields are opaque to brokers/ksqlDB — you can't filter/join on them server-side. - Data Contracts/CSFLE are a **client + registry** feature; the broker is unaware, which is why it works with vanilla Kafka brokers. ## Why it matters Data Contracts move governance (quality, evolution, privacy) into the schema artifact itself and enforce it at the edges via serdes — giving end-to-end PII protection (CSFLE), enforceable quality, and a path to breaking changes (migration rules) without coordinated downtime.
- Where does CSFLE encryption happen, and what does the broker see?Encryption happens client-side in the producer's serializer before the bytes leave the process; the broker stores and serves ciphertext for the tagged fields and never sees plaintext. Only consumers with KMS key access can decrypt.
- How do migration rules let you make a breaking schema change without a synchronized cutover?You split versions into compatibility groups and define UPGRADE/DOWNGRADE transform rules. Clients on either side apply the rule to translate payloads between the old and new shapes, so old and new producers/consumers interoperate during the transition instead of all switching at once.
saying these in an interview costs you the question
- Saying CSFLE encrypts on the broker or in transit only — it's client-side field-level; the broker stores ciphertext.
- Claiming Data Contracts require special brokers — it's a client+registry feature; brokers are unaware.
- Confusing migration rules with normal BACKWARD/FORWARD compatibility — migration rules exist precisely to bridge otherwise-incompatible major versions.
- Thinking the whole message is encrypted — CSFLE is field-level, scoped to tagged fields, leaving others queryable.