Strangler Fig Pattern
The Strangler Fig pattern enables gradual migration from legacy systems to new implementations. Named after the strangler fig tree that grows around its host, this pattern lets you incrementally replace functionality while keeping the system running.
📋 At a Glance
| Aspect | Details |
|---|---|
| Origin | Martin Fowler, 2004 |
| Use Case | Migrating from legacy to modern systems |
| Key Benefit | Continuous operation during migration |
| Risk Level | Low (incremental changes) |
🎯 What You'll Learn
- Strangler Fig pattern implementation
- Feature toggles for gradual rollout
- Request routing strategies
- Data synchronization during migration
- Rollback strategies when things go wrong
Production Story: The Monolith Migration
A company needed to migrate from a legacy Java 6 monolith to microservices:
TEXT(5 lines)CodeLoading syntax highlighter...
TEXT(7 lines)CodeLoading syntax highlighter...
Mental Model: Strangler Fig
TEXT(34 lines)CodeLoading syntax highlighter...
🔬 Deep Dive
Pattern 1: Proxy-Based Strangler
JAVA(42 lines)CodeLoading syntax highlighter...
Pattern 2: Feature Toggle Service
JAVA(57 lines)CodeLoading syntax highlighter...
Pattern 3: Parallel Run Verification
JAVA(54 lines)CodeLoading syntax highlighter...
Pattern 4: Data Synchronization
JAVA(75 lines)CodeLoading syntax highlighter...
Pattern 5: Rollback Strategy
JAVA(62 lines)CodeLoading syntax highlighter...
Pattern 6: Event-Based Strangler
JAVA(47 lines)CodeLoading syntax highlighter...
Migration Checklist
TEXT(26 lines)CodeLoading syntax highlighter...
🐛 Debug This: The Data Divergence
A team is migrating their order system using the Strangler Fig pattern. They set up dual-write to both legacy and new databases. After a week, they notice the new system reports different revenue totals. "We're writing to both systems - how can the data be different?"
JAVA(62 lines)CodeLoading syntax highlighter...
Multiple data synchronization bugs:
JAVA(5 lines)CodeLoading syntax highlighter...
request.getTotal() is null, 0, or different from calculated, data diverges.The code only handles new orders. What about orders created before dual-write was enabled? They exist in legacy but not in new system.
JAVA(2 lines)CodeLoading syntax highlighter...
If the new order hasn't synced yet (dual-write just enabled), status update is silently skipped.
If new system write fails, the error is swallowed (from earlier code review - try/catch with just logging).
JAVA(82 lines)CodeLoading syntax highlighter...
💻 Exercises
Exercise 1: Basic Feature Toggle
⭐ Difficulty: Easy | ⏱️ Time: 10 minutes
JAVA(4 lines)CodeLoading syntax highlighter...
JAVA(66 lines)CodeLoading syntax highlighter...
Exercise 2: Proxy Router
⭐⭐ Difficulty: Medium | ⏱️ Time: 15 minutes
JAVA(5 lines)CodeLoading syntax highlighter...
JAVA(119 lines)CodeLoading syntax highlighter...
Exercise 3: Parallel Run Comparator
⭐⭐ Difficulty: Medium | ⏱️ Time: 20 minutes
JAVA(5 lines)CodeLoading syntax highlighter...
JAVA(121 lines)CodeLoading syntax highlighter...
Exercise 4: Circuit Breaker for Migration
⭐⭐⭐ Difficulty: Medium-Hard | ⏱️ Time: 20 minutes
JAVA(6 lines)CodeLoading syntax highlighter...
JAVA(159 lines)CodeLoading syntax highlighter...
Exercise 5: Complete Migration Orchestrator
⭐⭐⭐⭐ Difficulty: Hard | ⏱️ Time: 30 minutes
JAVA(6 lines)CodeLoading syntax highlighter...
JAVA(234 lines)CodeLoading syntax highlighter...
📝 Summary
| Component | Purpose |
|---|---|
| Proxy/Facade | Route traffic between systems |
| Feature Toggles | Control rollout percentage |
| Parallel Run | Verify new matches legacy |
| Circuit Breaker | Automatic rollback on errors |
| CDC/Dual Write | Keep data synchronized |
📅 Review Schedule for This Article
| Day | Task | Time |
|---|---|---|
| Day 1 | Review the Strangler Fig phases diagram | 5 min |
| Day 3 | Redo Exercise 1 (Feature Toggle) | 10 min |
| Day 7 | Draw a strangler fig architecture for a system you know | 15 min |
| Day 14 | Redo Debug This (The Data Divergence) | 15 min |
| Day 30 | Plan a migration using the Migration Checklist | 20 min |