Surviving MatchException: The Sealed Hierarchy Landmine in Java 21+ Microservices
Teams are aggressively migrating event schemas to Java 21+ sealed interfaces and record patterns, celebrating the death of boilerplate visitor code right until their first rolling deployment triggers an outage. When a producer publishes a new permitted subtype to a shared artifact, downstream consumers running the previous client version fail at runtime with a catastrophic java.lang.MatchException .
Heads up: if you want to see these patterns applied to real interview problems, javalld.com has full machine coding solutions with traces.
Why Most Developers Get This Wrong
Confusing compile-time exhaustiveness with binary compatibility: javac validates exhaustive pattern switches against the hierarchy known at compile time , omitting a fallback branch and emitting a synthetic MatchException invocation if an unexpected subtype arrives at runtime.
Sharing domain models over the wire: Exposing internal sealed hierarchies…