A house people live in isn't renovated all at once. Where would the family sleep? One room is renovated, the family moves into another room for a while, the room is finished, then the next room. The house stays usable every single day.
A safe migration from a monolith to microservices goes step by step the same way: one part moves to a new service, its traffic is shifted over a little at a time, and the monolith keeps serving everything else. The pattern is called Strangler Fig, after the fig that slowly wraps around its host tree until the old tree can die without anything falling down.
Why not rewrite it all at once
A big bang rewrite means rewriting the whole application from scratch, then replacing the old one in a single launch. It almost always slips. The business doesn't stop during the rewrite: promoters keep asking for features, so the monolith keeps changing and the new version chases a moving target. Small behaviours nobody ever documented get lost in the new version. And the risk is all or nothing: if the launch fails, you're back to zero. Strangler Fig delivers something at every step, and every step can be undone on its own.
Strangler Fig: a facade in front of the monolith
The key is a facade: one door in front of the monolith that decides who serves each request. At TicketTune the facade is the api.tickettune.test API gateway from the API Gateway lesson. Here's the timeline for moving the ticket context (e-tickets and the gate scan) out of the app monolith:
| Phase | What happens | Routes on the new service |
|---|---|---|
| 0 | The app monolith, one PostgreSQL |
0 of 6 |
| 1 | The facade goes in front of app; every route still goes to app |
0 of 6 |
| 2 | The e_tickets table moves to a ticket schema in the same database |
0 of 6 |
| 3 | The ticket service runs; the facade sends 10% of GET /api/tickets/:code to it |
1 of 6 (10%) |
| 4 | The canary is healthy: both ticket routes go 100% to the ticket service |
2 of 6 |
| 5 | The ticket service moves to its own database, with the data synced |
2 of 6 |
| 6 | The old schema is dropped, the ticket code removed from the monolith | 2 of 6 |
The first real step is phase 3: one endpoint moves to the new service, while the monolith keeps serving the rest. If something goes wrong, rollback is setting the number in the facade back to 0%, with no monolith deploy.
Pick the first context
What moves is a bounded context from the DDD for Microservices lesson, not a random slice. ticket goes first because its boundary is clear (ETicket has its own model), it has few dependencies (it only needs to hear "the order is paid" from order), and its load pattern is different: 10,000 scans at the gate within two hours on concert day. Start with the most independent context to keep the risk low; don't make payment, the most sensitive one, the first.
Splitting the database: three steps
Splitting the database is the hardest part. The order:
- Logical (phase 2): the
e_ticketstable moves to aticketschema, still in the sameappdatabase. Other queries that JOIN that table are removed; theorderandpaymentmodules call theticketmodule's functions. - API (phase 3): the
ticketservice stands up and becomes the only thing that touches that schema. The monolith asks over HTTP, not with aSELECTon its tables. - Physical (phase 5): the
ticketservice moves to its own database. During the transition, data is synced through an outbox or CDC.
Splitting the physical database too early, before the code stops querying each other's tables, leaves data inconsistent during the migration.
Dual-write: two writes, one crash
In phase 5 the old schema is still the source of truth (so you can roll back), and the new database is being filled. Every new e-ticket must land in both places. The easiest way, dual-write: write to the old schema, then write to the new database. The example model in the Lab: 1,000 e-tickets, and the process dies on every 200th write, right between the two writes. Before you look at the table, take a guess: how many e-tickets are in the new database, 1,000 or 995?
| E-ticket no. | Old schema | New database |
|---|---|---|
| 1 to 199 | there | there |
| 200 | there (committed) | missing: the process died before the second write |
| 201 to 399 | there | there |
| ... 400, 600, 800, 1,000 | there | missing |
| Total | 1,000 | 995 |
The new database holds only 995. Five e-tickets are missing there, and no error tells anyone. As soon as the ticket service reads the new database, five buyers get turned away at the gate after paying. Swapping the order doesn't help either: if the new database is written first, a crash leaves an e-ticket in the new one that isn't in the source of truth.
Outbox: one transaction, a relay that retries
The fix is the outbox pattern from the Data Consistency Patterns lesson: the e_tickets row and an outbox row (the message "copy this e-ticket") are written in one local transaction, so either both exist or neither does. A separate process, the relay, reads the outbox and sends it to the new database.
// Phase 5: 1,000 new e-tickets while the data moves to the ticket DB, example model
const oldDb = new Set(); // the ticket schema in the app DB: the source of truth
const newDb = new Set(); // the new ticket DB
const outbox = [];
// Each e-ticket: the e_tickets row and the outbox row go in ONE local transaction
for (let i = 1; i <= 1000; i++) {
const code = "TN-42-" + String(i).padStart(4, "0");
oldDb.add(code); // INSERT INTO ticket.e_tickets ...
outbox.push({ code, sent: false }); // INSERT INTO ticket.outbox ... then COMMIT
}
// Relay: send what isn't marked yet, then mark it. Dying in between = sending it again.
let sends = 0;
let round = 0;
while (outbox.some((o) => !o.sent)) {
round++;
outbox.forEach((o, i) => {
if (o.sent) return;
sends++;
newDb.add(o.code); // upsert by code: a resend doesn't duplicate
const relayDied = round === 1 && (i + 1) % 200 === 0;
if (!relayDied) o.sent = true;
});
}
console.log("old", oldDb.size, "| sends", sends, "| new", newDb.size);
// old 1000 | sends 1005 | new 1000
- The first loop is the local transaction: the e-ticket and its outbox message go in together, leaving no gap for a crash in between.
- The relay can die after sending but before marking
sent. The result isn't a loss but a resend: 5 of the 1,005 sends are duplicates. newDb.add(o.code)stands for an upsert by code: writing an e-ticket that already exists adds no row. The receiver must be idempotent.- Instead of polling the outbox table, the relay can also read the database's log directly (CDC, Change Data Capture).
Canary: move traffic a little at a time
In phase 3 the facade sends only 10% of GET /api/tickets/:code to the new service, picked per user (user_id % 100 < 10) so one user always lands in the same place. Then compare the two paths with the signals from the Observability in Distributed Systems lesson: a 0.2% error rate on both, p95 180 ms on the monolith and 95 ms on the new service (example model). Healthy, so it goes up to 100%. If the new service's error rate climbs, set it back to 0% within seconds.
When to stop
A migration needs exit criteria: when it counts as done. The goal isn't an empty monolith, but a solved problem. After ticket, move on to concert only with a real reason, for example the event page having to take 50 times the usual load in a ticket war. account, which rarely changes and carries little load, can stay in the monolith. Without exit criteria, the team gets stuck "half migrated" forever, maintaining two systems.
Watch for the result called a distributed monolith: the services are split, but still read the same database and call each other synchronously everywhere. You pay the network cost without getting the freedom to deploy. The opposite is a trap too: nano-services, services so small that running them costs more than they bring.
⚠️ Five ways a migration goes wrong
- A big bang rewrite. It slips, chases a monolith that keeps changing, and the risk is all or nothing.
- Dual-writing to two places. A crash in between makes the data differ with nobody noticing. Use an outbox or CDC.
- Splitting the physical database too early. Split the schemas first, replace queries with API calls, then split the database.
- No canary and no facade. Every problem needs a deploy to roll back. With a facade, rollback is changing a number.
- No exit criteria. Half migrated forever means twice the maintenance cost.
TL;DR: Move from a monolith to microservices step by step with Strangler Fig: put a facade (an API gateway) in front of the monolith, then move one route to a new service while the monolith serves the rest; a big bang rewrite almost always fails. Move one whole bounded context, starting with the most independent one, like ticket. Split the database in three steps: separate schemas in the same database, replace direct queries with API calls, then a physical database of its own. While data is synced, dual-write loses data when a crash falls between the two writes (1,000 becomes 995); an outbox writes the row and its message in one local transaction, the relay resends, and an idempotent upsert keeps the result at 1,000. Raise traffic through a canary (10% then 100%) while comparing error rate and p95. Set exit criteria, and avoid a distributed monolith: separate services that still share a database and call each other synchronously.
⚡ Not a big bang rewrite
Meaning: Rewriting everything and swapping it in one launch almost always slips, and the risk is all or nothing. Strangler Fig moves one part to a new service, shifts its traffic over a little at a time, and the monolith keeps serving the rest.
⚡ A facade in front of the monolith
Meaning: The first real step: one endpoint moves to the new service, while the monolith keeps serving the rest. The facade sends 10% of GET /api/tickets/:code to the ticket service, picked per user (user_id % 100 < 10). If something goes wrong, rollback is setting the number in the facade back to 0%.
⚡ Dual-write quietly loses data
Guess first: 1,000 e-tickets are written to the old schema, then to the new database, and the process dies on every 200th write, right between the two. How many e-tickets are in the new database?
Only 995. Five e-tickets are missing there, and no error tells anyone.
Meaning: As soon as the ticket service reads the new database, five buyers get turned away at the gate after paying.
⚡ Outbox: one local transaction
Write:
oldDb.add(code); // INSERT INTO ticket.e_tickets ...
outbox.push({ code, sent: false }); // INSERT INTO ticket.outbox ... then COMMIT
Meaning: The e-ticket row and its outbox message go in one local transaction, so either both exist or neither does. The relay resends when it has to, and the receiver is idempotent: an upsert by code.