We are going to design a message schema registry workflow that includes:
Compatibility scanning (forward, backward, full)
Documentation generation and hosting
Client SDK updates (code generation for multiple languages)
The workflow will be broken down into steps and we'll consider tools and practices.
Steps:
Schema Definition and Storage:
Use a schema registry (like Confluent Schema Registry, Apicurio, or a custom one) to store schemas (Avro, Protobuf, JSON Schema).
Each schema must have a unique ID, version, and metadata (subject, compatibility level set at subject or globally).
Compatibility Scanning:
When a new schema version is proposed, we must check compatibility with previous versions.
Compatibility types:
BACKWARD: new schema can read data written by old schema.
FORWARD: old schema can read data written by new schema.
FULL: both backward and forward.
NONE: no compatibility check.
The registry should automatically check compatibility when registering a new version (if configured) and reject if incompatible.
We can also have a separate compatibility scan tool that can be run on a set of schemas (e.g., for CI/CD).
Documentation:
Generate human-readable documentation from schemas (e.g., using Avro's IDL, Protobuf's descriptor, or JSON Schema's standard).
Host the documentation (static site) and update on schema changes.
Consider generating API documentation (like OpenAPI) if the schemas are part of an API.
Client SDK Updates:
Generate client SDKs in multiple languages (Java, Python, Go, etc.) from the schemas.
The generated code should include serialization/deserialization and possibly domain objects.
We need a way to trigger regeneration and release of SDKs when schemas change.
Proposed Workflow:
Step 1: Developer writes a new schema (Avro/Proto/JSON) and submits it to the registry.
Step 2: Registry checks compatibility (if enabled for the subject). If incompatible, reject and notify.
Step 3: If compatible, registry stores the schema and assigns a new version.
Step 4: Trigger a pipeline (via webhook or event) that:
a. Regenerates documentation and deploys it (e.g., to GitHub Pages, an internal site).
b. Regenerates client SDKs for all languages and pushes to package repositories (e.g., Maven, PyPI, Go modules).
Step 5: Notify stakeholders (e.g., via Slack, email) of the new schema version and SDK updates.
Tools and Implementation:
Schema Registry:
Use an existing one (Confluent, Apicurio) or build a simple one if needed.
Must support compatibility checking.
Compatibility Scanning:
Use the registry's built-in compatibility check, or use a library (like Apache Avro's avro-compiler for Avro, protoc for Protobuf) to check compatibility.
For Avro, we can use avro-tools to compile and check. For Protobuf, protoc with --descriptor_set and then compare.
Documentation:
For Avro: use avro-tools to generate HTML or markdown.
For Protobuf: use protoc-gen-doc (a plugin) to generate documentation.
For JSON Schema: use redoc or spectacle to generate HTML from JSON Schema.
We can write a script that runs the appropriate generator for each schema type and then combines them.
Client SDK Generation:
For Avro: use avro-tools to generate Java classes, and for other languages we can use avro4s (Scala), avro-python3 (Python), etc. But note: Avro doesn't have official code generators for all languages. Alternatively, use a tool that supports multiple languages like avro-schema-generator (for Go) or avro4s (Scala/Java). We might need to use different tools per language.
For Protobuf: protoc with language-specific plugins (e.g., protoc-gen-grpc-java for Java, protoc-gen-grpc-python for Python, etc.).
For JSON Schema: there are code generators for various languages (e.g., quicktype).
We can set up a pipeline that:
Checks out the schemas from the registry (or from a git repo if we version schemas in git and then push to registry).
Runs code generation for each language and then packages and publishes.
Orchestration:
Use a CI/CD tool (Jenkins, GitHub Actions, GitLab CI) to run the pipeline on schema change.
Alternatively, set up a webhook from the schema registry to trigger the pipeline.
Versioning:
Schemas are versioned in the registry. We should also version the generated SDKs appropriately (e.g., semantic versioning, with the schema version as part of the version string?).
Considerations:
Schema evolution: We must enforce compatibility to ensure that consumers and producers can work together.
Security: The registry and the pipeline must be secure (authentication, authorization).
Monitoring: We should monitor the health of the registry and the pipeline.
Example with Apache Avro and Confluent Schema Registry:
Step 1: Developer uses curl or a client library to register a new schema:
Step 2: Confluent Schema Registry will check compatibility (if set) and then store.
Step 3: Confluent Schema Registry can send a webhook on new schema version. We set up a listener (or use a service that can receive webhooks and trigger the pipeline).
Step 4: Our pipeline (e.g., a Jenkins job) is triggered. It does:
code
a. Fetch the latest schema for the subject (or all subjects) from the registry.
b. Generate documentation:
- Use `avro-tools` to convert Avro schema to HTML (or markdown) and then combine into a site.
c. Generate client SDKs:
- For each language we support, run the code generator and then package and publish.
Step 5: Send notification.
Alternative: Version schemas in Git and then push to registry?
Some teams prefer to version schemas in a Git repository and then have a process that pushes to the registry. This way, the schemas are version controlled and the registry is just a runtime component.
In that case, the workflow might be:
Developer updates schema in Git and creates a pull request.
CI runs compatibility check (by trying to register the new schema in a test registry or using a compatibility tool) and also generates documentation and SDKs for review.
On merge to main, the CI pushes the new schema to the registry and then triggers the documentation and SDK generation and publishing.
This approach gives more control and auditability.
Let's design a hybrid:
We store schemas in Git (with a directory structure per subject and version in the filename or in the schema metadata).
We have a CI that on push to a branch (or merge) does:
a. Check compatibility (using the registry's compatibility endpoint or a tool) against the latest version in the registry (or against a set of previous versions in the same branch?).
b. If compatible, then:
- Register the new schema version in the registry (using the subject and the schema from Git, and let the registry assign a version? or we specify version?).
- Generate documentation and host.
- Generate SDKs and publish.
But note: the registry is the source of truth for the current version and compatibility. So we might want to let the registry assign the version.
Alternatively, we can have a two-step:
Register the schema via a CI job that checks compatibility and then registers (so the registry assigns the version).
Then, after registration, we use the version from the registry to generate documentation and SDKs with the correct version.
How about:
We have a Git repository for schemas.
Each schema file is named with the subject and version? Or we have a manifest that maps subject to schema file?
Let's assume we use Avro and the subject is the name of the record? Or we can have a subject per schema file? Typically, in Confluent, the subject is derived from the topic name or from the record name.
But then we have to manage the versioning in the file names. Alternatively, we can have one file per subject and then the version is in the registry. But then we lose the ability to see all versions in Git? Not necessarily, because the registry stores the history.
However, for reproducibility and code review, we want to version the schemas in Git. So we can do:
code
- Each time we change a schema, we create a new file? But then we have to manage the mapping between the file and the version in the registry.
Alternatively, we can have a single file per subject and then the version is the Git commit? But then the registry's version and the Git version are not aligned.
Let's think: the registry version is an integer that increments for that subject. We can store the registry version in the Git commit message? Not ideal.
Another approach: We use the registry as the source of truth and we don't version schemas in Git? But then we lose the ability to review changes in PRs.
So, we want:
code
- Schema changes are proposed via PR in Git.
- The PR shows the diff of the schema file (for a given subject) and we can review.
- The CI checks compatibility with the current version in the registry (which is the one that is in production) and also with the previous versions (if needed for full compatibility?).
- If the PR is merged, we push the new schema to the registry and then the registry assigns the next version.
How do we know which file to push for which subject? We can have a convention:
code
- Each subject has a directory, and in that directory there is a file named `current.avsc` (or `schema.avsc`). When we merge, we take the `current.avsc` and register it for that subject.
But then we lose the history of the schema in the Git repo? We can keep the history in Git (by committing the change to current.avsc), but we don't have multiple versions in Git. We can also tag the commit with the schema version? But then we have to coordinate.
Alternatively, we can have a script that, when merging, does:
code
1. For each subject, read the `current.avsc` and register it with the registry.
2. The registry returns the version number (say, 5). Then we can commit the `current.avsc` with a tag `subject1_v5`? But that's after the fact.
Maybe we don't need to store the version in Git. The registry is the source of truth for the version. We can store the schema in Git without version numbers, and then when we register, the registry assigns the version. We can then, in the CI, after successful registration, create a tag in Git with the registry version? But that's not necessary for the workflow.
Let's simplify:
code
- Git stores the current schema for each subject (one file per subject).
- When we merge to main, we run a job that:
a. For each subject, take the current schema file (from the merged commit) and try to register it with the registry (using the subject name and the schema). The registry will assign a version (incrementing).
b. If registration fails (incompatibility), we fail the CI and the merge is broken? But we already merged? We want to check before merge.
So we want to check compatibility before merge. We can do:
code
- On PR, we run a job that:
a. Fetches the current schema for the subject from the registry (the latest version).
b. Takes the changed schema file in the PR and checks compatibility with the current version (and possibly with a set of old versions?).
c. Reports if it's incompatible.
- On merge to main, we run a job that:
a. Registers the new schema (which will be assigned a version).
b. Then we generate documentation and SDKs with the new version.
But note: the compatibility check in the PR might pass, but what if there are other changes in the same subject that are being merged in another PR? We might have race conditions. So it's better to do the compatibility check at the time of registration (which we do in the post-merge job) and if it fails, we have to rollback? That's not ideal.
So we need to do the compatibility check in the post-merge job and if it fails, we cannot register. But then we have merged code that is not compatible? That's a problem.
To avoid that, we can:
code
- Use a lock on the subject during the registration? But that's complex.
Alternatively, we can do:
code
- In the PR, we check compatibility with the current version (from the registry) and also with the version that is expected to be the next one? But we don't know the next version until we register.
Actually, the compatibility check in the registry (when registering) is the definitive one. So we must rely on that.
Proposed:
code
- PR job: check compatibility with the current version (from registry) and if there are multiple PRs at the same time, they might both pass the check but when merged, the second one might fail because the first one has changed the current version.
So we can do:
PR job:
- Fetch the current version of the schema from the registry (let's say version 5).
- Check the new schema in the PR against version 5 (and maybe also against version 4 if we want full compatibility?).
Then, when merging, we do:
- Register the new schema. The registry will check against the current version (which might have been updated by another merge) and if it fails, the registration fails and we don't update the registry.
But then we have merged code that didn't get registered? We can have the merge job fail and then we have to revert the merge? Not ideal.
To avoid this, we can:
code
- Use a sequential merge process (only one PR per subject at a time) by using a lock file or by having a status check that must pass and that does the registration in the PR? But then we are registering in the PR, which we don't want because the PR might not be merged.
Alternatively, we can have a two-phase commit:
code
Phase 1 (PR): Check compatibility with the current version (and any other required versions). This is just a check, not a registration.
Phase 2 (merge):
a. We register the schema. If it fails (because the current version changed since the PR check), then we fail the merge job and the developer must re-run the PR check and fix.
But note: the PR check might have passed with version 5, but by the time of merge, the current version is 6 (from another PR). Then the new schema might be incompatible with 6? Actually, the compatibility check is between the new schema and the current version (at the time of registration). So if the current version changed, the check might fail.
So in the merge job, we must re-check compatibility (which the registry does) and if it fails, we fail the job. Then the developer must update the schema to be compatible with the new current version and re-merge.
This is acceptable because it ensures we never break compatibility.
So:
code
PR job:
- Check compatibility with the current version (from registry) and also with any other versions required by the subject's compatibility setting? Actually, the registry's compatibility setting (BACKWARD, FORWARD, FULL) defines which versions to check against. We should mimic the registry's check? We can use the registry's compatibility endpoint to check without registering?
Confluent Schema Registry has a compatibility endpoint:
POST /compatibility/subjects/{subject}/versions/{version}
This will check the compatibility of a provided schema with a specific version (or the latest). We can use this.
So in the PR job, we can:
curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
--data '{"schema": "..."}' \
http://localhost:8081/compatibility/subjects/MySubject/versions/latest
This returns whether it's compatible with the latest.
But note: the compatibility setting (BACKWARD, etc.) is set on the subject. So we are checking with the same setting.
However, what if the compatibility setting changes? Then we have to check with the new setting? But that's a separate change.
So in the PR job, we check with the current setting and the current latest version.
Then in the merge job, we register (which uses the same setting) and if it fails, we fail.
But what if the setting changes in another PR? Then the PR check might have passed with the old setting, but when we register, the setting might have changed? Actually, the setting is per subject and can be updated. We can check the setting at the time of registration.
So the merge job will use the current setting (which might have been updated by another PR) and the current latest version (which might have been updated by another PR) to check.
This is safe.
Steps in the merge job:
code
For each subject (or for the changed subjects):
1. Get the schema from the Git file (for that subject).
2. Register the schema with the registry (using the subject and the schema).
3. If registration fails (incompatibility), fail the job.
4. If successful, the registry returns the new version.
Then, we generate documentation and SDKs using the new version.
But note: we might have multiple subjects changing in the same PR. We must handle each subject independently.
How to know which subjects changed? We can have a convention: each subject has a directory, and the PR changes files in that directory.
Alternatively, we can have a manifest that lists the subjects and the path to the schema file. Then we only process the subjects that have changed.
And we have a script that, for each directory under schemas, if the schema.avsc file changed, then we process that subject.
We can use git diff to find changed directories.
So the merge job:
code
- Checkout the code.
- Find all directories under `schemas/` that have changes in `schema.avsc` (or any change in that directory?).
- For each changed subject:
a. Read the schema file.
b. Register with the registry (subject name is the directory name).
c. If any fails, break and fail the job.
- If all registered, then generate documentation and SDKs.
But note: the registration might succeed for some and fail for others? We want atomicity? We can do:
code
So let's design for one subject per PR.
Steps for one subject:
code
PR:
- Detect which subject changed (by the directory name).
- Get the current version of that subject from the registry (using the subject's latest version).
- Get the new schema from the changed file.
- Check compatibility using the registry's compatibility endpoint (with the current version and the current compatibility setting).
- Report.
Merge:
- Register the new schema for the subject.
- If successful, note the new version.
- Generate documentation (for all subjects? or just the changed one? We might want to update the entire documentation site for all subjects? But if we have many subjects, we might want to update only the changed one? But the documentation site is a static site that shows all subjects. We can regenerate the entire site on every change? That's acceptable if the number of subjects is not huge.
- Generate SDKs for all languages? Or just the ones that are affected? Typically, the SDKs are generated from all schemas? Because the SDK might include all the domain objects. So we regenerate all.
But note: if we change one subject, we might break compatibility for others? No, because we are only changing one subject and we checked compatibility for that subject. The other subjects are unchanged.
So we can:
- Generate documentation for all subjects (by fetching all current schemas from the registry) and then build the site.
- Generate SDKs for all languages from all schemas (by fetching all current schemas).
But note: the SDK generation might require all schemas to be compatible with each other? Not necessarily, if they are independent subjects. But if they are part of the same data model, then we must have them all in the same generation? Actually, if they are independent, we can generate each separately? But typically, an SDK for a service might include all the domain objects.
So we assume that the SDK generation takes a set of schemas (all subjects) and generates code accordingly.
Therefore, on every change to any subject, we:
- Fetch all current schemas from the registry (for all subjects).
- Generate SDKs and documentation from the entire set.
This ensures that the SDKs are consistent with the current state of the registry.
Summary:
code
We have a Git repository with a directory per subject. Each subject directory has a `schema.avsc` file (or whatever format).
Workflow:
PR:
- For each subject that has a change in its `schema.avsc` file:
a. Get the current version of the subject from the registry (GET /subjects/{subject}/versions/latest).
b. Get the new schema from the file.
c. Check compatibility: POST /compatibility/subjects/{subject}/versions/latest with the new schema.
d. Report the result.
- We can also check the compatibility setting? We don't need to, because the registry will use the current setting.
Merge to main:
- For each subject that changed (as detected by the PR):
a. Register the new schema: POST /subjects/{subject}/versions with the new schema.
b. If registration fails, fail the build.
c. If successful, record the new version.
- Then, regardless of which subjects changed, we:
d. Fetch all current schemas for all subjects from the registry (using the subject's latest version).
e. Generate documentation (for all subjects) and deploy.
f. Generate SDKs (for all subjects) and publish.
- We also send notifications.
But note: step (d) and (e) and (f) are for all subjects. We do them even if only one subject changed? Yes, because the entire set of schemas might have changed (if we changed one subject, the set of latest versions for all subjects is the same except that one subject has a new version). So we must regenerate everything.
Alternatively, we can regenerate only the changed subject's part? But for SDKs, we need all together? Not if they are independent. But if they are independent, we could generate per subject and then combine? That's more complex.
So we assume we generate the entire SDK and documentation from the full set of schemas.
Implementation:
code
Tools:
code
We can write a Python script that:
For PR:
- Uses the GitHub API (or GitLab, etc.) to get the changed files.
- Extracts the subject names (from the directory names under `schemas/`).
- If more than one subject is changed, fail the PR check with a message: "Only one subject per PR allowed."
- For the changed subject:
a. Get the current schema version from the registry (using the subject's latest version).
b. Get the new schema from the changed file.
c. Check compatibility using the registry's compatibility endpoint.
d. Report.
For merge:
- Checkout the code.
- Find the changed subject (should be only one).
- Register the new schema for that subject.
- If registration fails, fail the build.
- If registration succeeds:
- Fetch all current schemas for all subjects (from the registry).
- Generate documentation and SDKs.
- Deploy documentation and SDKs.
We can use the Confluent Schema Registry REST API.
Documentation Generation:
code
We can use:
For Avro: `avro-tools` to convert to HTML? Actually, `avro-tools` can compile to Java, but for HTML we can use a custom template? Or use a tool like `avro-schema-ref` (https://github.com/alanxz/avro-schema-ref) to generate markdown.
Alternatively, we can use a tool that generates a static site from multiple schemas? We might have to write our own.
Let's assume we generate a markdown file per subject and then have an index.
Steps:
- For each subject, get the current schema (Avro) and convert it to markdown (using a template that lists the fields, types, descriptions, etc.)
- Then, combine all markdown files into one big markdown file or generate an HTML site with a page per subject.
We can use a static site generator (like MkDocs) and generate a page per subject.
For Protobuf: we can use `protoc-gen-doc` to generate markdown.
We can support multiple schema types? We can have a configuration that says which type each subject is? Or we can detect by file extension.
Let's assume we only support Avro for now.
SDK Generation:
code
We can use `avro-tools` to generate Java classes, and then for other languages we use other tools.
But we want to generate SDKs for multiple languages. We can have a directory per language and run the appropriate generator.
Steps:
- Fetch all current schemas (for all subjects) from the registry.
- For each language (Java, Python, Go, etc.):
a. Create a temporary directory.
b. Write each schema to a file (with the subject as the filename?).
c. Run the code generator (e.g., for Java: `avro-tools` with `compile schema` and output to a directory).
d. Then package (e.g., Maven, set up pom.xml) and publish to a repository.
But note: the generated code for each subject might be in a separate package? We have to decide on the package structure.
We can have:
- One SDK per language that includes all subjects? Or one SDK per subject?
Typically, an SDK for a service includes all the domain objects (which are defined by the schemas). So we generate one SDK per language that includes all the generated classes for all subjects.
So we generate:
- A Maven project for Java that includes all the generated classes in one package (or per subject package) and then we build and deploy to a Maven repository.
We can use a build tool (like Maven, Gradle, or just `avro-tools` and then package the classes).
For Python, we generate a package with modules per subject.
This is complex and might be out of scope for the design.
Given the complexity of SDK generation, we might consider using a tool that does multi-language code generation from Avro schemas, like avro4s (for Scala/Java) and then for other languages we use other tools. But we have to orchestrate.
Alternatively, we can use a schema registry that supports multiple serialization formats and then generate SDKs per format?
Since the question is about designing the workflow, we can leave the SDK generation as an abstract step.
Conclusion:
code
We design a workflow that:
- Stores schemas in Git (one file per subject, with the file named `schema.avsc` in a directory named after the subject).
- Enforces one subject change per PR.
- In PR, checks compatibility of the changed subject with the current version in the registry.
- On merge, registers the changed subject (and fails if incompatible).
- Then, fetches all current schemas and generates documentation and SDKs for all languages.
We use the Confluent Schema Registry (or compatible) and its compatibility endpoint.
We write a custom script for the PR check and the merge job.
We host documentation on a static site (e.g., GitHub Pages) and publish SDKs to package repositories.
This is a simplified but workable design.
Let's write the steps in more detail:
code
Note: The above assumes the schema registry is accessible at http://schema-registry:8081 and we have authentication? We might need to add authentication.
Also, we are using jq and curl. We must have these installed.
This is a basic design. In practice, we would need to handle:
code
- Authentication to the schema registry.
- Error handling and retries.
- More efficient fetching of schemas (the registry might have an endpoint to get all subjects and their latest schema? Confluent has `/subjects` and then for each subject we get the latest version. But that's multiple calls. We can use `/subjects?subjectMetadata=true`? Not sure.
- For SDK generation, we might need to handle dependencies between schemas (if one schema references another). We must fetch all schemas and then generate in the correct order? Avro has a way to handle that by having all schemas in the same compilation.
- We might want to version the SDKs appropriately. We can use the version of the latest schema? Or we can use a versioning scheme independent of the schema version? Typically, the SDK version is independent and we use semantic versioning. We can have a version file in Git that we increment? Or we can use the date?
Given the complexity, we might leave SDK generation as a separate topic.
Final Note:
code
This design is for a single format (Avro) and a single registry (Confluent). We can extend to other formats and registries by abstracting the compatibility check and code generation.
We have outlined a workflow that:
code
- Stores schemas in Git.
- Enforces one subject per PR.
- Checks compatibility in PR.
- Registers on merge.
- Generates documentation and SDKs for all subjects on every successful merge.
This ensures that the registry is the source of truth for the current versions, and the documentation and SDKs are always in sync with the registry.
We must also consider:
code
- How to handle deletions of subjects? We might not allow deletion? Or we have a separate process.
- How to handle compatibility setting changes? That would be a separate PR that changes the setting for a subject. We would have to adjust the PR check and merge job to handle that? We can treat the setting change as a change to the subject? Actually, the setting is per subject and can be updated via the registry's API. We can have a separate process for that? Or we can have a file in Git that stores the setting? Then when we change the setting, we update the file and then in the merge job we update the setting in the registry? And then we don't change the schema? But then the compatibility check in the PR would use the old setting? So we must update the setting first?
This is getting too complex for the initial design.
We can start with:
code
- Schemas in Git.
- Compatibility setting in the registry (manually set) and we don't version the setting in Git? Then when we change the setting, we do it manually in the registry and then the PR check uses the new setting? That's acceptable.
Alternatively, we can store the setting in the Git repository (per subject) and then in the PR and merge job, we update the setting in the registry when we change the setting? But then we have two changes (schema and setting) that are independent?
Given the complexity, let's assume the compatibility setting is managed separately and is not part of the Git repository.
This design is a starting point and would need to be adjusted for real-world use.
I'll design a comprehensive message schema registry workflow with compatibility scanning, documentation, and client SDK updates. This is a critical system for microservices architectures.
BACKWARD: New can read old data (safe for consumers)
FORWARD: Old can read new data (safe for producers)
FULL: Both BACKWARD and FORWARD
NONE: No checks (dangerous)
Automated Checks:
python
# Pseudocode for compatibility validation
def validate_compatibility(new_schema, subject, level):
# 1. Fetch latest N versions from registry
previous_versions = registry.get_versions(subject, limit=10)
# 2. For each previous version:
for version in previous_versions:
if not is_compatible(new_schema, version, level):
return {
"compatible": False,
"conflicts": [
{
"version": version.id,
"message": f"Field 'email' changed from required to optional",
"breaking": True
}
]
}
# 3. Check against compatibility matrix (cross-subject)
if subject in cross_subject_dependencies:
for dep_subject in dependencies[subject]:
# Check that changes don't break dependent schemas
validate_cross_subject_compatibility(new_schema, dep_subject)
return {"compatible": True}
Integration Points:
Pre-commit hooks: Local validation
PR checks: GitHub/GitLab/Bitbucket integration
CI/CD pipeline gates: Block deployment on incompatibility
Slack/Teams: #schema-changes channel with rich cards
Email: Digest for stakeholders
Jira/Linear: Auto-create tickets for breaking changes
Dashboard: Real-time schema registry UI
API: Webhook for custom integrations
Governance Policies:
Approval Workflows:
Breaking changes require 2+ reviewer approval
Automated linting (schema style guide)
Security review for PII fields
Deprecation Process:
yaml
deprecation_policy:
warning_period: "90d" # Warn before removal
removal_period: "180d" # Auto-remove after
grace_period: "30d" # After removal, still accessible
New requirements (new formats, languages, regulations)
Performance metrics (build times, adoption rates)
- Register all and if any fails, we try to rollback? That's complex.
Alternatively, we can register one by one and if one fails, we stop and fail. Then the ones that were registered are already in the registry? We don't want partial updates.
So we must do:
Option 1: Register all in a transaction? The registry doesn't support that.
Option 2: We register in order and if one fails, we must delete the ones we already registered? That's risky.
Alternatively, we can register all in a test registry first? But then we have to copy to the real registry? Not good.
So we might have to accept that we might have partial registration? But then the registry would have a new version for some subjects and not for others, and the documentation and SDKs would be generated for the ones that succeeded.
That's not acceptable because we want the whole PR to be atomic.
How about:
We do a dry-run compatibility check for all subjects first (using the compatibility endpoint) and then if all pass, we register all? But note: between the dry-run and the registration, the registry might have changed? So we need a lock?
Alternatively, we can:
1. For each subject, get the current version and compatibility setting.
2. Check compatibility of the new schema with the current version (using the setting) without registering.
3. If all pass, then we register each one. But again, between step 2 and step 3, the current version might change.
We need a way to do the check and register atomically? The registry's register endpoint does the check and register in one step. So if we call register and it fails, we know that at the moment of the call, it was incompatible. But if we do multiple registers, we cannot do them atomically.
So we have to accept that if we have multiple subjects, we might have partial registration?
Alternatively, we can process one subject at a time and if one fails, we revert the previous ones? But then we are deleting versions from the registry? That's not typical.
Given the complexity, maybe we should only allow one subject change per PR? That would simplify.
But if we want multiple subjects, we can:
- Register each subject and if one fails, we mark the whole build as failed and leave the registry with the ones that succeeded. Then we have an inconsistent state.
- We can have a script that, on failure, deletes the newly registered versions? But that's dangerous because other processes might have already used the new version.
So perhaps we design the system to allow multiple independent subjects and if one fails, we fail the build and leave the registry with the ones that succeeded. Then we must manually revert the ones that succeeded? Not ideal.
Alternatively, we can:
- In the PR, we check each subject independently. Then when merging, we register each subject independently and if one fails, we fail the build and then we must manually delete the ones that succeeded? Or we can have a cleanup step that deletes the new versions if the build fails? But note: the build might fail after some registrations and then we don't want to delete because maybe the failure is in the documentation generation? We only want to delete if the registration failed? But if the registration fails for one, we don't want to keep the ones that succeeded? That's a design decision.
Let's assume we want atomicity for the whole PR (all subjects). Then we must:
- Register all subjects in a way that if any fails, we rollback all.
We can do:
Step 1: For each subject, check compatibility (using the compatibility endpoint) and store the current version and setting.
Step 2: Then, for each subject, register. If any registration fails, then for each subject that we successfully registered, we delete that version? But note: the version is assigned by the registry and we don't know it until we register. And if we delete, we are removing the version from the registry? That might break existing consumers that already fetched the schema? But if we are in the same CI job and the version was just registered, maybe it's safe? But if the version was registered and then we generate documentation and SDKs and publish, then we have to rollback the documentation and SDKs too.
This is getting very complex.
Given the complexity, I propose:
- We allow multiple subjects per PR, but we process them independently and if one fails, we fail the build and leave the registry with the ones that succeeded. Then we must manually revert the successful ones? Or we can have a manual step to revert.
Alternatively, we can:
- In the PR, we check each subject independently. Then in the merge job, we register each subject independently and if one fails, we fail the build and then we do not generate documentation and SDKs for any? But the registry has some new versions. We can have a post-failure cleanup that deletes the new versions? But only for the subjects that were in this PR? We can store the list of subjects and the new version numbers (from the registration response) and if the build fails, we delete them.
But note: if the build fails after registration and before we record the new versions, we might not know which ones to delete.
So we must record the new version numbers immediately after registration. We can do:
For each subject:
- Register and get the new version.
- Write the subject and new version to a file (or environment variable) and then if the build fails, we read that file and delete those versions.
But if the build fails after some registrations and before we write the file? We can do:
- Register and immediately record the version in a temporary file (or array in memory) and then if the build fails at any point, we use that record to delete.
However, if the build fails after we recorded but before we delete? We might leave orphaned versions? But that's okay because they are not used? They are just old versions that were registered by mistake.
But note: the registry might have a limit on the number of versions? So we don't want to leave junk.
So we can:
- In the merge job, we do:
subjects_to_rollback = []
for each subject in changed_subjects:
response = register_schema(subject, schema)
if response is success:
subjects_to_rollback.append((subject, response.version))
else:
# registration failed, break and then rollback the ones we already registered?
# But we don't want to rollback yet because we might have a partial failure and then we rollback and then the build passes?
# Actually, we want to rollback only if the entire build fails?
Then, we continue with documentation and SDK generation. If any of those steps fail, we rollback all the registrations in subjects_to_rollback.
But note: if the registration fails for one subject, we break and then we haven't registered the rest. Then we rollback the ones that succeeded and fail the build.
This seems acceptable.
However, we must be cautious: if the registration succeeds for a subject and then we generate documentation and SDKs, and then the SDK publishing fails, we rollback the registration. But what if someone already used the new version in the meantime? The rollback would delete the version and break them? But since we are in the same CI job and the version was just registered, it's unlikely that anyone is using it yet? But possible.
So we must design the system to be able to rollback safely? Or we decide that we don't rollback and we fix manually? Given the complexity, maybe we don't do automatic rollback and we just fail the build and let the developer fix and re-merge. Then we have to manually delete the partially registered versions? Not ideal.
Given the trade-offs, I think we should:
- Only allow one subject per PR to avoid this complexity.
But if we must support multiple subjects, then we accept that we might have partial registration and we have a manual cleanup process? Or we design the registry to allow soft deletion? Not typical.
Alternatively, we can change the workflow:
- We don't register in the merge job. Instead, we have a separate process that runs after the merge (like a batch job) that reads all the changed schemas from Git and registers them in order? But then we have the same atomicity problem.
Given the complexity, I will assume we are only changing one subject per PR. Then the atomicity is per subject and we can:
PR job:
- Check compatibility for the changed subject.
Merge job:
- Register the changed subject.
- If fails, fail the build.
- If succeeds, generate documentation and SDKs.
This is simpler.
We need:
- A script that, given a Git repository and a branch (or commit), does:
PR:
- Find changed subjects (by looking at changed files in the PR).
- For each changed subject, get the current schema from the registry and the new schema from the file, and check compatibility.
Merge:
- Find changed subjects.
- For each changed subject, register the new schema.
- If all registrations succeed, then:
- Fetch all schemas (for all subjects) from the registry.
- Generate documentation and SDKs.
- Publish documentation and SDKs.
- If any registration fails, fail the build and (optionally) rollback the successful registrations? We decided not to for simplicity? But if we have multiple subjects, we might have partial registration. So we might want to rollback the successful ones if any fails? Let's do:
In the merge job:
successful_registrations = []
for subject in changed_subjects:
try:
version = register(subject, schema)
successful_registrations.append((subject, version))
except:
# rollback all in successful_registrations
for subj, ver in successful_registrations:
delete_version(subj, ver) # but note: we might not have delete permission? And is it safe to delete?
exit(1)
But again, delete might break if someone already used the version? But we just registered it and the build is failing, so it's unlikely. But possible.
Alternatively, we don't delete and we leave it and then we have to manually revert? Given the low probability, maybe we don't delete and we just fail the build and let the developer fix and then re-merge? Then we have a new version number for the subject? And the old (failed) version remains? That's not ideal because we have a gap in the version numbers? But the registry version numbers are per subject and are consecutive. If we fail to register version 5, then the next successful registration will be version 6? Or will it be 5? The registry assigns incrementally, so if we try to register and fail, the version number is not used. So if we fail at version 5, then the next successful registration will be version 5? Actually, the registry assigns the next version when you successfully register. So if we fail, the version number is not used. So we don't have gaps? But if we delete a version, then we have a gap? We don't want to delete because that creates a gap and also might break existing consumers that have that version.
So we should not delete.
Therefore, if we have partial registration, we leave the successfully registered versions and fail the build. Then the developer must fix the incompatibility and re-merge. The next registration for that subject will get the next version (so we skip the failed one). That's acceptable.
But note: the documentation and SDKs are generated from all subjects, including the newly registered ones? We only generate if all registrations succeed? Because we want the documentation and SDKs to reflect the entire set of changes? But if we have two subjects and one fails, then we have one new version in the registry and the other is old. Then when we generate documentation and SDKs, we get a mix? We don't want that because the PR intended to change both, but one failed. So we should generate only if all registrations succeed? Yes.
So:
- If any registration fails, we fail the build and do not generate documentation/SDKs.
Then the next time we merge, we only have the successful ones? But the PR might have changed two subjects and one is incompatible. Then the developer must fix the incompatible one and re-merge. Then we will have two registrations: the one that succeeded previously (and is already in the registry) and the fixed one. But the one that succeeded previously is already in the registry and we didn't generate documentation for it? Because the first merge failed after registration? So we have a new version in the registry but no documentation? That's a problem.
So we must generate documentation and SDKs for the successfully registered ones even if the entire PR fails? But then the documentation and SDKs would be out of sync with the intention of the PR (which was to change two subjects).
This is a difficult problem.
Let's re-think:
We want the entire PR to be atomic: either all subjects are updated (registered, documented, SDKs published) or none.
So we must:
- Check compatibility for all subjects first (without registering) and then if all pass, register all. But again, between check and register, the registry might change.
We need a way to lock the subjects? Not really possible in a distributed system.
Alternatively, we can:
- Register the schemas in a separate "staging" registry? Then if all succeed, we promote them to the real registry? But that's complex.
Given the complexity, I think we should:
- Only allow one subject change per PR. Then we don't have the atomicity problem across subjects.
So we will enforce that a PR changes only one subject.
Then the workflow is:
PR:
- Check compatibility for the changed subject.
Merge:
- Register the changed subject.
- If registration fails, fail the build and do nothing else.
- If registration succeeds, then generate documentation and SDKs for all subjects (including the new version for the changed subject and the old versions for others).
This way, we don't have partial registration because we only change one subject.
But note: what if the PR changes multiple files in the same subject? That's fine because it's one subject.
So we enforce: a PR must only change one subject (i.e., one directory under `schemas/`).
PR Check (GitHub Action):
- name: Check changed subjects
run: |
changed_files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }})
subjects=$(echo "$changed_files" | grep '^schemas/' | cut -d/ -f2 | sort -u)
if [ $(echo "$subjects" | wc -l) -gt 1 ]; then
echo "Only one subject per PR allowed. Changed subjects: $subjects"
exit 1
fi
subject=$(echo "$subjects" | head -n1)
if [ -z "$subject" ]; then
echo "No subject changed."
exit 0
fi
# Get the current schema version for the subject
current_version=$(curl -s -X GET -H "Accept: application/vnd.schemaregistry.v1+json" \
http://schema-registry:8081/subjects/${subject}/versions/latest | jq -r '.version')
# Get the new schema from the file
new_schema=$(cat schemas/${subject}/schema.avsc)
# Check compatibility
compatibility=$(curl -s -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
-d "{\"schema\": $new_schema}" \
http://schema-registry:8081/compatibility/subjects/${subject}/versions/${current_version} | jq -r '.compatibility')
if [ "$compatibility" != "COMPATIBLE" ]; then
echo "Schema for subject $subject is not compatible with current version $current_version."
exit 1
fi
Merge Job (GitHub Action):
- name: Register changed subject
run: |
changed_files=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }})
subjects=$(echo "$changed_files" | grep '^schemas/' | cut -d/ -f2 | sort -u)
if [ $(echo "$subjects" | wc -l) -gt 1 ]; then
echo "Only one subject per PR allowed. Changed subjects: $subjects"
exit 1
fi
subject=$(echo "$subjects" | head -n1)
if [ -z "$subject" ]; then
echo "No subject changed."
exit 0
fi
new_schema=$(cat schemas/${subject}/schema.avsc)
response=$(curl -s -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
-d "{\"schema\": $new_schema}" \
http://schema-registry:8081/subjects/${subject}/versions)
if [ $? -ne 0 ] || [ "$(echo $response | jq -r '.error_code')" != "null" ]; then
echo "Failed to register schema for subject $subject: $response"
exit 1
fi
new_version=$(echo $response | jq -r '.id') # Note: in Confluent, the response has 'id' for the schema version? Actually, the response for register is:
# {"id": 1} for the schema? Wait, the subject version endpoint returns:
# {"id": <schema_id>}? Actually, the response of POST /subjects/{subject}/versions is:
# {"id": <schema_id>} (the schema id, not the version)
# And we want the version? We can get the version from the subject's versions? Alternatively, we can get the latest version for the subject after registration?
# Let's get the latest version for the subject after successful registration.
new_version=$(curl -s -X GET -H "Accept: application/vnd.schemaregistry.v1+json" \
http://schema-registry:8081/subjects/${subject}/versions/latest | jq -r '.version')
echo "Registered new version $new_version for subject $subject"
- name: Generate documentation
run: |
# Fetch all current schemas for all subjects
subjects=$(curl -s -X GET http://schema-registry:8081/subjects | jq -r '.[]')
for subject in $subjects; do
version=$(curl -s -X GET -H "Accept: application/vnd.schemaregistry.v1+json" \
http://schema-registry:8081/subjects/${subject}/versions/latest | jq -r '.version')
schema=$(curl -s -X GET -H "Accept: application/vnd.schemaregistry.v1+json" \
http://schema-registry:8081/subjects/${subject}/versions/$version | jq -r '.schema')
echo "# $subject (v$version)" > docs/$subject.md
echo "\`\`\`json" >> docs/$subject.md
echo "$schema" >> docs/$subject.md
echo "\`\`\`" >> docs/$subject.md
done
# Then use MkDocs to build the site and deploy.
- name: Generate and publish SDKs
run: |
# For each language, generate and publish.
# We assume we have a script for each language.
# Example for Java:
# mkdir java-sdk
# for each subject schema, write to a file and then run avro-tools to compile to java-sdk/src/main/java
# then set up pom.xml and run mvn deploy.
# This is complex and language-specific.
This is a simplified example.