πŸ—οΈ Architecture

Strangler Fig Pattern

A pattern for gradually replacing legacy systems

πŸ“– Detailed Explanation

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:

  • Incremental replacement: Replace functionality unit by unit instead of a full rewrite (Big Bang)
  • Facade layer: Routes requests to either the legacy or the new system
  • Parallel operation: Both systems coexist while the transition proceeds incrementally
  • Gradual decommissioning: Remove the legacy system after all functionality has been migrated

Implementation Steps:

  1. Transform: Develop replacement functionality in the new system
  2. Coexist: Split traffic with a Facade/Router and run both systems in parallel
  3. Eliminate: Remove legacy functionality after verification

Facade Implementation Approaches:

  • API Gateway: Define routing rules with Kong or AWS API Gateway
  • Reverse Proxy: Branch by URL pattern with Nginx or HAProxy
  • Service Mesh: Split traffic with Istio VirtualService
  • Feature Flag: Expose the new system by user or by percentage

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.

πŸ’» Code Example

Routing with Nginx Reverse Proxy

# 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;
    }
}

Node.js Express Router Facade

// 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);

Traffic splitting with Istio VirtualService

# 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

πŸ—£οΈ Examples of Practical Conversations

Migration Planning Meeting

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."

Technical Interview

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."

Sharing Progress

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

⚠️ Cautions

πŸ”— Related Terms

Microservices API Gateway Feature Flag CDC Canary Release Domain-Driven Design

πŸ“š Learn More