microservice.guru
← All posts

Database per Service: Who Owns the Data?

· Architecture · Microservices · Data · Ownership

By Alain Cyr, Founder, Koesion

microservice.guru — Database per service: who owns the data? Three services sharing one schema, versus three services that each own their store.

How to Find Your Microservice Boundaries ended with services that each own a business capability. This post is about the place where that ownership usually leaks first, which is the database.

The join that looked free

Claims needs to know whether a policy was in force on the date of loss. The policy tables sit in the same Postgres instance, two schemas over. The developer has a ticket due Thursday and a perfectly good JOIN at hand. It ships in an afternoon, it is fast, and nobody objects in review because the query is correct.

Six months later Billing does the same thing, for the same reason.

Three services, one database: Claims and Billing both join against tables that belong to Policy

Each of those rose arrows was a reasonable decision on the day it was made.

Now the Policy team wants to split policy into policy and policy_version, because endorsements have turned the table into a mess. They cannot. They do not know who reads it, the people who do know are on two other teams, and the only reliable way to find out is to ship the change and watch what breaks.

What you actually shared

A shared database does not share data. It shares a schema, and the schema becomes an API that nobody designed, nobody versions and nobody can deprecate. Column names turn into contracts. So do nullability, the meaning of status = 'S', and the fact that cancelled policies are kept in the table rather than moved out.

It also shares the operational side. A long report from Billing holds locks that slow down claim intake. One team’s migration window is everybody’s. And when the instance has a bad afternoon, three services go down together and the incident channel has thirty people in it.

You can live like this for years. Plenty of companies do. But you do not have three services. You have one application with three deploy pipelines.

One owner per table

The rule itself is short. A service owns its data, and the only way to that data is through the service.

Three services, each with a private store; Claims calls the Policy API, Billing consumes Policy events, and direct table access is crossed out

The schema is private. What other teams depend on is the API and the events.

“Database per service” scares people because they picture forty database servers. That is not what it asks for. A schema per service on the same instance, with separate credentials and no cross-schema grants, gives you the property that matters: the owner can change its tables without asking permission. Physical separation can come later, when a service needs its own scaling or its own engine.

“But I need that join”

You do still need the data. There are three ways to get it, and they suit different queries.

Ask the owner. Call the Policy API when you need the answer. This is the right default for a single record at request time: is this policy in force on this date? The cost is runtime coupling. If Policy is down, so is your screen, and a list of fifty claims should not turn into fifty calls.

Keep a copy. Policy publishes an event when a policy changes. Claims keeps the handful of fields it uses in a table of its own, updated from those events.

Policy publishes PolicyChanged; Claims consumes it and maintains a local policy_snapshot table next to its own claim table

Five columns, not the whole policy. Claims copies what it reads and nothing else.

The copy runs a few seconds behind, and you have to decide that this is acceptable. For “show the policy number and the coverage on the claim list”, it is. For “may we pay this claim”, ask the owner. The copy is also read-only: if Claims ever updates policy_snapshot by hand, you are back to two writers and no owner.

Ship it out. The quarterly loss-ratio report that joins claims, policies and payments is not a service’s job. Send the data to a warehouse and let analysts join there. Reporting is a different consumer with different needs, and it should not dictate how operational services store things.

Comparison of asking the owner, keeping a copy, shipping data to a warehouse and reading the owner's tables, by freshness, coupling and use

The last row is the one you already have.

What it costs

Three things, and it is better to say them out loud.

You lose transactions across services. Registering a claim and reserving money for it can no longer be one COMMIT. That is what sagas are for, and they get their own post.

You lose foreign keys across boundaries. claim.policy_id is now just a value. One day it will point at a policy that was merged or purged, and your code needs an answer for that.

And you lose the single place where anyone can query anything. Some people will miss it a great deal. Give them the warehouse early, or they will ask for read access “just for now”.

Getting out of a shared schema

Not in one move. The order that works:

  1. Find out who reads what. Database grants and a week of query logs will tell you more than any architecture diagram;
  2. Give each service its own credentials. Nothing changes yet, but you can finally see the traffic per service;
  3. Pick one table and revoke foreign access to it. Replace each read with an API call or a copy. A database view can serve as a temporary contract while you do it;
  4. Repeat. Move the schema to its own instance only if something forces you to.
If two services can write the same table, you have one service. Decide who owns each table before you decide where it runs.

Where to go next

References