Microservices: when to split, finding service boundaries, and carving up a monolith step by step
The pros and cons of microservices, the core components and the extras, the path from event storming to bounded contexts, six rules for splitting services, running old and new side by side, and data ownership.
On this page
1. The problem
A team splits the monolith by table: user-service, order-service, order-item-service. Adding a discount field means deploying 4 services in order; creating one order takes 6 synchronous calls.
2. Key idea
Microservices solve an organisational problem (many teams editing one codebase) more than a technical one.
| Pros | Cons |
|---|---|
| Independent deploys, one team owns one service | Operations: monitoring, debugging across services |
| Scale the hot part separately | Network latency; a chain of 99.9% hops drops to ~99.7% |
| Fault isolation (if designed right) | Inconsistent data across databases |
| Architecture | Use when | Don't use when |
|---|---|---|
| Modular monolith (clear modules, checked with ArchUnit) | the default; small teams, new products | many teams trip over each other daily |
| Microservices | ≥ 3–4 teams, very different scaling/deploy rhythms, fault isolation needed | no CI/CD, tracing or ops people yet |
2.1 How do you find boundaries?
Boundaries come from the business, not from tables or technical layers. Event storming: domain experts and engineers stick events ("Order placed", "Paid") on a wall in time order; clusters of events reveal subdomains. A bounded context is a boundary within which one model and vocabulary stay consistent: "Product" in Catalog (description, images) differs from Inventory (quantity) and Pricing (price).
2.2 Rules for splitting
- High cohesion, low coupling: things that change together live in one service.
- Big first, small later: merging two split services costs more than splitting one big one.
- Limit distributed transactions: an order and its lines stay in one service.
- Periphery first (Mail) to practise the process; or core first (User) to clear the way, at higher risk.
- Run side by side: the old module (API v1) and the new service (v2) behind a gateway, shifting traffic gradually, reversible: the strangler fig.
- Each service owns its data: others read through an API or a copy built from events, never querying each other's tables.
3. Worked example
Kubernetes already provides discovery and load balancing, so you usually don't need Eureka. What's left is routing for the side-by-side run:
# Spring Cloud Gateway: strangler fig for the User module
spring.cloud.gateway.routes:
- id: users-v2
uri: http://user-service # new service, gradually takes traffic
predicates: [Path=/api/v2/users/**]
- id: users-v1
uri: http://monolith # old module, still the writer at first
predicates: [Path=/api/v1/users/**]Steps for the v1/v2 side-by-side run
- The gateway routes v1 to the monolith and v2 to the User service.
- Sync data: the User service reads a copy via CDC on the
userstable; the monolith is still the writer. - Shadow reads: the gateway mirrors read requests to v2 without returning its results, logging differences until they reach ~0.
- Shift reads: 1% → 10% → 100% to v2 behind a feature flag.
- Shift writes: the User service becomes the writer and publishes
UserUpdated; the monolith listens until the old module is removed. - Remove v1 when gateway logs show nobody calls it.
Every step reverts with one config flag. Synchronous call chains need timeouts, circuit breakers and bulkheads on each call.
4. Working with AI
Delegate to AI: turning event-storming notes into a table, writing ArchUnit rules, gateway and CDC configuration.
Check yourself:
- Service boundaries follow the business, not tables or layers.
- No service reads another service's tables directly.
- No "common" library holding shared entities.
- A feature usually touches one service; deploys need no fixed order.
5. Exercises
Exercise 1: Split a social-network monolith
The monolith has User, Post, Reaction, Comment, Feed, Mail, Search. 25 people in 4 teams. Feed and Search carry heavy load. AI proposes "7 modules into 7 services this sprint". Propose an order and data boundaries.
Solution to Exercise 1
- Step 0: a modular monolith with ArchUnit in CI; use git log to see which modules change together.
- Step 1, periphery: split Mail/Notification (listening to
CommentCreated,UserFollowed) to practise the pipeline, tracing and outbox. - Step 2, core: split User with a v1/v2 side-by-side run; other modules keep a small copy (name, avatar) via
UserUpdated. - Step 3, by load: Search (listening to Post events) and Feed: very different scaling, read models rebuildable from events.
- Keep together: Post, Reaction, Comment as Content: they change together and belong to one team.
- 7 services at once means 4 teams running 7 systems, and almost certainly a distributed monolith.
Exercise 2: Spot the distributed monolith
Three signs: a feature deploys 4 services in order; a shop-common library holds Order and User entities; report-service reads order-service's DB directly. How do you fix each?
Solution to Exercise 2
- Ordered deploys: those services change together, so merge them; or evolve APIs backward-compatibly (add fields, keep old ones) so they deploy independently.
- Shared entities: each service defines its own model; share only contracts (OpenAPI, event schemas).
- Direct DB reads:
report-servicelistens to events and builds its own read model, or calls the API.
Summary
- Microservices solve an organisational problem; the default is a modular monolith.
- Boundaries follow the business: event storming → bounded context → service.
- Big first, small later; what needs one transaction stays in one service.
- Each service owns its data; split gradually with the strangler fig.
- Splitting by table or layer gives a distributed monolith.
Practice interview questions on this topic
- How do a monolith and microservices differ, and when should you split?
- Which principles do you follow when designing microservices?
- How do you define microservice boundaries, why should each service own its database, and what problems does that create?
- How would you split a monolith running in production into microservices, step by step?
- When should services call each other synchronously, and when asynchronously via messages?
- How does service discovery work? How does Eureka differ from a Kubernetes Service?
Comments
Community rules