Node.js › Microservices Architecture with Node.js
How does distributed data ownership work in microservices -- specifically, why shouldn't multiple services share direct access to the same database?
AdvancedAnswer
In a microservices architecture, each service should own its data exclusively, exposing it to other services only through its own API rather than allowing other services to query its database tables directly -- shared direct database access creates tight coupling (any schema change in the owning service risks breaking other services depending on those exact tables), defeats independent deployability (you can't safely change a shared table's structure without coordinating every service touching it), and undermines the whole premise of service boundaries and autonomy that microservices are meant to provide.
// Anti-pattern: order-service reaching directly into inventory-service's database
const inventory = await inventoryDb.query('SELECT * FROM stock WHERE product_id = ?', [productId]);
// Correct: order-service goes through inventory-service's own API instead
const response = await fetch('http://inventory-service/api/stock/' + productId);
const inventory = await response.json();Real-world example A team that initially let their order service query the inventory service's database tables directly for convenience found themselves unable to safely refactor the inventory service's schema without coordinating a risky, simultaneous deployment across both services; migrating to a proper API boundary between the two services let each evolve its own data model independently afterward.