Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

A shared library exposes a public enum Country used by thirty services. A new value must be added every few months, and some services break each time. How do you redesign this?

Adding an enum constant is binary compatible, but it breaks exhaustive switches when consumers recompile, makes old compiled code throw MatchException when it meets the new value, and makes strict JSON parsers reject it, so a shared enum that changes every few months is a recurring outage. Country data changes on a business schedule, so it is reference data, not code: we would replace the enum with a string code type backed by a lookup service or table, make every consumer tolerate unknown values, and migrate gradually by adding the code-based type beside the enum, deprecating direct switches, and versioning the library with clear release notes in the meantime.

Why this question is asked

A shared library that thirty services depend on is a contract, and an enum inside that contract is a promise that the set of values is closed. When the business keeps adding countries, that promise is broken every few months, and different services break in different ways. Interviewers ask this design question to see whether we can tell code from data and plan a migration across many teams. In this answer, we will look at exactly how services break when a constant is added, why country codes are the wrong thing to model as an enum, the redesign, and how to migrate thirty services without a big-bang release.

1How Adding a Constant Breaks Consumers

The Java Language Specification says that adding a constant to an enum is binary compatible: existing compiled classes still link and run. That sounds safe, but it is only about linking. Behaviour breaks in at least three ways, at different times.

Java
// Service A, compiled against library 1.4 (DE, FR, US)
String vatRule = switch (country) {
case DE -> "EU"; case FR -> "EU"; case US -> "NONE";
};
// Library 1.5 adds MX.
// Recompiling A: error: the switch expression does not cover all possible input values
// Running old A with a MX value: java.lang.MatchException (IncompatibleClassChangeError before Java 21)
// Service B receives JSON {"country":"MX"} but still has library 1.4
// Jackson: InvalidFormatException: Cannot deserialize value of type Country from String "MX"
What happens when MX is added
Consumer stateWhere it breaksSymptom
Recompiled, exhaustive switchbuildcompile error at each switch
Old binary, switch expressionrun timeMatchException
Old binary, JSON with MXparsingInvalidFormatException, 400 or crash
Old binary, switch with defaultnowhere visibleMX silently handled as the default
The last row is the quietest and often the most dangerous.

Notice that each failure appears at a different moment. Service A fails at build time once it upgrades, which is annoying but safe. A service that did not upgrade fails at run time, when the new value reaches it through a message or an API. Service B, also on the old library, cannot even parse a payload containing the new value. With thirty services on different release schedules, some are always in the broken window.

2Code or Data?

An enum is the right tool for a closed set that changes only when the code changes, such as the states of an internal workflow. Countries are not like that. The list is decided by the business, which countries we ship to, and by the outside world, and it changes on the business's schedule, not the release train. Values that change on a business schedule are reference data. Modelling them as code means every business change requires a coordinated release of thirty services.

There is a further hint: the services mostly do not have behaviour per country in code. They pass the country along, store it, display it, or look up a rule for it, such as a VAT category or a shipping zone. Those rules are data too, and belong in a table, not in thirty switches.

3The Redesign: A Code Type Backed by Reference Data

We replace the enum with a small value type that wraps a validated string code, such as an ISO 3166 alpha-2 code, and a reference-data service or table that holds the list of countries and their attributes. Services that only pass the value along need nothing else. Services that need attributes ask the reference data, which can be cached locally and refreshed.

Java
public record CountryCode(String value) {
public CountryCode {
if (value == null || !value.matches("[A-Z]{2}")) {
throw new IllegalArgumentException("Country code must be two capital letters: " + value);
}
}
}
public interface CountryCatalog {
Optional<CountryInfo> find(CountryCode code); // backed by a table or a service, cached
}
CountryCode mx = new CountryCode("MX"); // valid even if the library predates Mexico
catalog.find(mx).map(CountryInfo::vatZone) // Optional[NONE]
.orElse(VatZone.REVIEW_MANUALLY); // explicit handling of an unknown country

With this design, adding a country is a data change in one place. No library release, no redeploy of thirty services. A consumer that meets a country it has no rule for handles it explicitly, here by flagging it for manual review, instead of crashing. The format validation in the record still catches garbage such as lower-case or three-letter codes.

