Data Management
Table of Content:
Data Management
Unlike Monolithic architecture
where data resides typically in RDBMS
and could be easily accessed using SQL queries, data access is a little complex in Microservices architecture
.
This is because data owned by each microservice is private to that microservice and can only be accessed through its API
.
Data Encapsulation
showcases that the microservices are loosely coupled and can evolve independent of one another.
![Data Management Data Management](/library/images-tutorials/tutorial-951808847-804_Polyglot_Persistent.jpeg)
Microservices often use different kinds of databases (a mixture of both SQL and NoSQL databases), so-called Polyglot persistence
approach.
Benefits
- Loosely coupled services, leading to better performance and scalability.
Challenges
- Maintain consistency of Business services across multiple services.
- Implement queries that recover data from various services of different DB formats. (RDBMS, NoSQL)
Event Driven Architecture
![Event Driven Architecture Event Driven Architecture](/library/images-tutorials/tutorial-271510746-805_Data-Management.jpeg)
The Microservices trigger and update the business entities based on any event and publishes an event when some action occurs.
For example - An Order microservice, publishes an event when a particular customer places an order, and it also updates its private order table(DB table). Other microservice, say inventory if subscribed to those events, will receive an event and updates its private inventory table/DB, which might lead to an event publish by inventory service.
Event Driven Architecture
Triggering actions based on Events are wonderful concepts that Microservices adapt. Learn how this happens in this video.
Benefits and Challenges
![Benefits and Challenges Benefits and Challenges](/library/images-tutorials/tutorial-859489653-807_event-driven_architecture.jpeg)
Benefits
- It allows the implementation of transactions, which span multiple services and offer ultimate consistency.
Challenges
- The model is more
complex
. Often you must implement rollback compensating transactions to recover from application-level failures. - The service subscribers must be able to detect and ignore duplicate events.
CQRS
![CQRS CQRS](/library/images-tutorials/tutorial-217963087-808_cqrs2.jpeg)
Queries in a microservice architecture are implemented through Command Query Responsibility Segregation
(CQRS)
. In CQRS, the application is split into two components:
- command-side takes care of creating, updating, and deleting requests and emits events when data changes.
- query-side executes queries against one or more materialized views, which are kept updated by subscribing to the stream of events released when data changes.
Takeaways
- CQRS supports event driven architecture.
- Few complex domains may be simpler to tackle by utilizing CQRS.
- While handling high-performance applications, CQRS allows separation of a load from reads and writes, allowing to scale independently.