Course contents

11. Microservices Lesson 2 of 10

Service Boundaries

Why cutting services by data entity creates chatty call chains and shared-database coupling, and how capability boundaries with owned data fix it.

16 minIntermediate7 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Entity or layer cuts turn one business operation into a chain of synchronous network calls.
  • Every synchronous dependency adds latency and adds its downtime to the request (0.999ⁿ).
  • Services that share a database are coupled at runtime and at every schema change.
  • Capability boundaries keep logic and data that change together in one service; others get the data via API or events.
  • A good boundary is crossed rarely by user requests and rarely by feature changes.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

A service boundary is the line that decides which logic and which data live in which service. Everything inside a boundary can change and deploy together; everything that crosses it becomes a network call or a message, with a contract that two teams have to agree on.

Where the lines go matters more than how many services there are. Good boundaries make most requests and most feature changes stay inside one service. Bad ones produce a distributed monolith: many deployables that still have to change, release and run together.

Why it exists

Splitting a system is only worth its cost (network calls, partial failures, distributed data) if the parts can then be changed, scaled and operated independently. That independence comes from the boundaries, not from the deployment unit. Two services that share tables, or that call each other several times for every request, keep all the coupling of a monolith and add the network on top.

How it works

Cut by business capability. A capability is something the business does: take orders, manage inventory, maintain the catalog, collect payments, ship parcels. Capabilities are relatively stable, have an obvious owner, and keep the rules and data that change together in one place. Domain-driven design gives a vocabulary for this: a bounded context is a part of the domain in which a model and its words have one meaning ("product" in Catalog has a description and images; in Inventory it is a SKU with a stock count). Bounded contexts are good boundary candidates.

Avoid entity and layer cuts. "One service per table" (Customer, Product, Price, Stock, Order) or "one service per layer" (UI, logic, data) spread every business operation across several services. A checkout then needs five synchronous calls, and every feature touches several teams.

Count the hops on the request path. Latency adds up along a synchronous chain, and so does failure. If a request needs n independent services, each available a fraction a of the time, the request can be available at most aⁿ: five services at 99.9% give about 99.5%, roughly five times the downtime of one. Chatty patterns (a call per item in a loop, back-and-forth between the same two services) multiply this further.

Give each service its own data. A service's tables are private. Others get its data in one of two ways:

  • Ask the owner through its API, when they need the current value and can tolerate the dependency.
  • Keep a copy fed by the owner's events (price changed, product renamed), when they need the data often and can accept that it is briefly behind. The copy is shaped for the reader's needs, not a mirror of the owner's schema.

Publishing events reliably usually needs the transactional outbox; multi-service business transactions need sagas rather than distributed locks.

Align with teams. Conway's law: systems end up shaped like the communication structure of the organisation that builds them. A boundary that one team owns end to end stays clean; a boundary that cuts through a team's daily work erodes.

When to use it

  • When splitting a monolith, or deciding whether new functionality is a new service or part of an existing one.
  • When a "microservices" system shows lockstep releases, chatty calls or a shared database: those are boundary problems.
  • When designing data flows: decide which service owns each piece of data before deciding how others read it.

When not to

  • Do not draw service boundaries before you understand the domain. Early on, boundaries move; inside a modular monolith moving them is a refactoring, across services it is a migration.
  • Do not split purely for size or fashion. A capability that one team owns and that scales uniformly is fine as one service.

Trade-offs

Choice You gain You pay
Capability boundaries Requests and features mostly stay in one service Harder to find; needs domain knowledge and team buy-in
Database per service Independent schemas, isolated load No cross-service joins or transactions; data copies and events
Local copy fed by events No runtime dependency, fast reads Eventual consistency; one more consumer to run and monitor
Synchronous API call for data Always the current value Latency and availability coupled to the owner
Fewer, larger services Fewer hops and failure modes Larger teams per service, coarser scaling

Alternatives

  • Modular monolith: the same boundaries as modules with enforced interfaces inside one deployable. Most of the design benefit, none of the network cost; split later where it pays off.
  • Shared database, separate schemas per module: a stepping stone that enforces ownership by access rules while still running on one server.
  • Merge services back: when two services always change together and talk constantly, merging them is a legitimate fix.

Failure modes

  • Distributed monolith: services that must be released together because they share types, tables or tightly ordered calls.
  • Chatty chains: one request fans into many sequential calls; latency and outage windows add up (aⁿ).
  • Shared-database contention: one team's batch job or migration exhausts connections or locks for everyone.
  • Integration through the database: a service reads another's tables "just for a report", and the owner can no longer change its schema.
  • Entity services: a "Customer service" that every request needs becomes a bottleneck and a single point of failure.
  • Stale copies treated as truth: a local copy used for decisions that need the current value (for example, final stock reservation) without checking with the owner.

In production

Teams find boundaries with event storming or capability mapping, then validate them against real traffic: trace a few key requests (distributed tracing shows the call graph) and count synchronous hops. Track which services change together in version control; frequent co-changes point at a misplaced line. Migrate a shared database gradually: give each table one owning service, route other readers through APIs or events, then move the tables. Record the price, address or other facts a service used at decision time with its own data (an order stores the price charged), so later changes in the owner do not rewrite history.

Common misconceptions

  • "One service per entity is clean design." It mirrors the data model, not the business; every operation becomes a call chain.
  • "Separate deployments mean independent services." Not if they share a database or must be released together.
  • "Copying data between services is wrong." Deliberate, owned copies fed by events are the normal way to avoid runtime coupling; uncontrolled shared tables are the problem.
  • "Smaller is better." Granularity should follow capabilities and teams. More services mean more hops, more failure modes and more coordination.

