How do you approach a Monolith-to-Microservices migration?
Topic: microservices, Migration. Level: Hard.
Answer
Migrate incrementally, never big-bang. The **Strangler Fig** pattern routes traffic through a facade/proxy and peels off capabilities one at a time into new services until the monolith is "strangled." Steps: identify bounded contexts, extract the highest-value/lowest-risk capability first, split its data (the hardest part — decouple the shared DB with anti-corruption layers and events), run old and new in parallel, verify, then cut over. Keep the monolith the source of truth until each slice is proven. Don't migrate if the monolith isn't actually the bottleneck.
```java
// Strangler Fig: facade routes each capability to old or new implementation
@GetMapping("/orders/**")
Object orders(HttpServletRequest req) {
return featureFlags.isEnabled("orders-microservice")
? newOrderService.handle(req) // extracted slice
: legacyMonolith.forward(req); // not yet migrated
}
// Flip the flag per capability once the new service is verified.
```