Skip to content

Monolith & microservices · 2 of 4

The database every service shares

Step 1 · The pattern

Two services, one database

Orders and Reports are separate services, run by separate teams, but both use the same database. Reports reads the orders tables directly; it is quicker than asking Orders for them. On a normal day nobody notices. (All numbers are illustrative.)

  • an order
  • a report query
  • answered

In short

The database every service shares: how it works and how it breaks

Splitting code into services is the easy part; splitting the data is the hard part, so it often stays behind. Orders and Reports become separate services that still read and write the same database. They are still coupled, twice. A schema change in one breaks the other, which read the table directly, so the two teams must deploy together: a distributed monolith, with the network calls of services and the lockstep of one app. And they share capacity: a heavy report at month end takes the connections and the disk that checkouts need. The fix is that each service owns its data and no other service touches it. Others ask through its API, or keep their own copy fed by events it publishes, accepting that the copy is a few seconds behind.

What this simulation simplifies

  • The orders database here holds six queries at once and a report query takes 2.5 s; real systems are larger, but the contention is the same shape.
  • The reports copy is updated by one small event per order with no delay shown; real event pipelines add seconds of lag, more during incidents.

How this comes up in interviews

"Your services share one database. What problems does that cause, and how would you split the data?"

Then they ask

  1. How does another service read data it no longer owns?
  2. What do you give up with a copy fed by events?
  3. How do you keep the event and the write consistent?

A strong answer covers

  • A shared database couples deploys: the schema becomes a contract between teams.
  • It couples capacity: one service's heavy work starves the others.
  • Each service owns its data; others use its API or keep their own copy from its events.
  • Copies lag by seconds or more; anything that must be current asks the owner.
  • Use an outbox or change data capture so writes and events cannot drift apart.

Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.

On one page: the track cheat sheet.

Last updated: Official documentation

Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.