Search This Blog

Saturday, June 28, 2025

Q16-Q20

Q16: Tell me something about CQRS? 
Q17: Do we have two different databases in CQRS?
Q18: Techniques to keep two database in Sync?
Q19: Define Database per Microservice Architecture Design pattern? [Microservice Architecture Design patterns -- Database per Microservice, Event Sourcing, CQRS, Saga, BFF, API Gateway, Strangler, Circuit Breaker, Externalized Configuration, Consumer-Driven Contract Tracing]
-----------------------------------------------------------------------------------------------------------------------------
Q16: Tell me something about CQRS? 

Answer:
CQRS (Command Query Responsibility Segregation) is a powerful architectural pattern often used in microservices to separate the responsibilities of reading and writing data

Benefits:
>> This separation allows each side to be optimized independently for performance, scalability, and complexity.

CQRS splits the application into two distinct parts:

Command Side (Write Model): Handles operations that change data (Create, Update, Delete).
Query Side (Read Model): Handles operations that retrieve data (Read).

-----------------------------------------------------------------------------------------------------------------------------
Q17: Do we have two different databases in CQRS?

Answer:
In CQRS, it's common but not mandatory to use two separate databases for the read and write sides. 

Benefits of using Two databases:
1. Independent Scaling : We can scale READ DB horizontally to handle high query loads.
2. Technology Flexibility: You might use a relational database (e.g., PostgreSQL) for writes. And a NoSQL database (e.g., MongoDB, Elasticsearch) for reads.

-----------------------------------------------------------------------------------------------------------------------------
Q18: Techniques to keep two database in Sync?

Answer:
Techniques
1. Event-Driven Synchronization (Most Common)
  • How it works: After a write operation, the system emits a domain event (e.g., OrderCreated, CustomerUpdated).
  • These events are published to a message broker (like Kafka, RabbitMQ, or Azure Service Bus).
  • A consumer service listens to these events and updates the read database (e.g., MongoDB).
  • This enables eventual consistency (With little latency). 
2. Dual Writes (Not Recommended)#
The application writes to both databases in the same transaction or sequentially.
⚠️ Cons:
Risk of inconsistency if one write fails.
Hard to manage rollback and error handling.
-----------------------------------------------------------------------------------------------------------------------------
Q19: Define Database per Microservice Architecture Design pattern? 

Answer:
Each Microservice has its own dedicated Database and un related microservice can NOT access db of other domain directly. 

Advantages:
1. Understanding of Project and related DB is easy as it has limited tables and data. 
2. Scaling of DB is easy as required. 
-----------------------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment

Q31-Q35

Q31: What is pods and how it is related to Docker images and Docker containers? Q32: What is the use of Compose Docker file? Q33. What are t...