How to use the strangler fig pattern for migration
· Category: System Design
Short answer
The strangler fig pattern gradually replaces a legacy system by intercepting requests and routing them to new services over time.
Steps
- Place a facade or proxy in front of the legacy system.
- Identify bounded contexts that can be extracted incrementally.
- Build new services for each context and route relevant traffic to them.
- Migrate data and functionality piece by piece.
- Retire legacy components once all traffic has been redirected.
Tips
- Start with low-risk, well-understood domains to build confidence.
- Maintain data synchronization between old and new systems during transition.
- Use feature flags to control traffic routing and enable quick rollback.
- Monitor both systems to ensure parity in behavior and performance.
Common issues
- Complex data migration and synchronization challenges.
- Temporary increased operational overhead from running dual systems.
- Resistance to retiring the legacy system before all edge cases are covered.
- Routing logic becoming complex as more services are added.
Example
# Consistent hashing for service discovery
import hashlib
def get_node(key, nodes):
hash_val = int(hashlib.md5(key.encode()).hexdigest(), 16)
return nodes[hash_val % len(nodes)]
node = get_node('user-123', ['node-a', 'node-b', 'node-c'])
This snippet implements consistent hashing to distribute keys across nodes, a foundational technique in scalable distributed systems.