Strangler Fig Pattern
A pattern for gradually replacing legacy systems
A pattern for gradually replacing legacy systems
Strangler Fig Pattern is a migration strategy that incrementally replaces a legacy system with a new one. It takes its name from the Australian strangler fig tree, which slowly wraps around its host tree and eventually replaces it. Since Martin Fowler introduced it in 2004, it has been widely used for transitioning from monoliths to microservices.
Core Principles:
Implementation Steps:
Facade Implementation Approaches:
Advantages: Risk distribution (a little at a time), easy rollback, business continuity maintained, eased team learning curve. Disadvantages: Cost of maintaining two systems simultaneously, complex routing logic, risk of becoming a long-running project.
# nginx.conf - Strangler Facade
upstream legacy_monolith {
server legacy-app:8080;
}
upstream new_order_service {
server order-service:8080;
}
upstream new_user_service {
server user-service:8080;
}
server {
listen 80;
server_name api.example.com;
# μ λ§μ΄ν¬λ‘μλΉμ€λ‘ μ΄μ λ κΈ°λ₯
location /api/v2/orders {
proxy_pass http://new_order_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /api/v2/users {
proxy_pass http://new_user_service;
proxy_set_header Host $host;
}
# μμ§ μ΄μ λμ§ μμ κΈ°λ₯ β λ κ±°μλ‘ μ λ¬
location /api/ {
proxy_pass http://legacy_monolith;
proxy_set_header Host $host;
}
# λ¨κ³μ μ ν: 10% νΈλν½λ§ μ μμ€ν
μΌλ‘
location /api/v2/products {
# λμ κΈ°λ° νΈλν½ λΆν
set $backend legacy_monolith;
if ($request_id ~* "[0-9]$") {
set $backend new_product_service;
}
proxy_pass http://$backend;
}
}
// strangler-facade.js
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';
const app = express();
// Feature Flag μλΉμ€ (LaunchDarkly, Unleash λ±)
const featureFlags = {
'new-order-service': true,
'new-user-service': true,
'new-product-service': false, // μμ§ μ΄μ μ€
};
// λ κ±°μ λͺ¨λ리μ€
const legacyProxy = createProxyMiddleware({
target: 'http://legacy-monolith:8080',
changeOrigin: true,
});
// μ λ§μ΄ν¬λ‘μλΉμ€λ€
const orderServiceProxy = createProxyMiddleware({
target: 'http://order-service:8080',
changeOrigin: true,
pathRewrite: { '^/api/orders': '/orders' },
});
const userServiceProxy = createProxyMiddleware({
target: 'http://user-service:8080',
changeOrigin: true,
});
// Strangler Router λ―Έλ€μ¨μ΄
function stranglerRouter(featureFlag, newProxy) {
return (req, res, next) => {
if (featureFlags[featureFlag]) {
// μ μλΉμ€λ‘ λΌμ°ν
console.log(`[Strangler] Routing to new service: ${featureFlag}`);
return newProxy(req, res, next);
}
// λ κ±°μλ‘ ν΄λ°±
console.log(`[Strangler] Fallback to legacy: ${featureFlag}`);
return legacyProxy(req, res, next);
};
}
// λΌμ°ν
κ·μΉ
app.use('/api/orders', stranglerRouter('new-order-service', orderServiceProxy));
app.use('/api/users', stranglerRouter('new-user-service', userServiceProxy));
// μ΄μ λμ§ μμ λͺ¨λ μμ² β λ κ±°μ
app.use('/api', legacyProxy);
// μ μ§μ λ‘€μμ (νΌμΌνΈ κΈ°λ°)
function canaryRouter(percentage, newProxy) {
return (req, res, next) => {
const userId = req.headers['x-user-id'] || req.ip;
const hash = hashCode(userId) % 100;
if (hash < percentage) {
return newProxy(req, res, next);
}
return legacyProxy(req, res, next);
};
}
// 20% νΈλν½λ§ μ μν μλΉμ€λ‘
app.use('/api/products', canaryRouter(20, productServiceProxy));
app.listen(3000);
# istio-strangler.yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: api-strangler
spec:
hosts:
- api.example.com
http:
# μμ ν μ΄μ λ κΈ°λ₯
- match:
- uri:
prefix: /api/orders
route:
- destination:
host: order-service
port:
number: 8080
# μ μ§μ μ΄μ μ€ (80% λ κ±°μ, 20% μ μλΉμ€)
- match:
- uri:
prefix: /api/products
route:
- destination:
host: legacy-monolith
port:
number: 8080
weight: 80
- destination:
host: product-service
port:
number: 8080
weight: 20
# νΉμ ν€λλ‘ μ μλΉμ€ ν
μ€νΈ
- match:
- headers:
x-canary:
exact: "true"
uri:
prefix: /api/inventory
route:
- destination:
host: inventory-service
# λλ¨Έμ§ β λ κ±°μ
- route:
- destination:
host: legacy-monolith
port:
number: 8080
CTO: "We need to move a 10-year-old monolith to microservices, but the risk of a Big Bang rewrite is too high."
Architect: "Let's take the Strangler Fig Pattern approach. We put an API Gateway in front and migrate features to new services one by one. We'll start with the order domain, then payments, then users."
CTO: "What about the existing services?"
Architect: "We keep them running and gradually shift traffic over. If a problem comes up, we can roll back to the legacy system immediately. We can migrate the core features in about a year."
Interviewer: "What are the drawbacks of the Strangler Fig Pattern?"
Candidate: "First, you have to run both systems at the same time, which increases infrastructure cost and operational complexity. Second, maintaining data consistency is difficult. When the legacy and new systems reference the same data, synchronization problems arise. Third, if the project drags on, it can fall into a state of 'eternal migration.'"
Interviewer: "How do you solve data synchronization?"
Candidate: "You can synchronize in real time with CDC (Change Data Capture), or use event sourcing to publish events to both sides. Alternatively, when migrating a specific feature, you can migrate the related data along with it to cut the dependency."
PM: "What's the status of the order service migration?"
Developer: "We're sending 30% of traffic to the new service right now. The error rate is under 0.1%, and response time is 40% faster than the legacy system. We plan to raise it to 50% this week and switch to 100% next week."
PM: "What about the legacy code?"
Developer: "After switching to 100%, we'll monitor for two weeks, and if there are no problems, we plan to remove the legacy order module code. That's when the real 'strangling' is complete."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free