Scrivio Interview library
LeadershipPrincipal6 min read · updated 12 November 2026

Across years of operation, enum values have been persisted as ordinals, names and custom codes in databases, caches and messages, causing several data incidents. What long-term rules would you set?

We would persist a stable external code for every enum, never an ordinal, and accept names only where renames are forbidden; keep that code separate from the Java constant name so code can be refactored without migrating data; require every consumer to tolerate unknown values, because producers and stored data outlive any deployment; never remove a value that may exist in stored data, only deprecate it; and build these rules into the shared persistence and serialization libraries, with tests that pin each constant's code.

Background to this question

After years of operation, a company's enums have been written into database columns, caches, message topics and other teams' systems in every possible form. Each incident got its own fix, but the next one came from a different direction. A senior engineer's job here is to replace incident-by-incident fixes with a few rules that make the whole class of failure rare, and to make those rules the easy path for teams. In this answer, we will look at the three ways enums have been persisted and how each failed, derive the rules, and then show how to build them into shared libraries and tests so teams follow them without thinking.

1Three Ways Enums Were Persisted, and How Each Failed

Looking back over the incidents, each one came from one of three representations. Ordinals, the position of a constant in its declaration, were used because they are compact and are JPA's default; they failed when someone inserted a constant in the middle and every stored number after it changed meaning. Names were used because they are readable; they failed when someone renamed a constant in an IDE and every stored copy of the old name became unreadable. Custom codes were used in some places; they mostly worked, but each team wrote its own mapping, some lookups returned null for unknown codes, and some codes were reused for a different meaning after a value was retired.

How each representation failed
Stored asBreaks whenTypical incident
ordinal()a constant is inserted or reorderedsuspended accounts read as closed
name()a constant is renamedold sessions and messages unreadable
Ad hoc codesunknown codes, reused codesnull status, wrong meaning for old rows
Stable code, central mappingonly if a code is changed or reusedrare, and caught by tests

2Rule 1: Persist a Stable External Code, Never an Ordinal

Every enum value that leaves the process gets an external code that is fixed for ever. Ordinals are banned for persistence and messaging outright, because their meaning depends on source order that developers change for readability. Names are acceptable only where a rename is explicitly forbidden, for example a small internal table, and that restriction is documented next to the enum.

The code is defined on the constant, not derived from it, so it is visible in the source and reviewable.

Java
public enum AccountStatus implements PersistentEnum {
ACTIVE("A"), SUSPENDED("S"), CLOSED("C"),
@Deprecated PENDING_KYC("K"); // retired: never written, still readable
private final String code;
AccountStatus(String code) { this.code = code; }
@Override public String code() { return code; }
}

Notice the deprecated constant. That is the fourth rule in action, and we will come back to it.

3Rule 2: Separate the External Code From the Java Name

Once the stored value is an explicit code, the Java name is free to change. A developer can rename SUSPENDED to FROZEN, or reorder constants for readability, and no data migration is needed, because the code S still maps to the same constant. This is what turns a dangerous refactoring into a safe one.

It also lets the code follow the needs of other systems. A partner may require numeric codes, a reporting team may prefer short letters, and the Java name can still read naturally. The mapping is in one place, the constant's declaration, rather than spread across converters and SQL scripts.

4Rule 3: Consumers Tolerate Unknown Values

Producers and stored data outlive any single deployment. A message produced by a newer service version can reach an older consumer; a row written by tomorrow's release can be read by today's batch job during a rollback. So every reader must handle a code it does not recognize without crashing or blocking: map it to an explicit UNKNOWN constant, log and count it, and handle UNKNOWN deliberately in code. The value read is kept as raw text where possible, so it can be written back unchanged and nothing is lost when an old version passes the data along.

5Rule 4: Never Remove a Value That May Exist in Stored Data

Retiring a value is the change teams most often get wrong. If we delete the constant PENDING_KYC, every old row or retained message that holds K becomes unreadable, or worse, K is later reused for a new meaning and old rows silently change meaning. So we deprecate instead: the constant stays, producers stop writing it, a check rejects new writes, readers still understand it, and its code is never reused. Only after a data migration has rewritten every stored copy, and retention has expired for every message, can it be removed, and even then the code stays reserved.

6Making the Rules the Easy Path

