简体   繁体   中英

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)?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM