Scrivio Interview library
ScenarioAdvanced7 min read · updated 12 November 2026

Several services read and write the same database tables directly, so no service clearly owns the data. Plan the move to one owner per aggregate, accessed through that owner's repository and API.

We first map every table to the aggregate and the team that should own it, then make each owner expose APIs or events and get other services to stop writing to its tables first. Readers then move to those APIs, to events, or to read-only replicas built with change data capture, and once a table has no outside callers we enforce ownership with database permissions. We do this in stages with a timeline, track the remaining direct access as a number that goes down, and expect some cross-table queries to need new read models.

Why this question is asked

A shared database is the most common reason a microservice architecture behaves like a distributed monolith. Any team can break any other by changing a column, nobody can say who is responsible for a bad row, and schema migrations need a meeting of five teams. Interviewers ask senior engineers this scenario because the target state is easy to describe and the migration is hard to do without stopping delivery. In this answer, we will map ownership, explain why writes move before reads, look at the options for readers, see how to lock the doors with permissions, and organise it all into stages with measurable progress.

1Why Shared Tables Hurt

When several services write the same tables, the rules that keep the data correct are spread across all of them. An order's status might be set by the order service, the warehouse service and a nightly batch job, each with its own idea of which transitions are allowed. That breaks the core DDD idea that an aggregate's root, and the team behind it, enforces its invariants.

It also freezes the schema. Renaming a column or splitting a table means finding and changing every service that touches it, and releasing them together. So teams stop improving their data model, and every new feature adds a nullable column instead.

2Map Every Table to an Aggregate and an Owner

The first deliverable is an inventory. For every table, we record which aggregate it belongs to, which team should own it, and which services currently read or write it. The database itself helps: query logs, pg_stat_statements in PostgreSQL, or the performance schema in MySQL show which database users run which statements, especially if each service has its own login. Code search for table names fills the gaps.

Assigning owners is a business conversation as much as a technical one. The orders and order_lines tables belong to the Order aggregate and the ordering team; stock_levels belongs to Inventory. Some tables will turn out to hold two aggregates mixed together, which we note for later splitting. The result is an ownership map that everyone can see.

An example ownership map
TableAggregate and ownerOther writersOther readers
orders, order_linesOrder, ordering teamwarehouse, billing batchreporting, support
stock_levelsInventory, warehouse teamorderingstorefront
customersCustomer, accounts teamsupport toolordering, billing, marketing
invoicesInvoice, billing teamnonesupport, reporting
Outside writers are the first problem to remove; outside readers come next.

3Owners Expose APIs and Events, Writers Move First

Each owner now offers a proper way in: commands on an API, such as POST /orders/{id}/ship, and events, such as OrderShipped, published reliably through an outbox. The API methods are business operations that go through the aggregate root, not generic row updates.

Writers move before readers, and the reason is risk. A foreign write can break the owner's invariants and corrupt data; a foreign read can at worst show stale or misunderstood data. So the warehouse service stops running UPDATE orders SET status = 'SHIPPED' and calls the ship command instead, and the billing batch calls an API or reacts to events. Until all writers are gone, the owner cannot safely change its schema or rely on its own rules.

SQL
-- Before: the warehouse writes the ordering team's table directly
UPDATE orders SET status = 'SHIPPED', shipped_at = now() WHERE id = :id;
// After: the warehouse calls the owner, which checks the rules
orderClient.ship(orderId, new ShipCommand(trackingNumber)); // 409 if the order is cancelled

4Move Readers to APIs, Events or Replicas

Readers have three options, and which one fits depends on how they read. A service that needs one order at a time, during a request, calls the owner's API. A service that needs its own copy to work with, such as billing keeping order totals, subscribes to events and builds a local model. A reader that runs large queries, such as reporting, gets a read-only copy built with change data capture, CDC, which reads the database's transaction log, for example with Debezium, and streams the changes into a warehouse or a replica schema.

Notice that CDC replicas still expose the owner's table structure, so they create a weaker coupling: the owner can still break readers by changing a column. For that reason, owners often publish a curated view or an event stream as the contract, rather than raw tables.