Rules that rely on memory fail across many teams and many years. So we put them into the shared libraries that teams already use. A PersistentEnum interface with a code() method, a generic JPA AttributeConverter and Jackson serializer and deserializer that work for any such enum, and a lookup that maps unknown codes to UNKNOWN with a metric. Teams then get correct persistence by implementing one interface.

Java
public interface PersistentEnum { String code(); }
public abstract class CodeConverter<E extends Enum<E> & PersistentEnum>
implements AttributeConverter<E, String> {
private final Map<String, E> byCode;
protected CodeConverter(Class<E> type) {
byCode = Arrays.stream(type.getEnumConstants())
.collect(Collectors.toUnmodifiableMap(PersistentEnum::code, e -> e)); // duplicate codes fail at startup
}
public String convertToDatabaseColumn(E e) { return e == null ? null : e.code(); }
public E convertToEntityAttribute(String c) {
if (c == null) return null;
E e = byCode.get(c);
return e != null ? e : onUnknown(c); // never a silent null
}
protected E onUnknown(String code) { // override to return UNKNOWN and count it
throw new IllegalStateException("Unknown enum code: " + code);
}
}
@Converter(autoApply = true)
class AccountStatusConverter extends CodeConverter<AccountStatus> {
AccountStatusConverter() { super(AccountStatus.class); }
}
Step throughLifecycle of a persisted enum value
  1. Add constant with a new, never-used codepinning test updated
  2. Readers deploy firstthey understand the code before anyone writes it
  3. Writers start using itbehind a flag if the meaning matters
  4. Retire: mark @Deprecatedno new writes, still readable
  5. Migrate stored copies, wait out retentionthe code stays reserved for ever

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

Alongside the library, each persistent enum gets a pinning test that lists every constant with its code, so changing or reusing a code fails the build with a message explaining why. An architecture test bans @Enumerated(EnumType.ORDINAL) and enums without codes in entity and message classes. With those in place, the rules hold even for teams that have never read them.

7Follow-up questions

Isn't a code per constant extra work for every enum?

It is one string per constant, written once. Compared with a data migration after a rename or an incident from shifted ordinals, it is very cheap, and the shared converter means no extra mapping code.

How would you migrate existing ordinal and name columns?

One enum at a time: add codes to the enum, add a new column, backfill it with a mapping from the old representation, deploy readers that use the new column, then stop writing the old one and drop it. The pinning test goes in with the first step.

What about enums that only live in memory?

They are unaffected. The rules apply only to values that leave the process: databases, caches, messages, files and APIs. EnumSet, EnumMap and ordinals in memory are fine.

8Conclusion

In this answer, we looked at how persisting ordinals, names and ad hoc codes each caused incidents, and set five rules: persist a stable external code and never an ordinal, keep that code separate from the Java name, make every consumer tolerate unknown values, never remove a value that may exist in stored data but deprecate it and never reuse its code, and build all of this into shared persistence and serialization libraries with pinning and architecture tests so that following the rules is the default.

What to say in the interviewAbout 80 seconds aloud

Open with

Looking at the incidents, each one came from the representation: ordinals broke when someone inserted a constant, names broke on a rename, and ad hoc codes broke on unknown or reused codes.

Then, if they want more

So I would set a few rules. First, anything that leaves the process stores a stable external code defined on the constant, never an ordinal, and names only where renames are explicitly forbidden. Second, the code is separate from the Java name, so developers can rename or reorder constants without any data migration.

Third, consumers must tolerate unknown codes, mapping them to an explicit UNKNOWN with a metric, because producers and stored data outlive any one deployment and rollbacks happen. Fourth, we never delete a value that might exist in stored data or retained messages: we deprecate it, stop writing it, keep reading it, and never reuse its code.

And fifth, I would not rely on people remembering this. I would put it into the shared libraries: a PersistentEnum interface with a code method, a generic JPA converter and Jackson serializers that use it, a lookup with the unknown-value policy built in, a pinning test per enum, and an architecture test that bans ordinal mapping in entities and messages. Then the correct path is also the easiest one.

About this answer

Last updated
12 November 2026
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 "Across years of operation, enum values have been persisted as ordinals, names and custom...": https://scrivio-interview-library.netlify.app/java/enums/ae8f78d8f1/