Skip to content
notes.vinhnq.com

Search

System DesignSenior13 min read

Microservices: when to split, finding service boundaries, and carving up a monolith step by step

Review 11 cards
On this page

Microservices in system design series: 1. Service boundaries · 2. Distributed transactions: 2PC, saga, outbox · 3. Case study: a liveness system

Wrong boundaries are the most expensive mistake in microservices. Services call each other all the time, every feature touches five repos, and transactions span several databases. This post goes from “should we split at all?” to finding boundaries and carving up a monolith without breaking daily deploys. Tactical DDD (aggregates, packages) is covered in DDD and package structure.

1. Should you use microservices at all

Key idea: microservices solve an organizational problem (many teams editing one codebase) more than a technical one. A small team on a new product usually moves faster with a modular monolith.

Pros Cons
Independent deploys: changing one service doesn’t redeploy the whole system Operations: more things to monitor, cross-service debugging, infrastructure cost
Fits the org: each team owns a service, less coordination Testing: you need contract tests and an integration environment
Scale the hot parts separately Latency: network calls instead of function calls
Pick the right tech for each problem Data is inconsistent across databases
Fault isolation: one dead service doesn’t take down everything (if designed well)

2. The components of a microservices system

Key idea: once you have many services, you need a way for them to find each other, a single entry point, load distribution, and cross-service observability. Without these, microservices are just more problems.

