The database per service pattern is an ownership rule, not a requirement to buy a separate database server for every microservice. The real boundary is simple to test: another service’s database role should never be able to read or mutate the owning service’s private tables.
As documented on Microservices.io, keeping each service’s persistent data private behind an explicit API or event contract is what enables independent schema evolution and deployment. This guide breaks down the isolation spectrum from private tables to dedicated servers, walks through an inspectable PostgreSQL role setup, and shows how to handle cross-service reads and writes without falling back into shared-table coupling.
Quick takeaway: A service owns its data when no other service can query its tables directly. Separate schemas or logical databases with strict role credentials already enforce ownership—dedicated servers are an operational scaling and blast-radius decision.
What Database per Service Isolates
In a microservices architecture, each service owns its persistent records and encapsulates its storage internals. Other services request state or trigger behavior through documented HTTP APIs, gRPC contracts, or domain events rather than opening the owner’s tables—even when multiple services run on the same database server.
That distinction changes what “separate” actually means in practice:
- A separately named schema means nothing if every service logs in with a shared superuser or unrestricted database role.
- Conversely, a shared database instance does not violate ownership if each service connects with its own least-privilege credentials and cannot see another service’s objects.
Start by auditing which database role can read each table. If an Order Service SQL query joins directly against Customer tables, moving Customer onto a brand-new RDS instance won’t fix the architectural coupling—you have to replace that direct read with an API call or a dedicated read model. As explored in the 15 microservices architecture components guide, data ownership is one of the foundational boundaries separating true microservices from a distributed monolith.
Choosing Your Isolation Boundary
Microservices.io outlines three primary deployment models—private tables per service, schema per service, and database server per service—while Chris Richardson’s FTGO example demonstrates a fourth practical middle ground: giving each service credentials to its own logical database on a shared server.
| Isolation Boundary | Access Barrier to Verify | Remaining Shared Resource | Operational Trade-Off |
|---|---|---|---|
| Private tables | Per-table role privileges | Schema and server | Lowest infrastructure cost; requires careful table grant hygiene |
| Schema per service | Separate schema ownership and role grants | Database server | Clear namespace and migration ownership; shared CPU/RAM |
| Logical DB per service | Database-specific credentials and privileges | Host instance / cluster | Distinct catalogs; still shares a single failure domain |
| Server per service | Independent server credentials and network rules | None at the database layer | Full blast-radius isolation and scaling; highest ops overhead |
Schema-per-service makes ownership boundaries easy to inspect without multiplying database servers, whereas a dedicated server makes sense when one service drives disproportionate IOPS, needs polyglot storage, or carries strict compliance isolation. Remember that a shared server still pools CPU, memory, and connection limits—separate logical databases do not eliminate a shared failure domain (Encore). Pick the least complex deployment that enforces the access boundary and satisfies your reliability and compliance requirements (AWS Prescriptive Guidance).
Shared Servers vs. Shared Tables
Suppose Order Service and Customer Service run on the same PostgreSQL cluster. If you assign each service its own login role, restrict schema USAGE, and deny cross-schema reads, the server hardware is shared—but data ownership remains intact.
Shared tables, on the other hand, are a trap. Once two services read and write the exact same rows directly, any column rename or migration forces both teams to coordinate deployments. That is the real root of the debate in Software Engineering Stack Exchange: objections to “separate databases” are usually objections to operating dozens of standalone database servers, not to enforcing clean ownership boundaries.
Enforcing Role Isolation in PostgreSQL
The setup below illustrates how to separate schema ownership (used by migrations) from runtime application logins in PostgreSQL. Separating those two identities ensures application code can read and write rows without holding DDL privileges to alter schemas or grant access to others (PostgreSQL Privileges Documentation).
-- 1. Create non-login schema owners for migrations
CREATE ROLE customers_owner NOLOGIN;
CREATE ROLE orders_owner NOLOGIN;
-- 2. Create least-privilege runtime login roles
CREATE ROLE customers_app LOGIN PASSWORD 'replace-with-secret';
CREATE ROLE orders_app LOGIN PASSWORD 'replace-with-secret';
-- 3. Create isolated schemas and lock down PUBLIC access
CREATE SCHEMA customers AUTHORIZATION customers_owner;
CREATE SCHEMA orders AUTHORIZATION orders_owner;
REVOKE ALL ON SCHEMA customers FROM PUBLIC;
REVOKE ALL ON SCHEMA orders FROM PUBLIC;
-- 4. Grant schema usage only to the owning runtime role
GRANT USAGE ON SCHEMA customers TO customers_app;
GRANT USAGE ON SCHEMA orders TO orders_app;
-- 5. Grant table and sequence privileges for existing objects
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA customers TO customers_app;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA customers TO customers_app;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA orders TO orders_app;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA orders TO orders_app;
-- 6. Automatically apply grants to future tables & sequences created by each owner
ALTER DEFAULT PRIVILEGES FOR ROLE customers_owner IN SCHEMA customers
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO customers_app;
ALTER DEFAULT PRIVILEGES FOR ROLE customers_owner IN SCHEMA customers
GRANT USAGE, SELECT ON SEQUENCES TO customers_app;
ALTER DEFAULT PRIVILEGES FOR ROLE orders_owner IN SCHEMA orders
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO orders_app;
ALTER DEFAULT PRIVILEGES FOR ROLE orders_owner IN SCHEMA orders
GRANT USAGE, SELECT ON SEQUENCES TO orders_app;
In PostgreSQL, table privileges and sequence privileges are separate categories (PostgreSQL Privileges Documentation). If a table uses a SERIAL or IDENTITY column and you omit GRANT USAGE on sequences, INSERT statements from the application role fail even when table INSERT is granted. Configuring ALTER DEFAULT PRIVILEGES scoped to each migration owner guarantees newly migrated tables and sequences remain accessible to the owning service without manual grants on every deploy.
Negative Permission Verification
Pair role setup scripts with both a negative cross-schema query and an explicit catalog audit. First, connect in a fresh session as orders_app (or switch temporarily with SET ROLE orders_app; from an admin prompt before resetting) and confirm direct cross-schema reads fail:
SET ROLE orders_app;
SELECT * FROM customers.customer LIMIT 1;
-- Expected: ERROR: permission denied for schema customers
RESET ROLE;
Because revoking PUBLIC access on a schema proves isolation only for unprivileged roles—not pre-existing superusers, inherited role memberships, or stale table grants—verify effective memberships and explicit grants directly from PostgreSQL’s system catalogs:
-- Check for unexpected role inheritance (e.g., orders_app inheriting customers_app)
SELECT r.rolname AS member_role, m.rolname AS granted_role
FROM pg_auth_members am
JOIN pg_roles r ON r.oid = am.member
JOIN pg_roles m ON m.oid = am.roleid
WHERE r.rolname IN ('orders_app', 'customers_app');
-- Check explicit table grants across service schemas
SELECT grantee, table_schema, table_name, privilege_type
FROM information_schema.role_table_grants
WHERE table_schema IN ('customers', 'orders')
ORDER BY table_schema, grantee;
Choosing Storage Engines by Workload
Data ownership does not force every microservice onto a different database engine—and conversely, choosing a different engine (polyglot persistence) is a separate decision from dedicating an isolated physical server. Five services that all need relational ACID transactions can share one PostgreSQL cluster through isolated schemas or logical databases.
Introduce a specialized engine only when a service’s access pattern demands it—for example, a graph database for social recommendations, a search index like OpenSearch for full-text catalog filtering, or a document store for nested product attributes. Two services can use PostgreSQL and a document store without requiring physical server isolation for every relational service. AWS Prescriptive Guidance also highlights differing security, residency, or compliance requirements as valid reasons to separate persistence boundaries. Because every additional database engine brings its own backup, monitoring, and upgrade burden, make sure the workload justifies the operational cost.
Handling Cross-Service Reads and Writes
Once direct SQL joins across services are off the table, cross-domain screens—such as displaying a customer’s name and shipping tier on an order history page—are handled in one of two ways:
- API Composition: An API Gateway or backend aggregator queries Customer Service and Order Service over HTTP/gRPC and joins the results in memory. This avoids data duplication and returns up-to-date data, at the cost of extra network hops and runtime coupling to downstream availability.
- Event-Fed Local Read Model (CQRS): Customer Service publishes a
CustomerUpdatedevent whenever profile fields change. Order Service consumes that event and stores a slim local projection (customer_read_model) containing only the customer name and ID it needs to render orders in a single fast local query.
Choose between them based on latency and freshness tolerances. If a page can tolerate a few hundred milliseconds of eventual consistency, a local read model eliminates runtime cross-service dependencies. For writes that span multiple services—like reserving inventory while capturing payment—avoid brittle two-phase commits across databases and coordinate the workflow using distributed transactions in microservices (compensating actions via the Saga pattern or TCC), as illustrated in the microservices architecture diagram guide.
When Dedicated DB Servers Make Sense
Do not spin up dedicated database servers merely to satisfy the pattern’s name. In an empirical study published in PVLDB surveying over 120 microservices practitioners and researchers, 43% of responses favored giving each service its own managed database server—a plurality rather than a majority, reflecting that teams also weigh shared instances, isolated schemas, and hybrid setups against operational overhead.
Start by checking whether private schemas or logical databases with strict role barriers meet your needs. Escalate a specific service to its own dedicated database server when you observe:
- Noisy-neighbor contention: Heavy analytical or catalog traffic saturating CPU, memory, or connection pools shared with checkout transactions.
- Independent scaling or failover SLAs: One service requiring multi-region read replicas or higher IOPS tiers than the rest of the platform.
- Strict compliance boundaries: Payment or PII data that must reside on isolated hosts with dedicated encryption keys and audit logs.
Running an Ownership Audit
Before splitting an existing monolith’s database—or reviewing a growing microservices platform—map every table to its single owner and explicit external contract, matching the gradual migration strategy outlined in Microservices vs Monolithic Architecture.
| Owning Service | Schema / Table | Runtime Role | Allowed Consumer Contract | Migration Owner | Prohibited Direct SQL |
|---|---|---|---|---|---|
| Customer Service | customers.customer | customers_app | GET /customers/:id, CustomerUpdated event | customers_owner | orders_app reading customers.customer |
| Order Service | orders.orders | orders_app | GET /orders/:id, OrderCreated event | orders_owner | customers_app reading orders.orders |
Every cross-owner SELECT or JOIN uncovered during this audit goes onto a deprecation backlog: replace it with a versioned API endpoint (documented in OpenAPI / AsyncAPI) or a local projection before tightening database grants in production.
Frequently Asked Questions
Can two microservices share a database server without sharing tables?
Yes. Giving each microservice its own schema or logical database on a shared server—paired with separate login roles and strict GRANT/REVOKE rules—enforces data ownership at low operational cost. Just keep in mind that a shared server still shares CPU, memory, connection limits, and failover risk.
How do you perform joins when each microservice has its own database?
You replace cross-database SQL joins with either API Composition (fetching records from each owning service’s API and merging them in memory) or an event-fed local read model (subscribing to domain events so the querying service maintains a local projection of the fields it needs).
Is schema-per-service enough, or do I need separate physical databases?
For many teams, schema-per-service or separate logical databases on one managed cluster is enough to enforce clean ownership and independent migrations. Escalate a service to its own dedicated database server when workload contention, independent failover SLAs, or strict compliance isolation justify the extra cost—or adopt a specialized engine (polyglot persistence) when the data model requires it.
What happens if one microservice modifies another service’s database directly?
Direct cross-service database access creates hidden coupling: schema migrations in the owning service break downstream consumers unexpectedly, domain validation rules are bypassed, and teams end up stuck with coordinated deployments—turning the system into a distributed monolith.
How do you handle transactions that span multiple microservices?
While distributed transaction protocols such as two-phase commit (2PC) can coordinate across databases, microservice architectures generally avoid them because cross-service locking hurts latency, availability, and operational simplicity. Instead, multi-service writes typically rely on eventual consistency and the Saga pattern—breaking a workflow into local ACID transactions coordinated by domain events and explicit compensating actions (such as issuing a refund if inventory reservation fails).
Related Articles
- What Are Microservices? A Simple Explanation for Beginners
- Distributed Transactions in Microservices: Sagas & Outbox
- 15 Microservices Architecture Components Explained
- Microservices Architecture Diagram Explained + Examples
- Microservices vs Monolithic: Pros, Cons & Differences
- API Gateway in Microservices: Request Flow & Examples
- Microservices Documentation Tools: A Practical Guide