What is the Database-Per-Service pattern and why is it important?
Topic: microservices, Data Management. Level: Basic.
Answer
Each microservice owns its own database and no other service may access it directly — only through the owning service's API/events. This enforces loose coupling, lets each service choose the best datastore, and allows independent schema evolution and scaling. The cost: no cross-service JOINs or ACID transactions, so you need API composition or read models for queries, and Sagas for consistency. A shared database is an anti-pattern because it re-couples services at the schema level.
```yaml
# Each service has isolated credentials/schema — no cross-service table access
order-service:
datasource: { url: jdbc:postgresql://order-db:5432/orders }
payment-service:
datasource: { url: jdbc:postgresql://payment-db:5432/payments }
# order-service NEVER runs: SELECT * FROM payments.transactions
# it calls payment-service's API or consumes its events instead
```