AR5: Legacy modernisation: strangler fig, anti-corruption layers and safe migrations

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Moving data safely

🎯You'll be able to

Plan a data migration that can be run more than once, checked by counts, and undone, using the expand-and-contract approach for changes to shared data.

🏠The everyday picture

Moving house, you label boxes, count them into the van and count them out at the new place, before throwing away the old cupboards.

📘What it really is

📝A worked example

Input: the notify service needs the four message templates that live in the monolith.

Steps:

  1. Read the monolith's templates.json and the service's file (empty at first).
  2. Copy templates that are missing or different; count copied and unchanged.
  3. Write data/migration-report.json: { source: 4, target: 4, copied: 4, unchanged: 0, matching: true }.
  4. Run it again: copied: 0, unchanged: 4, matching: true.

Expected output: a migration that can be repeated safely, with a report that proves the two sides match.

🌍Where it's used

📝Case scenario

A clinic chain moved patient appointments to a new system with a script that failed half-way. Re-running it created duplicate appointments for the first half, and patients turned up for slots that had been double-booked. A migration that skipped records already copied would have been safe to re-run.

🖱️Try it

Write the steps for moving one table in a project of yours to a new shape with expand and contract. Success: each step can be undone, and nothing breaks between steps.

⚠️Common mistakes

🛠️For your project

Project 2's test 9 runs your migration twice into a temporary folder and checks the counts and the report.

✅Quick check

What makes a migration idempotent?

Show the answer

Running it again gives the same result, with nothing copied twice.

What are the steps of expand and contract?

Show the answer

Add the new shape beside the old, move readers and writers over, then remove the old shape.

← PreviousAnti-corruption layers and facades
ProLearners Assist