We are going to draft a monolith strangler plan. The strangler pattern involves incrementally replacing a monolith with microservices by intercepting requests to the monolith and routing them to new services, eventually strangling the monolith.
Steps:
- Identify decomposition seams: boundaries in the monolith where we can split functionality (e.g., by business capability, by data domain).
- Routing: set up a proxy or gateway to route requests to either the monolith or the new service.
- Data migration: plan for separating the database, considering synchronization and eventual consistency.
- Cuts: the actual implementation steps of extracting a service.
Let's break it down:
-
Decomposition Seams:
- Identify bounded contexts (Domain-Driven Design) or business capabilities that are relatively independent.
- Look for natural boundaries: modules, packages, or layers in the monolith that have low coupling.
- Consider data: which tables or data structures are used exclusively by one capability?
- Example: In an e-commerce monolith, we might identify: User Management, Product Catalog, Order Processing, Inventory, Payments.
-
Routing:
- Implement an API gateway (or edge service) that acts as a single entry point.
- Initially, all requests go to the monolith.
- For each new service, change the routing rules to send specific requests (by path, method, or other criteria) to the new service.
- The gateway should handle authentication, logging, etc., but initially, we might just forward.
-
Data Migration:
- This is the hardest part. We have two main strategies:
a. Shared database: Both the monolith and the new service use the same database initially. Then we gradually move data to a new database for the service.
b. Database per service: We set up a new database for the service and migrate data. We need to handle data synchronization during transition.
Steps for data migration:
- Identify the data that the new service will own.
- Create a new database (or schema) for the service.
- Synchronize data from the monolith database to the new one (real-time or batch).
- Change the service to read/write to the new database.
- Change the monolith to read/write to the old database (or eventually stop using that data).
- After the monolith no longer uses the data, we can drop the tables from the monolith database.
Challenges:
- Data consistency during the transition: We might need to handle dual writes or use change data capture (CDC) to keep the new database in sync.
- Transactions across services: We might need to use eventual consistency and handle failures.
-
Cuts (Extraction Steps):
-
For each identified seam, we will do the following:
Step 1: Implement the new service (with its own database if possible) and deploy it.
Step 2: Set up data synchronization (if we are using a separate database) so that the new service has the current data.
Step 3: Change the routing in the gateway to send requests for this capability to the new service.
Step 4: Monitor and test. If something goes wrong, we can route back to the monolith.
Step 5: Once the new service is stable, we can remove the corresponding code from the monolith (optional, but eventually we want to remove the monolith).
Step 6: Also, we can stop synchronizing data and let the new service be thesole owner of its data. Then, remove the data from the monolith (if shared) and update the monolith to not use that data.
-
We repeat for each seam until the monolith is empty.
Additional considerations:
- Testing: We need to have good tests for the monolith and the new services.
- Monitoring and observability: We must monitor the new services and the routing.
- Team structure: The Conway's law - we might need to reorganize teams to align with the new services.
Let's write a more structured plan:
Phase 1: Preparation
- Identify seams and prioritize (start with the least coupled, most independent).
- Set up the API gateway (if not already) and ensure all traffic goes through it.
- Set up monitoring, logging, and tracing across the monolith and services.
Phase 2: First Extraction (Example: User Management)
-
Decompose the User Management module:
a. Create a new User service with its own database.
b. Set up CDC (or dual writes) to sync user data from the monolith to the new service's database.
c. Change the gateway to route user-related requests (e.g., /users/*) to the new service.
d. The new service should handle all user CRUD operations. Initially, it might still need to read from the monolith database for some data until we fully migrate.
e. Gradually, update the monolith to not use the user tables (except maybe for foreign keys until we break those too).
f. Once the monolith no longer writes to the user tables, we can stop the CDC and let the User service be the sole owner.
-
Note: We must also consider authentication and authorization. The gateway might handle auth, but the User service might need to validate tokens, etc.
Phase 3: Subsequent Extractions
- Repeat for other seams (Product Catalog, Orders, etc.).
- Dependencies: Some services might depend on data from others. We might need to adjust the order of extraction.
Phase 4: Final Cleanup
- Once all functionality is extracted, the monolith should be reduced to a minimal stub (or nothing).
- We can then decommission the monolith.
Risks and Mitigations:
- Data inconsistency: Use CDC and eventually consistent models. Have compensation transactions for failures.
- Performance: The gateway and multiple services might introduce latency. Optimize and cache where necessary.
- Complexity: The system becomes more complex. Invest in automation, monitoring, and good documentation.
This is a high-level plan. Each extraction will have its own challenges and details.
Here’s a structured Monolith Strangler Fig Plan with actionable steps for decomposition, routing, data migration, and incremental cuts.
Monolith Strangler Fig Plan
Goal: Safely replace a monolith with microservices by incrementally extracting bounded contexts, routing traffic, migrating data, and “strangling” legacy functionality.
1. Decomposition Seams Identification
Objective: Find natural boundaries in the monolith for extraction.
-
Bounded Contexts (DDD):
Identify domains (e.g., User Management, Order Processing, Inventory, Payments). Look for:
- Low coupling/high cohesion within modules.
- Separate data models (distinct database tables/aggregates).
- Unique business rules or workflows.
-
Technical Seams:
- Routing-based: Separate by URL path (
/api/users/* → User Service) or HTTP headers.
- Library/Plugin Architecture: Extract modules that already expose APIs or interfaces.
- Database Schemas/Tables: Isolate tables used exclusively by one domain (e.g.,
users table only referenced by user auth logic).
-
Prioritization:
Start with low-risk, high-value seams:
- Least coupled to core transaction flows.
- Read-heavy or async operations (easier to migrate).
- Features with clear ownership and minimal cross-cutting concerns.
2. Routing & Traffic Management
Objective: Redirect requests from monolith to new services without downtime.
3. Data Migration Strategy
Objective: Decouple databases without breaking consistency.
-
Phase 1: Dual-Write & Sync (Strangler Fig Data Pattern)
- CDC (Change Data Capture): Use Debezium, AWS DMS, or GoldenGate to stream monolith DB changes to new service DB.
- Synchronization Scripts: Batch sync historical data initially.
- New Service Reads/Writes: Start with read-only from its own DB; monolith remains source of truth for writes.
-
Phase 2: Cutover Write Ownership
- Route write operations for a domain to the new service (via gateway).
- Monolith reads from new service DB for that domain (or syncs back via CDC if needed).
- Use idempotency keys to handle duplicate requests during cutover.
-
Phase 3: Monolith Data Cleanup
- Once monolith no longer reads/writes to migrated tables, drop them.
- archive old data if needed (e.g., for compliance).
-
Anti-Corruption Layer (ACL):
- New service calls monolith via adapters for any remaining dependencies (temporary).
- Translate data models between services (e.g., using GraphQL/REST facades).
4. Incremental Cuts (Extraction Steps)
Objective: Execute each extraction in small, reversible steps.
Template for One Seam Extraction:
-
Preparation:
- Add logging/tracing (OpenTelemetry) to monolith calls to analyze dependencies.
- Create new service skeleton (with its own DB).
- Implement CDC/sync from monolith DB to service DB.
- Test sync integrity (reconcile counts, spot-check data).
-
Safety Switch (Gate in Front):
- Implement routing rule in gateway for new service path.
- Route 0% traffic initially; log requests but still pass through to monolith.
-
Read Extraction:
- Route read requests for domain to new service (e.g., GET
/users/{id}).
- Monolith continues handling writes.
- Validate data consistency (compare responses between monolith/service).
-
Write Extraction (The Cut):
- Route write requests (POST/PUT/DELETE) to new service.
- Use idempotency keys to avoid duplicates.
- If service fails, fallback to monolith via gateway.
-
Monolith Cleanup:
- Remove dead code/APIs from monolith related to the domain.
- Decommission sync for that domain.
- Update documentation/architecture diagrams.
-
Repeat for next seam.
5. Example Timeline (for “User Management” Seam)
| Week | Step | Action |
|---|
| 1 | Identify Seam | users table, /api/users/* endpoints. Low coupling to orders. |
| 2 | Build Service | Scaffold User Service, own PostgreSQL DB. |
| 3 | Data Sync | Deploy CDC to sync users table to new DB. Validate 100% match. |
| 4 | Routing | Add gateway route: /api/users/* → User Service (0% traffic, log only). |
| 5 | Read Cutover | Route 5% read traffic to User Service. Compare responses. |
| 6 | Full Read Cutover | 100% reads to User Service. Monolith reads from User Service DB for user data. |
| 7 | Write Cutover | Route writes to User Service. Enable idempotency. Fallback to monolith on errors. |
| 8 | Cleanup | Remove user-related controllers/models from monolith. Stop CDC for users. |
6. Critical Success Factors
-
Observability:
- Trace requests across monolith/services (distributed tracing).
- Monitor data lag in CDC syncs.
- Alert on error rates >1% during cutovers.
-
Team Coordination:
- Assign “service owners” for each extracted domain.
- Document contracts (OpenAPI specs) between services and monolith.
-
Rollback Plan:
- Always keep monolith functional during extraction.
- Use gateway to instantly reroute 100% back to monolith if needed.
-
Testing:
- Contract tests between services (Pact).
- Chaos engineering (Simulate service failures, network latency).
7. Common Pitfalls & Mitigations
| Pitfall | Mitigation |
|---|
| Database deadlocks | Use CDC with minimal impact; schedule sync during off-peak. |
| Circular dependencies | Enforce “monolith calls new service only via ACL, never vice versa.” |
| Partial extraction (half-done seams) | Fully extract one seam before starting next; avoid “shared tables.” |
| Performance degradation | Cache aggressively at gateway; profile new service queries. |
| Team resistance | Involve monolith team in design; show tangible benefits (deploy autonomy). |
8. Tools & Technologies
- Routing: Kong, Envoy, HAProxy, AWS Application Load Balancer.
- Data Sync: Debezium, AWS DMS, Attunity, custom scripts.
- Monitoring: Prometheus/Grafana, ELK stack, distributed tracing (Jaeger/Zipkin).
- Feature Flags: LaunchDarkly, Unleash, Flagsmith.
- Testing: Postman/Newman for contract tests, Pact for consumer-driven contracts.
Final Notes
- Strangling is iterative: Each cut should take 2–8 weeks depending on complexity.
- Embrace eventual consistency: Not all data needs strong consistency immediately.
- Stop when monolith is empty: Decommission only after all seams extracted and traffic 100% on services.
This plan minimizes risk by strangling the monolith incrementally—each cut is a small, testable step toward full decomposition.