LoremIpsum
LoremIpsum

Reputation: 1862

CQRS: How to implement eventual consistency between MongoDB (read model) and SQL Server (write model)

I have a project using the CQRS design pattern with domain driven design. In the read side of this project, I use a materialized view to build the read model so that read side and write side can be updated to be consistent by SQL Server.

I would like to refactor my project and use MongoDB for the read side. I don`t know much about MongoDB. Is there any algorithm or strategy to keep data consistent between a NoSQL database (MongoDB) and a relational database (SQL Server)?

Upvotes: 1

Views: 1230

Answers (1)

Arkadiusz K
Arkadiusz K

Reputation: 1817

To synchronize data storages your queries (read side) and commands (write side) you can use one of the following strategies:

  • Synchronous - Every command also synchronously update query side data storage
  • Asynchronous - Every command triggers asynchronous update of query side data storage
  • Scheduled - A predefined job runs periodically and updates query side data storage
  • On-demand - Query side data storage is updated on-demand. For example when a request comes and you have a some sort of algorithm that triggers updating if staleness of data is detected.

So, it's up to you to figure out what strategy best applies to your project.

Only with synchronous approach you got strong consistency. Other options causes eventual consistency and they differ how much long the data could be stale. In asynchronous approach you have stale data in terms of milliseconds.

Upvotes: 3

Related Questions