Drafting schema evolution policy
I need to provide a schema evolution policy that includes compatibility rules, reserved fields, and data upgrades. They probably want a structured document that covers goals, definitions, and a compatibility matrix. I'll find examples related to Protobuf, Avro, and JSON.
In outlining data upgrades, I'd include methods like in-place migration and version bumping. I should also prepare checklists for processes like reviews and schema registries. It’s vital to mention how to handle incompatible changes and the need for audit logs as well.Creating guidelines for default values
I need to craft guidelines for default values, so I'll structure it clearly with headings and bullet points. For Protobuf, I should emphasize that field numbers can’t change, only optional fields can be added, and types shouldn’t be altered. If they’re using JSON, I'll note that consumers must ignore unknown fields and that fields need to be optional.
For Avro, it’s essential to use a schema registry with backward compatibility. I’ll outline data upgrade steps and the reserved fields policy. Finally, I want to include compatibility tiers and describe the process for maintaining a changelog.
Schema evolution policy
1) Scope and goals
This policy applies to any persisted or exchanged schema (events, API payloads, DB records, files), including Protobuf/Avro/JSON, and to the code that produces/consumes it.
Goals:
- Allow independent deployment of producers and consumers.
- Prevent data loss/corruption during rolling upgrades.
- Make breaking changes explicit, rare, and planned.
2) Compatibility rules
2.1 Definitions (producer/consumer view)
- Backward compatible: New consumers can read data produced by old producers.
- Forward compatible: Old consumers can read data produced by new producers (typically by ignoring unknown fields).
- Full compatible: Both backward and forward compatible across the supported window.
2.2 Required compatibility level
Default requirement: Full compatibility across the supported upgrade window (e.g., “current prod version and previous N versions” or “last 2 releases”).
Breaking changes require:
- a major version bump (or new topic/endpoint/table), and
- an explicit data upgrade plan (Section 4).
2.3 Safe vs breaking changes (generic rules)
Safe (compatible)
- Add a new optional field with a sensible default.
- Add a new enum value (with caveats below).
- Relax validation (accept a broader set of values), while continuing to produce the old valid subset during the window.
- Add a new message variant that old readers can ignore (e.g., new
oneof arm / new union branch) only if old readers are specified to ignore unknown variants.
Breaking (incompatible)
- Remove a field that old readers require.
- Change field meaning/semantics in a way that violates old assumptions (even if the wire format doesn’t change).
- Change type/format in-place (string→int, int→bytes, milliseconds→seconds, etc.).
- Rename a field in formats where name is the key (e.g., JSON) without aliasing.
- Tighten validation such that previously valid data becomes invalid for readers/writers in the window.
- Reuse an old field identifier (number/name) for new meaning.
2.4 Format-specific rules (apply as relevant)
Protobuf (proto2/proto3)
Must:
- Never change field numbers.
- Never change wire type (e.g.,
int32 ↔ string is breaking).
- Field renames are allowed (name is not on the wire), but keep comments and reserve old names to prevent confusion.
- When deleting fields: reserve field numbers and names (see Section 3).
Be cautious:
- Enum changes: adding values is usually OK, but old code may treat unknown enum values as default/0; handle explicitly. Never change meaning of existing numeric values.
Avro (with schema registry)
- Choose and enforce a registry mode (commonly BACKWARD_TRANSITIVE or FULL_TRANSITIVE).
- When adding a field: provide a default to maintain backward compatibility.
- Never change a field type without a documented promotion rule and compatibility verification.
- Don’t remove fields unless you’re also handling old data (aliases/defaults) and registry compatibility allows it.
JSON (or other name-keyed formats)
- Consumers must ignore unknown fields.
- New fields must be optional for the entire support window.
- Renames must be done via aliasing (write both fields, read either) until the old field is retired.
- Avoid changing numeric encoding/precision (e.g., number ↔ string) without dual-read/dual-write.
3) Reserved fields policy (prevent accidental reuse)
3.1 When to reserve
Reserve immediately when:
- a field is deleted,
- a field is repurposed (treated as delete + add),
- an enum value is removed.
3.2 What to reserve
- Protobuf: reserve both the field number(s) and field name(s).
- Avro/JSON: keep a “tombstone” entry in schema docs/changelog:
- field name
- former type
- semantics
- deprecation/removal date
- replacement field (if any)
3.3 Rules
- Never reuse reserved identifiers (numbers/names) for any new meaning.
- Keep reserved lists indefinitely (or at least for the lifetime of stored data).
4) Data upgrades (migrations) policy
4.1 When a data upgrade is required
You need a migration plan when:
- making a breaking schema change,
- changing semantics that require rewriting existing stored records,
- tightening validation in a way old data may violate,
- changing partition keys/index keys/primary keys.
4.2 Supported upgrade strategies
Pick one, document it, and execute with verification.
A) Expand/contract (recommended for most systems)
- Expand: add new fields/code paths; keep old fields; dual-read.
- Backfill: populate new fields from old data.
- Dual-write: write both old and new fields (if needed) during window.
- Contract: stop writing old field; after window, remove old field and reserve identifiers.
B) Lazy migration (“read repair”)
- On read: if record is old, upgrade in memory and write back upgraded form.
- Requires idempotency, careful load control, and monitoring for completion.
C) Offline rewrite
- Batch job rewrites all historical data to the new schema.
- Requires correctness checks, reprocessing plan, and rollback strategy.
D) New store/topic + cutover
- Write new schema to a new topic/table/bucket.
- Consumers migrate to new source; old source is retired later.
- Best when true incompatibility is unavoidable or blast radius must be limited.
4.3 Rollout rules (to support rolling deploys)
- Deploy order for compatible changes: consumers first, then producers.
- For changes requiring dual-write: deploy dual-read consumers, then dual-write producers, then complete backfill, then remove old paths.
- Keep telemetry:
- % messages/records using new fields
- parse/validation errors by schema version
- backfill progress and lag
- “unknown field” rates (if measurable)
4.4 Verification and rollback
- Every migration must include:
- invariants (what must remain true),
- sampling-based validation (old vs new),
- rollback plan (how to stop writing new format, how to restore old reads),
- data retention implications.
5) Deprecation policy
- Mark fields as deprecated before removal, with:
- replacement field
- earliest removal version/date
- migration guidance
- Maintain deprecation for at least the supported compatibility window.
6) Versioning and governance
- Maintain a schema changelog per schema: date, author, compatibility assessment, migration notes.
- Enforce changes via CI:
- schema compatibility checks (registry or tooling),
- “no field number reuse” checks (protobuf),
- contract tests (golden payloads from prior versions).
- Breaking changes require explicit review/approval and a documented migration (Section 4).
If you tell me your serialization format(s) (Protobuf/Avro/JSON), deployment model (rolling? canary?), and the “supported window” you want (e.g., 2 releases / 30 days), I can tailor this into a concrete, enforceable policy with exact allowed/disallowed diffs and CI checks.