Step throughChoosing the path for a reader
  1. Does it need data during a request, one item at a time?
    YesCall the owner's APINoNext question
  2. Does it need its own working copy?
    YesSubscribe to events, build a local modelNoNext question
  3. Large analytical queries?
    YesCDC into a warehouse or replicaNoRevisit the need with the owner

3 steps in order. Play them, or step through them one at a time.

5Enforce Ownership With Permissions

Agreements decay, so once a table has no outside callers, we make the database enforce it. Each service gets its own database user, and only the owner's user has INSERT, UPDATE and DELETE on its tables. In PostgreSQL that can be a schema per owner with grants such as GRANT SELECT to the reporting user while a transition is still running, and finally no grants at all to other services. A new direct access attempt now fails in testing instead of quietly creating coupling again.

Revoking write permissions first, table by table, also gives a useful safety check: if something breaks, we have found a caller the inventory missed.

6Running It in Stages and Tracking Progress

This is a programme of many months, so it needs a timeline and a visible measure. A simple one is the count of foreign accesses: pairs of a service and a table it does not own, split into writes and reads. We publish it every sprint, and each team owns the reduction of its own outgoing accesses. A plausible plan is writes removed for the most critical aggregates in the first quarter, all foreign writes in two quarters, and reads moved over the following two, with permissions tightened as each table becomes clean.

We should also expect some queries to have no easy home. A support screen that joins orders, customers and invoices in one SQL statement cannot be replaced by three API calls without becoming slow. Such needs get a new read model, a denormalised view built from events by the team that needs it, or a query service owned by one team. Budgeting for these up front prevents the programme from stalling on the last ten percent.

7Follow-up questions

What about foreign keys between tables owned by different teams?

They have to go once the tables are owned separately, because a foreign key is a direct dependency on another team's schema. The reference stays as an id column, and integrity is maintained by the owning services through validation and events, which is the usual consistency model between aggregates.

Should every owner get its own database server?

Not necessarily. Separate schemas and users on the same server already give ownership and permission enforcement. Moving to separate servers is a later decision about scaling and isolation, and it is far easier once no service touches another's tables.

How do you handle a nightly batch that updates many tables?

Break it into the owning teams' responsibilities. Usually each part becomes a bulk command on the owner's API or a job run by the owner. If it truly needs a cross-aggregate transaction, that is a sign the boundaries or the process need another look.

8Conclusion

In this answer, we planned the move from shared tables to one owner per aggregate. We mapped every table to its aggregate and owning team, had owners expose APIs and events, and moved writers off foreign tables first because writes can break invariants. Readers then moved to APIs, events or read-only replicas built with change data capture, and database permissions enforced ownership once callers were gone. We ran it in stages with a timeline, tracked remaining direct access as a number, and planned new read models for queries that span owners.

What to say in the interviewAbout 100 seconds aloud

Open with

I would start with an inventory: map every table to the aggregate it belongs to and the team that should own it, and record which services read or write it today, using database logs and statement statistics as well as code search.

Then, if they want more

That ownership map is the plan everyone works from. Then each owner exposes proper ways in: business commands on an API and events published through an outbox. The first goal is that nobody else writes to an owner's tables, because foreign writes bypass the aggregate's rules and are where corruption comes from.

So the warehouse stops updating the orders table and calls a ship command instead. Next, readers move.

If they need one item during a request, they call the API; if they need their own copy, they subscribe to events; for heavy analytical reads, we build read-only copies with change data capture, for example Debezium into a warehouse, ideally from a curated view so the owner can still change its schema.

Once a table has no outside callers, I enforce it: each service has its own database user, and only the owner can write, later only the owner can read. I would run this in stages over several quarters with a visible metric, the number of service-to-foreign-table accesses split into writes and reads, reviewed every sprint.

And I would budget from the start for queries that joined across owners, because some of them will need new read models.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Several services read and write the same database tables directly, so no service clearly...": https://scrivio-interview-library.netlify.app/java/domain-driven-design/62dc37c306/