Microservices Databases : Separate or Shared?

In 2018, when I initially started to explore microservices, my understanding of microservices was limited to only one aspect of microservice and that is team Autonomy. Or else why would I separate everything? Isn't it difficult to manage in any startup where a developer resources are limited?
New Learning aspects of Microservices
After being a technical reviewer of a book called Hands-on Microservices with Node.js , it just opened my mind towards thinking in depth about the patterns and more aspects of it.
In this blog, I would like to share such aspects about designing a microservice. We will not explore all dimensions of it but the most important one that I think is from a data or database point of view.
While separating databases for each microservice is a common practice in microservices architecture, it's not always a strict requirement. The decision depends on various factors and trade-offs.
Design Factors
Following are the points to remember while making a decision to separate or share
Data Relationships: Analyze how data is related between services. Closely coupled services might benefit from a shared database, while loosely coupled services can have separate databases.
Transaction Requirements: Determine if transactions need to span multiple services. If so, careful coordination is required to ensure data consistency.
Performance and Scalability: Consider the expected load and performance requirements of each service and its data.
Team Autonomy: Evaluate the degree of autonomy required for each team responsible for a micro-service.
Based on this we can decide an approach
Approaches
Shared Database: In some cases, a shared database can be suitable, especially for closely related services or when data consistency is critical. However, it's essential to carefully manage dependencies and potential conflicts.
Hybrid Approach: A combination of separate and shared databases can be used, where some services have their own databases, while others share a common database for specific data.
Another key concept that needs to be highlighted is DDD for microservices
Domain-Driven Design : a bounded context is a specific, well-defined boundary within which a particular model or subdomain is consistent and applicable.
Consider an e-commerce system. It might have the following bounded contexts:
Customer Context: Manages customer data, profiles, and orders.
Product Catalog Context: Handles product information, pricing, and inventory.
Order Fulfillment Context: Manages order processing, shipping, and delivery.
Each context has its own model, data, and services, and they interact with each other through well-defined interfaces.
The decision to use separate or shared databases in a micro-services architecture is not one-size-fits-all. We have to understand pros and cons as per our requirements. Well I think, a well-designed data strategy can significantly impact the overall success of your micro-services implementation.




