Isolate the monotonic int32 ID allocator behind a single Raft/PostgreSQL consensus writer while serving immutable GET /schemas/ids/{id} reads from stateless edge pods and client LRU caches.
TL;DR30-second simplified summary▾
Split control-plane writes (Raft/Postgres serializable ID allocation + compatibility DAG check) from data-plane reads (edge LRU caches + singleflight).
R & E — Scale Physics & Consistency Boundaries.
Coupled Monolith vs. Read/Write Split
Read traffic (GET /schemas/ids/{id}) hits 500k req/s during cluster-wide deployments, but every id -> schema mapping is immutable. Write traffic (POST /subjects/{s}/versions) is low (~200 req/s) but linearizable: if two regions ever allocate ID 1042 to different schemas, downstream Parquet/Iceberg pipelines suffer silent data corruption.
Storage footprint is tiny: 250,000 schema versions × 4 KB average = ~1 GB total, fitting completely in RAM across every Registry pod.
S — Canonical Deduplication & Storage Schema.
Compacted _schemas Topic & Postgres Metadata Store
Before allocating a new ID, the Registry parses the Avro/Protobuf AST into a canonical form (stripping comments and insignificant whitespace) and computes canonical_sha256. An idempotent INSERT ... ON CONFLICT (canonical_sha256) DO NOTHING ensures identical schemas always return the same schema_id, while subject-version bindings map (subject, version) -> schema_id.
import hashlib
import json
def canonical_schema_hash(schema_dict: dict) -> str:
"""Normalize Avro/JSON schema keys deterministically before hashing."""
normalized = json.dumps(schema_dict, sort_keys=True, separators=(",", ":"))
return hashlib.sha256(normalized.encode("utf-8")).hexdigest()
H & A — High-Level Architecture & Compatibility Gate.
Distributed Schema Registry Architecture
- Problem
- How should a multi-region Schema Registry allocate 32-bit schema IDs without split-brain?
- Naive Solution
- Run active-active multi-master writes in US-East and EU-West with independent auto-increment sequences.
- Why It Fails
- Two concurrent registrations in different regions can mint the same int32 ID for incompatible schemas, corrupting cross-region replicated topics.
- Alternatives
- Single-leader Raft / primary Postgres allocator with regional read replicas,Disjoint per-region ID ranges (e.g., region_id << 28 | seq),128-bit UUIDs instead of 32-bit int32 IDs
- Engineering Decision
- Use a single-leader Raft/Postgres consensus allocator for writes (200 req/s) paired with regional read-only replicas and edge LRU caches for reads (500k req/s).
- Trade-Offs
- Cross-region schema registration incurs 1 WAN RTT (~70-120 ms) during CI/CD, which is negligible compared to zero-collision safety.
A team wants to rename user_id to account_id in an Avro schema under FULL_TRANSITIVE compatibility. What happens?
Every registration request passes through a stateless Registry API pod that fetches the subject's prior versions and runs the compatibility checker:
- BACKWARD: New reader can read old data (new fields must have defaults; deletions allowed).
- FORWARD: Old reader can read new data (deletions only if old schema had defaults; additions allowed).
- FULL: Both backward and forward simultaneously.
Non-transitive modes only check v_n against v_{n-1}, which can silently break v_{n-2} if a field is deleted and re-added with a different type; production platforms default to BACKWARD_TRANSITIVE or FULL_TRANSITIVE.
D — Zero-Downtime Expand-Contract & Backfill Pipeline.
Expand-Contract Backfill Lifecycle
When a breaking change is unavoidable (e.g., amount_cents int32 -> amount_micros int64), use the 4-phase Expand-Contract pattern:
- Expand: Add optional
amount_microswith defaultnull(BACKWARD/FULL compatible). - Dual-Write: Producers populate both
amount_centsandamount_micros; a bounded backfill job rewrites historical topic/table partitions. - Migrate Readers: Consumers switch to reading
amount_microswith fallback toamount_cents. - Contract: Once retention expires on old
v1records, deprecateamount_cents.