Course map

Topic 146 / Advanced

Database per service

Chapter 9.2 · Decomposition and Boundaries

What and why

If two services can still reach into the same tables, decomposition (→ 144) hasn't actually created independence — a schema change in one breaks the other, and both must still release together. Database per service makes each service the sole owner of its data: no other service touches its tables directly, ever; everything crosses the boundary through the service's API or its published events. Builds on: → 145 (service boundaries and ownership).

Diagram

Wrong                                    Right
─────                                    ─────
[Order Service]──┐                       [Order Service]──[Orders DB]
[Shipping Service]┼──[Shared DB]                │ API / events
                  ┘                              ▼
   both read/write the same tables       [Shipping Service]──[Shipping DB]
   → a schema change breaks the other          (own schema, own technology)

How it works

  1. Give each service its own schema or database instance; revoke every other service's direct access.
  2. Choose storage per service's own needs — polyglot persistence: a relational store for orders, a document store for a catalog, a graph store for a recommendation graph (→ 62 informs the per-service choice).
  3. Expose data to other services only through the owning service's API (synchronous read) or its published events (→ 113) — never a backdoor query.
  4. Accept that a query spanning two services' data (a report joining orders and shipments) can no longer be one SQL JOIN — it becomes either an API composition, a read model built from events (→ 155), or a batch job reading each service's export.
  5. Handle writes that must stay consistent across two databases without a cross-database transaction: the saga pattern (→ 139) with compensating actions, or the transactional outbox (→ 118) to publish the change reliably.

Practical example

(illustrative). "Cancel order" must not deduct anything from Shipping's database directly — Order Service updates its own orders row, then publishes an OrderCancelled event; Shipping Service consumes it and updates its own shipments row. There is a brief window where Order shows "cancelled" and Shipping still shows "in transit" — the system is eventually consistent by design, and the saga (→ 139) defines what happens if Shipping's compensating action (recall the shipment) fails.

When to use it

  • Use when: services have already been split (→ 144) and are owned by separate teams (→ 145) — shared data ownership would quietly re-merge them.
  • Avoid when: the services are still one team's modules inside a monolith — a shared database with enforced schema ownership per module is the modular-monolith equivalent, with none of the cross-database consistency cost.
  • Failure scenarios: the "integration database" anti-pattern (several services reading and writing one shared schema "temporarily") becomes permanent and recreates the distributed monolith (→ 143); a report that silently depends on another service's internal table breaks the moment that service refactors it.
  • Simpler alternative: one shared database with per-module schemas and access control, inside a modular monolith (→ 53) — gives most of the ownership clarity without distributed transactions.

Practice

(Pattern kind — implement minimal, spot the misuse; 25 min, shared lab: Postgres, two schemas). Create two Postgres schemas, orders and shipping, each with its own table and its own database role that can only access its own schema. Implement "cancel order" as: update orders, then write a row to an orders.outbox table in the same transaction (→ 118). Then spot the misuse: write a query that joins the orders and shipping tables directly across schemas, and explain which future change it would silently break.

Done when the cross-schema JOIN is identified as the violation and replaced with a design that goes through the outbox instead.

Shared lab setup

Key takeaway

Database per service is what makes services actually independent — and what forces cross-service consistency onto events and sagas instead of transactions. Done when you can: design a cross-service write with database per service, picking outbox, event, or saga as the integration path. Used next in → 147 (inter-service communication), → 155 (CQRS read models).

References

A pattern language for microservices — see references.md.

Source directory

Search the guide