Search code examples
cachingdesign-patternscqrs

Cache and update regularly complex data


Lets star with background. I have an api endpoint that I have to query every 15 minutes and that returns complex data. Unfortunately this endpoint does not provide information of what exactly changed. So it requires me to compare the data that I have in db and compare everything and than execute update, add or delete. This is pretty boring...

I came to and idea that I can simply remove all data from certain tables and build everything from scratch... But it I have to also return this cached data to my clients. So there might be a situation that the db will be empty during some request from my client because it will be "refreshing/rebulding". And that cant happen because I have to return something

So I cam to and idea to

  1. Lock the certain db tables so that the client will have to wait for the "refreshing the db"

or

  1. CQRS https://martinfowler.com/bliki/CQRS.html

Do you have any suggestions how to solve the problem?


Solution

  • It sounds like you're using a relational database, so I'll try to outline a solution using database terms. The idea, however, is more general than that. In general, it's similar to Blue-Green deployment.

    Have two data tables (or two databases, for that matter); one is active, and one is inactive.

    When the software starts the update process, it can wipe the inactive table and write new data into it. During this process, the system keeps serving data from the active table.

    Once the data update is entirely done, the system can begin to serve data from the previously inactive table. In other words, the inactive table becomes the active table, and vice versa.