Video in progress
If two services or modules read and write the same database tables, what hidden coupling does that create, and how do you get out of it?
A shared table is the strongest and most invisible contract: column names, types, even status values become the API of both sides. If one team runs a migration, the other breaks in prod. The way out is for each piece of data to have one owner, and for the others to get it through an API, an event or a read-only view.
📖 Answer
A typical story: we split the billing service out of the monolith, but it still reads the orders table directly and updates orders.payment_status. The result:
- the
ordersteam moved thestatuscolumn from an enum to a lookup table, and billing crashed overnight; - two services update the same row in different transactions: a lost update;
- the
ordersinvariants (status transitions) are bypassed by billing: encapsulation is broken at the data level.
This is the architecture-level version of a "public field" in OOP.
The exit plan (in stages, not in one day):
- Assign ownership. Each table has one owner. Others don't write. This is the first and most important step. We move the writes to the owner's API (
POST /orders/{id}/payment-confirmed). - Separate the reads. If reads are needed, the owner provides a stable contract: a DB view (versioned,
orders_v1), an API, or an event from which the consumer builds its own copy. - A technical barrier. A separate DB user and schema for each service, with no write permission on another schema.
GRANTprotects you, not an agreement. - Observation. Find out which service touches which table through DB audit logs or
pg_stat_statements: dependencies that nobody documented often turn up.
An exception: in a modular monolith one DB is fine, as long as each module has its own schema and JOINs against another module's tables are forbidden. For reporting, use a separate read model or a DWH, not direct SQL against operational tables.
Sign up to mark studied questions and save your progress.