Migrating a legacy Java codebase without stopping delivery
The big rewrite is attractive because it promises a clean start. It also freezes the old system while the new one catches up with a target that keeps moving, and nothing reaches production until the end. I prefer to treat modernization as ordinary delivery: many small changes, each shippable and each reversible.
These are the techniques I reach for, in the order I usually need them.
Pin down current behavior first
Legacy code has few tests, and the behavior people depend on often isn't what anyone would have designed. Before changing it, write characterization tests. These don't assert what the code should do. They record what it does now.
@Test
void feeCalculationKeepsItsCurrentRounding() {
BigDecimal fee = calculator.feeFor(new BigDecimal("10.005"), Currency.getInstance("USD"));
// Looks odd, but callers depend on it. Change it deliberately or not at all.
assertThat(fee).isEqualByComparingTo("0.11");
}
Golden-file tests work well for code with large outputs: capture the output of a real input, commit it, and compare on each run. When a test fails during a refactor, you've found either a mistake or a behavior someone needs to decide about. Both are worth knowing before production finds them.
Find the seams
A seam is a place where you can change behavior without editing the code around it. In old Java, the obstacles are usually static calls, new buried inside methods, and business logic mixed with SQL.
Michael Feathers' Working Effectively with Legacy Code has the full catalog. The move I use most is extracting an interface around a dependency and passing it in. That one change makes the class testable and gives you a place to swap the implementation later.
Strangle, don't replace
The strangler fig pattern moves one piece of behavior at a time behind a routing layer that callers already go through. At first everything goes to the old code. Then one path moves, and then another, until the old code has no traffic and can be deleted.
Inside one codebase, the same idea is called branch by abstraction:
public interface StatementRenderer {
Statement render(Account account, YearMonth period);
}
@Component
class RoutingStatementRenderer implements StatementRenderer {
private final LegacyStatementRenderer legacy;
private final ModernStatementRenderer modern;
private final Flags flags;
// constructor omitted
@Override
public Statement render(Account account, YearMonth period) {
return flags.isOn("modern-statements", account.id())
? modern.render(account, period)
: legacy.render(account, period);
}
}
The team keeps merging to the main branch, the new code ships switched off, and a flag decides who gets it. A long-lived migration branch is the thing to avoid. It costs a little more every day it exists, and the final merge is where these projects go to die.
Compare before you switch
For code where a wrong answer is expensive, run both versions and compare. Serve the old result, compute the new one on the side, and log any difference.
Statement current = legacy.render(account, period);
try {
Statement candidate = modern.render(account, period);
if (!current.equals(candidate)) {
log.warn("Statement mismatch for account {} period {}", account.id(), period);
}
} catch (RuntimeException e) {
log.warn("Modern renderer failed for period {}", period, e);
}
return current;
Only do this for reads, or for writes that go to a separate place. Watch what ends up in the log, too: mismatch messages have a way of including data that shouldn't be there. Mismatches trend toward zero as you fix them, which gives you evidence for switching rather than optimism.
Upgrade the platform in steps
Moving an old application to a current JDK and framework is also better in stages:
- Move to the newest build of the JDK you're already on, and make the build reproducible.
- Run
jdeps --jdk-internalsto find use of JDK internals, andjdeprscanto find deprecated APIs. - Upgrade one LTS at a time, fixing
--add-opensneeds properly rather than collecting flags. - Treat
javax.*tojakarta.*as its own change, because every library has to agree. OpenRewrite recipes handle most of the mechanical edits. - Keep each step in production for a while before starting the next.
If a step goes wrong, you then know which step it was.
Keep the database for last
Application code can be flipped by a flag. A schema can't. Use expand and contract: add the new column or table, write to both, backfill, move reads, and only then remove the old one. Each stage is deployable and can be rolled back, and the old and new code can run at the same time during a rollout.
Delete things
A migration isn't finished when the new code works. It's finished when the old code is gone, along with its flag. Each flag should have an owner and an end date. Otherwise you've built a system with two implementations of everything, which is harder to work on than the one you started with.
The measure of a good migration is dull: features kept shipping, each step was small enough to undo, and one day someone deleted the last of the old code in an unremarkable pull request.