LOAD-BEARINGfree
High-level designTier 5 · unit 3 of 5 · 15 min read

The shared database, and how to unpick one

The most common architecture in the working world is not a monolith and it is not microservices. It is several applications, sometimes owned by several teams, all pointed at one database. It is also the hardest coupling to remove, because unlike a shared library it is invisible: nothing in anyone's code declares the dependency. It exists only as a set of queries that someone, somewhere, is still running.

Why it happens, and why it is not stupid

It happens because it is genuinely the fastest thing to do, every single time. Need data that another team owns? The credentials are right there, and a join is three lines. No API to design, no contract to agree, no latency, no partial failure — and the data is transactionally consistent, which an API can never promise you.

Take that seriously, because it explains why the pattern is so sticky: the shared database is not laziness, it is a real set of advantages you are about to give up. Anyone proposing to unpick one should be able to name what they are trading away.

What it costs

  • The schema becomes an API that nobody designed and nobody can version. Every column is public. Renaming one requires knowing all its readers, and the honest answer to "who reads this table?" is almost always "we're not sure."
  • Change stops. Not slows — stops. When the risk of altering a table is unbounded, teams stop altering tables. They add columns beside the old ones, write around the model, and the schema fossilises into a record of everything nobody dared touch.
  • Failure is shared. A quarterly reporting query with a bad plan takes locks, fills the buffer pool, and checkout starts timing out. One team's careless query is another team's incident, and the second team has no lever to pull.
  • Capacity has no owner. The instance is at 80% CPU. Whose growth caused it, whose budget fixes it, and who is allowed to say no? With no answer, the honest capacity plan is "hope".
  • Invariants have no owner either. Six applications write to orders. Five of them enforce the "cannot cancel after shipment" rule. The sixth is a batch script from 2019. Lesson 5.01's whole framing collapses: an invariant enforced in six places is enforced in none.

The unpicking, in the order that actually works

The instinct is to split the database. That is the last step, and doing it first is how these projects fail. The real sequence separates logical ownership long before anything physical moves.

Step 1 — find the real readers. Not the documented ones. Turn on query logging, sample it for a fortnight, and group by the credential that connected. Enumerate the database grants. Check the BI tool, the ETL job, the analysts with a read replica password, the internal admin tool nobody maintains. Expect surprises; there is always at least one consumer that predates everyone currently employed. You cannot unpick coupling you cannot see, and this step is most of the project.

Step 2 — stop the bleeding. Before removing any existing coupling, make sure none is added. New services get their own schema, with no cross-schema grants. New joins across an ownership line get rejected in review. This is unglamorous and it is the difference between a migration that converges and one that runs forever.

Step 3 — separate logically, inside the same instance. Give each service its own schema and its own database user. Move tables under their owner. Then revoke the cross-schema grants — and this is the moment of truth, because every unknown reader you missed in step 1 now fails loudly, in an environment where a single GRANT undoes it. You get the full coupling map for the price of a permissions change, with no data movement and no rollback plan.

Step 4 — replace the joins. This is the real work, and it is where the design happens. Three options, and they are not interchangeable:

  • API composition — call the owning service and stitch results in memory. Simplest and always correct, but you have turned a join into a loop of network calls: the N+1 problem from Tier 1, now with 200 ms of latency attached. Fine for a handful of ids, terrible for a report.
  • Replicate what you need — the owner publishes events; you keep a local, read-only copy of exactly the three fields you use, and join against that. Fast, and it survives the owner being down. The cost is that your copy is eventually consistent, and you now own a cache-invalidation problem forever. Correct for high-volume reads and denormalised views.
  • Move the logic to the data — the honest answer more often than people admit. If a query needs to join four of another service's tables, it is that service's query. Give it an endpoint and stop pulling its data across the wire.

Step 5 — split the instance. Only now, and it is mostly an operations task rather than an architectural one: replicate, cut over, decommission. If steps 1–4 were done properly, this step is boring. If they were skipped, this step is the incident.

The foreign key you have to give up

Worth naming on its own, because it is the loss people underestimate. Once orders and users live in separate databases, the database cannot enforce that every order points at a real user. Nothing does, unless you build it.

What replaces it:

  • Tolerate the dangling reference. Design the reader so a missing user renders as "unknown", not a 500. This is mostly a discipline about null handling, and it is cheaper than it sounds.
  • Reconcile on a schedule. Tier 3's reconciliation job, promoted to a permanent fixture: a periodic sweep that finds orphans and reports or repairs them. Eventual integrity, deliberately chosen and monitored, rather than assumed and absent.
  • Never delete across the seam. Deletes are what create orphans. Soft-delete or tombstone at the owner and publish the event; let each holder of a copy decide what to do about it.

Scaffolding for the transition

Two tools make the middle of the migration survivable. Database views let you move a table and leave a view behind at the old name, so unknown readers keep working while you find them — a compatibility shim inside the database, and the cheapest way to buy time. Change data capture reads the write-ahead log and turns committed changes into a stream, which lets you populate a new service's store from the old database without asking the legacy application to publish anything. It is the standard bridge out of a shared database precisely because it requires no cooperation from the system you are leaving.

The trap

Splitting the code into services while leaving the database shared, then calling it done. This is the single most common way the migration goes wrong, and it lands you in the worst available position: you now have the distributed monolith's network calls, partial failures and deploy coordination and the shared database's invisible coupling and unversionable schema. Every cost, neither benefit. If you can only do one of the two, split the data first — separate ownership of the tables inside one deployable is a real, defensible architecture. Separate deployables over one shared schema is not.