wasAsked.

How would you split a Spring Boot modular monolith into microservices without coupling every deployment?

Razorpay · Full Stack Developer · 3-4-years · technical

Published

Year not specified · Java, Spring Boot, Microservices

Community answers

Posted by wasAsked editorial collection

The goal is independent deployability: each service can change and release without coordinating with others. 1. Start with a well-modularised monolith - Define bounded contexts with domain-driven design: Orders, Payments, Catalog, Shipping. - Enforce boundaries inside the monolith first: modules communicate through public APIs or application events, never by accessing each other's internal classes or tables. - Spring Modulith helps verify module structure (ApplicationModules.verify()), test modules in isolation, and publish events between modules with an event publication registry (an outbox). - Separate data per module (own tables or schemas) even before splitting. 2. Decide what to extract and why - Extract when there is a concrete benefit: independent scaling, different release cadence, separate team ownership, or fault isolation. Pick a module with clear boundaries and few synchronous dependencies first. 3. Data ownership - Each service owns its database; no shared tables. Others obtain data through APIs or events, and keep local read models when they need data frequently. - Use the transactional outbox (or Debezium change data capture) to publish events reliably. 4. Communication design - Prefer asynchronous events (Kafka) for propagating state changes; use synchronous REST or gRPC calls sparingly, with timeouts and circuit breakers. - Avoid chains of synchronous calls: they couple availability and deployment. 5. Contracts and versioning - Treat APIs and event schemas as public contracts: additive changes, versioning, schema registry (Avro or Protobuf with compatibility rules), and consumer-driven contract tests (Pact, Spring Cloud Contract) so services can deploy independently. - Avoid shared libraries containing domain models; share only small, stable technical libraries. 6. Migration approach: strangler fig - Route traffic through an API gateway (Spring Cloud Gateway); move one capability to the new service, run in parallel if needed, then remove the old code. 7. Platform prerequisites - Independent CI/CD pipelines per service, containerisation, centralised logging and tracing (OpenTelemetry), service discovery and configuration, and automated testing. Warning signs of a distributed monolith - Releases require deploying several services together; a change in one service's model breaks others; synchronous calls fan out widely; services share a database.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics