Skip to main content
← PROBLEMS / SYSTEM-DESIGN
An end-to-end domain learning journey

Design a Distributed Schema Registry
& Zero-Downtime Backfill Platform.

A production schema registry must separate read-heavy immutable ID lookups (99.99% cacheable) from strongly consistent schema registration and compatibility validation.
Start the journey4 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

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).

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
5-Byte Magic Wire Framing
Producers prepend 0x00 + big-endian int32 schema ID to every event so consumers resolve schemas by ID.
Raft / Single-Leader Consensus
Guarantees two concurrent registrations of different schemas never receive the same int32 ID during a network partition.
Full walkthrough · 4 stages
01
L4 · FUNDAMENTALS

R & E — Scale Physics & Consistency Boundaries.

Coupled Monolith vs. Read/Write Split

Before · Uncached Registry DB
After · Immutable Read Plane + Consensus Write Plane

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.

02
L4 · FUNDAMENTALS

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()
03
L5 · APPLIED DESIGN

H & A — High-Level Architecture & Compatibility Gate.

Distributed Schema Registry Architecture

Producers / CIStateless API PodsConsensus ID LeaderPostgres / _schemasPOST /versionsCompat OK -> Alloc IDLinearizable commit
Engineering Decision Framework
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.
Decision Exercise · Make the Call

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.

04
L5 · APPLIED DESIGN

D — Zero-Downtime Expand-Contract & Backfill Pipeline.

Expand-Contract Backfill Lifecycle

v2 deployed100% backfill done0 v1 readers activeEXPAND_SCHEMADUAL_WRITE_BACKFI…MIGRATE_READERSCONTRACT_OLD_FIELD

When a breaking change is unavoidable (e.g., amount_cents int32 -> amount_micros int64), use the 4-phase Expand-Contract pattern:

  1. Expand: Add optional amount_micros with default null (BACKWARD/FULL compatible).
  2. Dual-Write: Producers populate both amount_cents and amount_micros; a bounded backfill job rewrites historical topic/table partitions.
  3. Migrate Readers: Consumers switch to reading amount_micros with fallback to amount_cents.
  4. Contract: Once retention expires on old v1 records, deprecate amount_cents.
Recommended Learning Flow