Video in progress
What breaks when a new value is added to an enum passed between services, and how do you design for it?
// billing-service (the consumer), the old version
enum PaymentMethod { CARD, CASH }
record PaymentEvent(String id, PaymentMethod method, long amount) {}
// orders-service added PaymentMethod.QR in its new version and
// sent {"id":"p-9","method":"QR","amount":5000} to Kafka
A consumer that doesn't know the new value yet either fails in deserialization or lands in an unexpected state in an exhaustive switch. Since producers and consumers are deployed at different times, you need tolerance for an "unknown" value, to update consumers first, and to version the contract.
📖 Answer
By default Jackson can't turn QR into a PaymentMethod and throws InvalidFormatException. In a Kafka consumer this is usually a "poison pill": the message is read again and again, the partition stalls, and lag grows. One enum value stopped the whole of billing.
Other forms of it:
- in Protobuf, an unknown enum value is preserved in proto3, but in Java it arrives as
UNRECOGNIZED: if the code doesn't expect it, theswitchfalls into default or throws an NPE; - in a Java 21 exhaustive switch with no
default, if old compiled code runs with a new enum, you getMatchException(notIncompatibleClassChangeErrorfor an enum, exactly this).
Design rules:
- The consumer should be tolerant.
enum PaymentMethod {
CARD, CASH,
@JsonEnumDefaultValue UNKNOWN
}
// mapper: READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE is enabled
When UNKNOWN arrives, it's a business decision: send it to a DLQ, skip it with a warning, or review it by hand.
-
Deployment order. For a new value: first the version that every consumer understands, then the producer starts sending it. For removal, the reverse order.
-
Contract tests. Pact or a schema registry (Avro/Protobuf compatibility rules) answers the question "does this change break a consumer?" in CI.
-
If the set is open, don't use an enum. If payment methods, countries or providers grow fast, use a
Stringcode in the contract and map it to known values inside the service. -
In internal switches, use the compiler's help (without
default), but first normalize the value coming in from the boundary toUNKNOWN.
The bottom line: inside a service an enum is a closed set, but between services it is never fully closed, because versions live side by side.
Sign up to mark studied questions and save your progress.