flowchart TD
  C[Client] --> GW[API Gateway]
  GW --> LB[Load balancer]
  LB --> S1a[Order #1]
  LB --> S1b[Order #2]
  GW --> S2[User service]
  S1a -. register, heartbeat .-> SD[Service discovery]
  S2 -. register, heartbeat .-> SD
  S1a --> DB1[(Order DB)]
  S2 --> DB2[(User DB)]
  S1a -->|event| MB[[Message broker]]
  • API gateway: the single entry point for clients. It handles authentication, rate limiting, version-based routing and response aggregation. Examples: Spring Cloud Gateway, Kong, Envoy.
  • Service discovery: services register themselves and send heartbeats; callers ask “where is order running right now?”. It comes in two flavors: client-side (Eureka + Spring Cloud LoadBalancer: the client picks an instance) or server-side (Kubernetes Service: you call a DNS name and kube-proxy spreads the load).
  • Load balancer: spreads requests across instances and drops unhealthy ones.
  • Extras: a message broker (async communication), logs and metrics, tracing (see distributed tracing with OpenTelemetry), config and secret management (Vault, Kubernetes Secret), and a service mesh (Istio, Linkerd: mTLS, retries and timeouts in a sidecar instead of in code).

Kubernetes gives you discovery (DNS), load balancing (Service) and part of a gateway (Ingress/Gateway API) out of the box, so on K8s you usually don’t need Eureka.

3. Finding service boundaries

Key idea: boundaries come from the business, not from tables or technical layers. The process: discover subdomains, define bounded contexts, map the relationships between them, then turn each context into a service.

  1. Event storming + domain storytelling: business people and engineers stick events (“Order placed”, “Payment received”, “Order delivered”) on a wall in time order, then retell the user’s story. Clusters of events reveal the subdomains.
  2. Bounded Context Canvas: for each context, write down its purpose, its language (one word has one meaning inside the context), the data it owns, the commands coming in, the events going out, and its dependencies.
  3. Context map: draw the relationships between contexts (who is upstream, who is downstream; customer–supplier, conformist, anti-corruption layer) and how they talk (sync API, events).
  4. One bounded context = one service is the starting point. If a context is still too big (several teams, very different load), split it further.

A bounded context is a boundary inside which one model and one vocabulary stay consistent. “Product” in Catalog (description, images) is not the same as “Product” in Inventory (quantity, warehouse) or in Pricing (price, promotions). That’s three contexts, three models, and possibly three services.

4. Six rules for splitting services

Key idea: split so that a business change usually touches only one service, cross-service transactions are rare, and the split doesn’t disrupt daily releases.

  1. High cohesion, low coupling: things that change together live together. Order and Finance are two services because their changes rarely overlap.
  2. Coarse first, fine later: start with a big service (say “Commerce”) and split again once the fault line is clear. Merging two services back is much more expensive than splitting a big one.
  3. Avoid or limit distributed transactions: whatever must be correct together in one transaction (an order and its lines) stays in one service. Two services that interact but don’t need a shared transaction make a good boundary. See the distributed transactions post.
  4. Split a peripheral service first: for example, pull Mail out of a monolith (User, Post, Mail) first. Few dependencies, small blast radius if it breaks, and the team learns how to run a new service.
  5. Or split a core service first: User is what Post and Reaction both call. Pull User out first, and later splitting Post or Reaction just means calling a stable service, not untangling circular dependencies.
  6. Run side by side at first: keep the User module in the monolith (API v1) and the new User service (API v2) running at the same time behind the gateway. Shift traffic to v2 gradually, compare results, and fall back to v1 on errors. This is the strangler fig pattern: the new service slowly “strangles” the old part.
Running v1/v2 side by side in detail: shadow traffic and result comparison
  1. The gateway routes /api/v1/users/** to the monolith and /api/v2/users/** to the User service.
  2. Data sync: at first the User service reads from a copy of the monolith’s data (CDC from the users table). The monolith is still where writes happen.
  3. Shadow reads: the gateway sends a copy of each read request to v2, doesn’t return v2’s result to the client, and only logs differences. Fix things until the difference is ~0.
  4. Shift reads: 1% → 10% → 100% of read traffic goes to v2 (feature flag per user).
  5. Shift writes: the User service becomes the write owner and publishes UserUpdated events. The monolith consumes them to update its own copy until the old module is removed.
  6. Remove v1 once nobody calls it (measured from gateway logs).

Every step can be rolled back with one config flag. That’s why the “dual run” matters more than writing the new service fast.

5. Data ownership

Key idea: each service owns its data (database per service). Other services read it only through an API or through a copy built from events. They never query each other’s tables directly.

With a shared database, every service depends on every other service’s schema. Changing a column means coordinating several teams, and independent deploys are gone. There are three ways to read another service’s data:

Approach When Cost
Sync API call You need the latest data, and calls are rare Runtime coupling: if the other service is down, you fail
Local copy from events (event-carried state transfer) Read-heavy, a few seconds of lag is fine Stale data; you must handle duplicate and out-of-order events
Replicated cache Small reference data that rarely changes Same as above, plus a startup dependency

Queries that combine several services (a “my orders” screen with product names and delivery status) use a CQRS read model: one service listens to everyone’s events and builds its own pre-joined read table. Writes still go through the owning service.

Exercises

Exercise 1: Splitting a social network monolith

Problem: a Spring Boot monolith has the modules User, Post, Reaction, Comment, Feed, Mail and Search. The team is 25 people in 4 groups. Feed and Search take heavy load; Post and Reaction are write-heavy. Propose a split order, data boundaries, and a side-by-side plan.

Solution to Exercise 1
  • Step 0: make it a modular monolith: one package per module, no cross-calls except through interfaces, ArchUnit in CI. Measure which modules change together (git log) to find the real boundaries.
  • Step 1, peripheral: extract Mail/Notification (listens to CommentCreated, UserFollowed). Practice the deploy pipeline, tracing and the outbox.
  • Step 2, core: extract User/Identity with a v1/v2 side-by-side run. Other modules keep user_id plus a small copy (name, avatar) updated through UserUpdated events.
  • Step 3, by load: extract Search (Elasticsearch, listens to Post events) and Feed (fan-out, Redis). These two scale on a very different rhythm from the rest.
  • Keep together: Post, Reaction and Comment start as one Content service. They change together, belong to the same group, and reaction counters are tied to posts. Split further only with a clear reason.
  • Data: one schema/DB per service. Feed and Search are read models that can be rebuilt from events.
  • Teams: Identity + Notification (1 group), Content (1), Feed (1), Search + platform (1), matching the service boundaries.

Remember

  • Explain the pros and cons of microservices and when a modular monolith is better.
  • List the core components, the extras, and what Kubernetes already handles.
  • Describe the path from event storming to bounded contexts to services.
  • Explain the six splitting rules and the v1/v2 side-by-side pattern.
  • Spot the signs of a distributed monolith, and know the ways to read another service’s data.

Interview questions

Mid

What is service discovery, and how do client-side and server-side differ?

Service discovery is how a service finds the addresses of another service’s live instances, because IPs change all the time as things scale and restart.

Client-side: the client asks a registry (Eureka, Consul), then picks an instance and balances the load itself. Server-side: the client calls a fixed address (Kubernetes Service DNS, a load balancer) and the infrastructure picks the instance. On Kubernetes you usually go server-side.

What does an API gateway do?

It’s the single entry point for clients: routing to services, authentication, rate limiting, API version switching, and sometimes aggregating several responses.

Don’t put business logic in the gateway. It turns into a new monolith that every team has to edit.

Why shouldn’t several services share one database?

Because the schema becomes a hidden contract between services: changing a table needs everyone to coordinate, independent deploys are lost, and one heavy query from one service slows everyone down.

Each service owns its data; others read it through an API or through a copy built from events.

Senior

How do you decide on microservice boundaries?

From the business: event storming to find subdomains, define bounded contexts, draw a context map, then start with one context per service.

To check a boundary: a business change usually touches only one service, cross-service transactions are rare, and one team owns it end to end. If a context is big, split it further by load or by team. Avoid splitting by table or by layer.

How would you break up a monolith that’s running in production, step by step?

Modular monolith first, then strangler fig: carve out one part at a time behind the gateway, run old and new side by side, shift traffic gradually, and always keep a way back.

Order: one peripheral service to practice the process, then a core service that many modules depend on. Data: sync with CDC at first, use shadow reads to compare results, shift reads first and writes after.

What signs tell you that you have a distributed monolith?

One feature needs several services deployed together in order, services read each other’s tables, synchronous call chains are long, and a shared library holds entities.

The fix: merge services that always change together, move communication to events, give each service ownership of its data, and add contract tests so APIs can change without lockstep deploys.

A screen needs data from 4 services. How do you avoid 4 synchronous calls per request?

Build a read model (CQRS): one service listens to the events of all 4, stores a pre-joined table for that screen, and answers in one query.

The price is a few seconds of lag, plus handling duplicate and out-of-order events (versions, idempotency). If one part truly needs fresh data, make a separate sync call just for that part, with a timeout and a fallback value.

Useful phrases for interviews

  • “Microservices mostly solve an organizational problem: many teams stepping on each other in one codebase.”
  • “I’d start with a modular monolith and enforce the module boundaries with ArchUnit.”
  • “A good starting point is one bounded context per service.”
  • “Event storming surfaced three subdomains we had lumped together.”
  • “High cohesion means things that change together live together.”
  • “Whatever has to be consistent in one transaction stays inside one service.”
  • “We used the strangler fig pattern behind the gateway and ran v1 and v2 side by side.”
  • “Shadow traffic let us compare v1 and v2 responses safely before we shifted any reads.”
  • “Each service owns its data; nobody queries another service’s tables.”
  • “If every release needs four services deployed in order, it’s a distributed monolith.”

Notes based on the System Design course (Ronin Engineer), rewritten in my own words.