Interview angle

When you split a design into services, say why each line is where it is: name the capability, the owning team and the data it owns. Interviewers probe: how does Ordering get prices (API call versus event-fed copy, and the consistency trade-off), what happens to a checkout if Catalog is down, how many synchronous hops a key request makes, and how you would migrate off a shared database. Quoting aⁿ (five services at 99.9% ≈ 99.5%) shows you understand why hop count matters.

Model assumptions

The simulation is a simplified model, not a benchmark. Checkout does 5 ms of work, then calls each dependency one after another and holds a worker while it waits; entity services do 8 ms of work plus one database query (4 ms reads, 20 ms writes). Network hops take 3–4 ms between services and databases, and 20 ms between shoppers and Checkout. The shared database has 8 connections and a 200 ms pool timeout. The price import is a client that only writes. Service outages are scripted redeploys, not random. Ordering's local price copy is represented by the price updater consuming events; its effect on Ordering's data is not modelled. Arrivals are random (Poisson). Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. Services cut along data entities

    A shop was split into one service per noun in the data model: Customer, Product, Price, Stock and Order. It looks tidy on a diagram. Every service still reads the same shared database.

    One checkout needs all five: Checkout calls them one after another, five synchronous network calls per order. The median checkout takes about 175 ms, most of it spent travelling between services rather than doing work.

    Scene: Shoppers send 40 checkouts per second to Checkout, which calls Customer, Product, Price, Stock and Order services in turn; all five read one shared database. Checkouts succeed in about 175 ms.

    Key idea: Splitting by data entity turns one business operation into a chain of network calls.

  2. 2. Every outage becomes a checkout outage

    At 1 s the Customer service is redeployed and is down for a second. At 3.5 s Price has its turn. Each time, every checkout fails, although the other four services are fine.

    The checkout depends on five services, so it inherits all of their downtime: the outage windows add up. If each service were available 99.9% of the time, the checkout would be available only about 99.5% (0.999⁵). Chattiness is not only slow; it multiplies the ways a request can fail.

    Scene: Customer goes down for a second, then Price. During each outage every checkout fails; between them checkouts succeed again.

    Key idea: A request that needs N services synchronously is down whenever any of them is: failure probability grows with every hop.

  3. 3. The shared database couples everyone

    The Product team starts a price import that writes about 450 rows per second straight into the shared database. Writes are slow, and the database has only eight connections. Within a second the pool is exhausted.

    Checkout code did not change, nor did the five services, yet checkouts start to time out waiting for a database connection. Services that share tables are not independent: one team’s batch job is every team’s outage, and nobody can change a table without asking everyone else.

    Scene: A price import job writes heavily into the shared database. Its connection pool runs out and checkouts start failing with database pool timeouts, though no checkout code changed.

    Key idea: A shared database is a hidden dependency: it couples services at runtime and at every schema change.

  4. 4. Redraw the lines around capabilities

    The services are regrouped around business capabilities, the things the business does: *Ordering* (taking an order), *Inventory* (reserving stock) and *Catalog* (products and prices). Each owns its data in its own database.

    Ordering keeps a local copy of the prices it needs. Catalog publishes a *price changed* event whenever a price changes, and Ordering’s updater applies it. A checkout now needs Ordering’s own database and one call to Inventory: two dependencies instead of six, and the median checkout drops to about 85 ms.

    Scene: Ordering reads its own database and calls Inventory, which has its own database. Catalog, with its own database, publishes price events that Ordering’s price updater consumes. Checkouts succeed in about 85 ms.

    Key idea: A good boundary keeps the data and logic of one business capability together, so most requests stay inside one service.

  5. 5. The import only hurts its owner

    The same price import runs again at 450 writes per second. This time it lands on Catalog and its own database, which cannot keep up: Catalog’s queue fills and it rejects many import writes. The error tile counts those failed writes.

    Checkouts do not notice: none fails and their latency stays where it was. They never touch Catalog’s tables; the price changes that do get through reach Ordering as events. Data ownership turns a cross-team outage into one team’s problem, and lets each team change its schema without a meeting.

    Scene: The price import overloads Catalog and many import writes are rejected. Checkouts keep succeeding: Ordering never reads Catalog’s database.

    Key idea: Each service owns its data; others get it through the owner’s API or events, never through its tables.

  6. 6. Catalog goes down, checkout carries on

    At 1 s the Catalog service goes down. The import job’s writes fail, and no new price events are published.

    Checkouts keep succeeding at the last known prices, because Ordering never calls Catalog while a shopper waits. Compare that with the second step, where any one of five services failing stopped every checkout. Use the sandbox to fail Inventory instead: it is still on the checkout path, so that one would hurt.

    Scene: Catalog goes offline. Import writes fail, but every checkout still succeeds using Ordering’s local copy of prices.

    Key idea: Fewer synchronous dependencies on the request path means fewer outages that users see.

  7. 7. How to judge a boundary

    Good boundaries follow business capabilities: things that change together and that one team can own end to end. Test a proposed cut by asking how often a user request has to cross it synchronously, and how often a feature change needs two teams. Both should be rare.

    Each service owns its data. Others get it through the owner’s API or by keeping a copy fed by events, accepting that the copy can be slightly behind. When unsure, keep things together: merging two services later is harder than splitting a well-structured module.

    Scene: Ordering, Inventory and Catalog each run with their own database; price changes flow from Catalog to Ordering as events. Everything is healthy.

    Key idea: Cut where requests and changes rarely cross; give each side its own data.