Post

Handling Schema Evolution Safely

Handling Schema Evolution Safely

Schema changes are a top source of production incidents in distributed systems. Safe evolution requires backward and forward compatibility across both APIs and data stores.

Expand and Contract Pattern

Use two phases:

  • Expand: add new fields or tables without removing old ones.
  • Contract: remove deprecated fields after all clients migrate.

Backward and Forward Compatibility

  • Backward compatible: new servers can read old data.
  • Forward compatible: old servers can ignore new fields.

For APIs, add optional fields rather than changing types or required fields.

Event Schema Evolution

For event-driven systems, use a schema registry and versioned events. Prefer additive changes and avoid renaming fields.

Spring Boot Example: Safe Field Addition

1
2
3
4
5
public class CustomerProfile {
    private String id;
    private String email;
    private String phoneNumber;
}
1
2
ALTER TABLE customer_profile
ADD COLUMN phone_number VARCHAR(32);

Testing Compatibility

  • Contract tests for API changes.
  • Backfill verification for new fields.
  • Canary deploys with dual-read logic.

Summary

Schema evolution must be deliberate. Expand-and-contract, compatibility testing, and event schema versioning help avoid breaking changes while enabling continuous delivery.

This post is licensed under CC BY 4.0 by the author.