Separate Services, Shared Database: How You Build a Distributed Monolith
When services read and write the same tables, the schema becomes a contract nobody owns, and releases have to be coordinated again. Give each service its own data.
The team split its shop into Orders, Billing and Shipping services, each with its own repository, pipeline and container. A year later, Orders splits CustomerName into first and last name. The migration runs, Orders deploys, and that night Shipping's label job starts failing, because it reads that column directly. The fix is a release plan that deploys three services in a set order, the very thing the split was supposed to end.
The common misconception
The belief: splitting the code into separate services gives you independent deployments, even if they all keep using one database. The data can be separated later; the code is what matters.
It's an understandable shortcut. Code splits cleanly; data doesn't. One database keeps joins, foreign keys, one backup and easy reporting. Microsoft's .NET microservices guide admits the pull: a centralized database "initially looks simpler and seems to enable reuse of entities in different subsystems to make everything consistent." Martin Fowler names the real benefit too: with an , "Any changes to data made in a single application are made available to all applications at the time of database commit."
The half-truth: deploying a service is now independent. Changing it isn't.
What actually happens
The schema becomes the contract
When several services use the same tables, every column name, type and status value is part of their interface. Fowler: an integration database "needs a schema that takes all its client applications into account. The resulting schema is either more general, more complex or both - because it has to unify what should be separate BoundedContexts." His verdict: "the database becomes a point of coupling between the applications that access it. This is usually a deep coupling that significantly increases the risk involved in changing those applications and making it harder to evolve them." That's you can't see in the code, because it sits in SQL and in the meaning of values like 'paid'.
Releases get coordinated again
Microsoft's guide spells out the consequence: "If multiple services were accessing the same data, schema updates would require coordinated updates to all the services. This would break the microservice lifecycle autonomy." Fowler goes further: since independent deployment is part of the definition, "a suite of services that must have its deployments coordinated is not a microservice architecture", and many teams get into trouble because "they end up having to coordinate service deployments."
The result is a : you have network calls, more pipelines and more moving parts, and you still release together.
The server isn't the problem; the tables are
The Azure Architecture Center draws the line precisely: "Services can safely share the same physical database server. Problems occur when services share the same schema, or they read and write to the same set of database tables." Its rule is short: "Two services shouldn't share a data store. Each service manages its own private data store, and other services can't access it directly." A shared store also "limits each team's ability to optimize data storage for its specific service."
What you give up, and what replaces it
Splitting data has a real cost, and Microsoft names it: a single database offers "ACID transactions and the SQL language, both working across all the tables", while separate data means "you can't make a single ACID transaction across microservices." Instead, a service that needs another service's data keeps a copy of "only the attributes you need", kept up to date by events or fetched through the owner's API. That copy lives in its own and can use its own names.
The fix: own your data, publish a contract
C# · Shipping/LabelJob.cs
// Shipping queries Orders' tables through the shared databasepublic sealed class LabelJob(ShopDb db){ public Task<List<Label>> RunAsync(CancellationToken ct) => db.Orders .Where(o => o.Status == "paid" && o.ShippedAt == null) .Select(o => new Label(o.Id, o.CustomerName, o.Street, o.City)) .ToListAsync(ct);}// Orders renames a status or splits CustomerName: this breaks at// runtime, and only a coordinated release of both services fixes it
C# · Shipping/OnOrderPaid.cs
// Orders publishes this contract and promises to keep it stablepublic sealed record OrderPaid(Guid OrderId, string Recipient, Address ShipTo);// Shipping keeps what it needs, in tables only Shipping can touchpublic sealed class OnOrderPaid(ShippingDb db){ public async Task HandleAsync(OrderPaid e, CancellationToken ct) { db.Shipments.Add(new Shipment(e.OrderId, e.Recipient, e.ShipTo)); await db.SaveChangesAsync(ct); }}public sealed class LabelJob(ShippingDb db){ public Task<List<Shipment>> PendingAsync(CancellationToken ct) => db.Shipments.Where(s => s.ShippedAt == null).ToListAsync(ct);}
Shipping now depends on OrderPaid, a small contract Orders chose to publish, instead of on Orders' tables. Orders can split names, rename statuses or move to another database without asking anyone, as long as it keeps publishing the same . Shipping stores the recipient and address under its own names, so its label job no longer reads anyone else's schema. Delivering that event reliably, and living with the delay before it arrives, are separate problems covered later in this path.
Myth
Reality
Separate repos and pipelines make services independent
Shared tables make the schema a contract every service depends on
Sharing a database server is the problem
Sharing a server is fine; sharing schemas and tables is the coupling
Read-only access to another service's tables is harmless
Readers depend on names and meanings the owner can no longer change freely
We'll split the data later
Each new query against shared tables makes that split bigger
A shared database keeps data consistent for free
It does, and that's exactly what you must replace when you split
How to apply it in your project
Map who touches which tableFor each service, list the tables it reads and writes, from its code and the database's logs. Every table used by two services is a shared contract.
Give every table one ownerAssign each table to the service whose business rules write it. Only the owner may write; plan to remove every other access.
Replace cross-service access with contractsWhere another service needs the data, expose it through the owner's API or publish events, and let the consumer keep only the attributes it needs.
Enforce it with permissionsMove each service to its own schema and login, so the boundary is checked by the database, not by memory.
Key takeaways
Separate code with shared tables gives you a distributed monolith: distributed costs, coordinated releases.
Shared tables turn the schema into a contract nobody owns, so every column change becomes a negotiation.
Sharing a database server is fine; sharing schemas and tables is the coupling.
Each service should own its data and share it through APIs or published events, keeping only the attributes it needs.
Splitting data costs you cross-service transactions and joins, so plan for that instead of avoiding the split.
التعليقات (0)