← Back to SQL, page 23

sql-0456 · Medium mid

In a microservices system, should each service have its own database? What problems follow?

Giving each service its own database (or schema with exclusive access) keeps services independently deployable and stops hidden coupling through shared tables. The cost is that joins and transactions across services disappear: data is combined through APIs, events or read replicas, and multi-service changes need sagas with compensating actions instead of one ACID transaction.

Full answer

Benefits:

  • A service can change its schema without coordinating with others.
  • Each can choose a fitting store and scale independently.
  • Ownership is clear.

Problems and common answers:

  • Cross-service queries ('orders with customer names'): keep a local copy of the needed customer fields updated from events, or compose in an API layer.
  • Multi-step business processes (reserve stock, charge card, create shipment): a saga runs each step as a local transaction and, on failure, runs compensating steps (release stock, refund).
  • Reporting: stream all services' changes into a warehouse.
  • Consistency: data is eventually consistent; screens must tolerate brief disagreement.

A shared database used by several services is simpler at first but becomes a coupling point where one team's migration can break others. A pragmatic middle path is one database server with a schema per service and no cross-schema access.

Key points

  • Own database per service for independence
  • No cross-service joins or transactions
  • Local copies via events for queries
  • Sagas with compensation for workflows
  • Warehouses for cross-service reporting

Common mistakes

  • Services reading each other's tables
  • Expecting ACID across services

Follow-ups

  • How does a saga handle a failed payment?
  • When is a shared database acceptable?

Tips

  • Name the lost capabilities