4If an Enum Must Stay

Sometimes an enum is still justified, for example because some code really does branch on a handful of countries. Then the library must be designed for evolution. Every consumer must handle unknown values: a JSON setting that maps them to an UNKNOWN constant, and switches that treat UNKNOWN explicitly. The library team announces new constants a release ahead, so consumers can deploy the tolerant version before producers start sending the new value. And the library uses semantic versioning with release notes that list added constants clearly, so a consumer's build failure is expected rather than a surprise.

5Migrating Thirty Services Gradually

A big-bang switch is not realistic across thirty teams. Instead, we add the new type beside the enum, so both work for a while.

Step throughMigrating from enum Country to CountryCode
  1. Add CountryCode and CountryCataloglibrary 2.0, enum kept, with country.toCode()
  2. Make all consumers tolerate unknown valuesJSON default and UNKNOWN handling
  3. Deprecate direct switches on Countrywarnings point to CountryCatalog
  4. Teams migrate service by servicetrack remaining usages in the build
  5. Any usages of the enum left?
    YesKeep migrating, report per teamNoRemove the enum in the next major version

5 steps in order. Play them, or step through them one at a time.

Each step is safe on its own. Library 2.0 adds a toCode() method on the enum and a CountryCode type that APIs accept, so teams can switch call sites one at a time. Tolerating unknown values comes early because it removes the outages immediately, even before the full migration. The deprecation warnings and a simple usage report give every team a clear list of remaining work, and only when that list is empty do we remove the enum in a major version.

6Follow-up questions

Is adding an enum constant a breaking change or not?

For binary linking, it is compatible, which is what the specification means. For behaviour, it is breaking for any consumer that switches exhaustively, rejects unknown values when parsing, or stores the value in a constrained column. In a shared library, we treat it as breaking for planning purposes.

Why not just tell every team to add a default branch?

A default branch removes the crash but hides the problem: a new country is silently treated like some other case, perhaps with the wrong tax rule. It also removes the compiler's help for the cases that matter. Explicit unknown handling, plus moving per-country rules to data, is safer.

How do you keep the reference data consistent across services?

One owner publishes it, from a table or a small service, with a version or an updated timestamp. Consumers cache it with a refresh interval and expose the version in metrics, so we can see which services have the latest list.

7Conclusion

In this answer, we saw that adding an enum constant is binary compatible but breaks consumers at build time, at run time with MatchException, and at parse time, depending on their state. Countries change on a business schedule, so they are reference data, not code. We redesigned the library around a validated CountryCode and a reference-data catalog, made consumers tolerate unknown values, kept versioning and advance notice for any enum that remains, and migrated gradually by adding the new type, deprecating switches and removing the enum only once nothing uses it.

What to say in the interviewAbout 90 seconds aloud

Open with

The core problem is that country is reference data that changes on a business schedule, but it is modelled as code in a shared enum, so every new country is a coordinated release across thirty services.

Then, if they want more

Adding a constant is binary compatible, but behaviour breaks in several ways: services that recompile get compile errors on exhaustive switch expressions, services still on old binaries throw MatchException when the new value reaches a switch, and services with the old library cannot even deserialize JSON containing the new value.

A default branch hides it instead, which can apply the wrong rule silently. My redesign replaces the enum with a small CountryCode record that validates an ISO code, plus a country catalog backed by a table or reference-data service, cached locally, which holds attributes like VAT zone. Adding a country becomes a data change.

Every consumer must handle an unknown country explicitly, for example by routing it to manual review. If some enum must remain, consumers tolerate unknown values through an UNKNOWN default, and the library announces new constants a release ahead with clear versioning.

I would migrate gradually: ship the new type next to the enum, make consumers tolerant first to stop the outages, deprecate direct switches, track remaining usages per team, and remove the enum in a major version.

About this answer

Last updated
12 November 2026
Versions it names
Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A shared library exposes a public enum Country used by thirty services. A new value must...": https://scrivio-interview-library.netlify.app/java/enums/88e9c7b593/