Video in progress
What does the "database per service" rule mean?
Each service is the sole owner of its data: only it reads and writes its tables. Other services get this data only through its API or its events.
📖 Answer
That doesn't mean it has to be a separate database server. A separate schema and a separate user inside one PostgreSQL is enough too. What matters is that order-service can't connect to the payments schema.
-- each service gets its own user and its own schema
CREATE USER order_svc WITH PASSWORD '...';
CREATE SCHEMA orders AUTHORIZATION order_svc;
REVOKE ALL ON SCHEMA payments FROM order_svc;
Why you need it: a service can change its own table however it likes: rename a column, add an index, split a table. Nobody breaks, because the outside world sees only the API.
There's a cost too: you can no longer get data from two services with a single JOIN, and there's no ordinary transaction across two databases. You have to solve these problems with API composition, events and sagas.
Sign up to mark studied questions and save your progress.