Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache invalidation and synchronisation Angular/back-end

Intro:

I've got a complex and long lasting query on the back-end, feeding back the angular app on the front-end.

Currently the angular app uses the cached data on the back-end rather than reading directly from the complex query, which would take few minutes. The cache gets warm every morning and every night.

As users make changes to the UI, and save the data, which is then passed onto the server side, and saved to database. At that time the UI is up to date until the user refreshes the page. At the same time database is up to date, but the cache is stale.

So when the user refreshes the page the stale cache values are displayed on the page.

More info:

I'm now thinking of ways to refresh the cache, and any advice from more experienced folks would be most welcome.

My idea is to refresh the cache by a cache job (one at a time), which is queued as soon as user saves something. The job will have the relevant info what changed, and the whole cache won't have to be recalculated but rather just the bit which changed.

Question part:

What technique can I use to keep the user up to date with the data even if the user refreshes the page? Should I save the 'deltas', on the client side in a form of indexedDB or localstorage, at the same when the data is sent to server. So when the page refreshes the user reads the data from the localstorage or indexed db.

I'm still thinking this through, obviously I don't have much experience in this, any comments on the directions I've taken so far?

Basically I can change anything including back-end/front-end/caching it's still in the POC phase, I'm just trying to be as informed as possible to what worked for other people.

Update

Little more background. I'm working on a index like page, so there are more than one records that can be edited inline.

Also I'm doing some transformation of the flat db records on the back-end, before dumping them into the map like structure, and passing it to the front-end in a form of json.

like image 523
Gandalf StormCrow Avatar asked Nov 06 '15 22:11

Gandalf StormCrow


People also ask

What happens when cache is invalidated?

Cache invalidation refers to process during which web cache proxies declare cached content as invalid, meaning it will not longer be served as the most current piece of content when it is requested. Several invalidation methods are possible, including purging, refreshing and banning.

What is meant by cache invalidation?

Cache invalidation describes the process of actively invalidating stale cache entries when data in the source of truth mutates. If a cache invalidation gets mishandled, it can indefinitely leave inconsistent values in the cache that are different from what's in the source of truth.

Why is cache invalidation important?

Cache invalidation can be used to push new content to a client. This method functions as an alternative to other methods of displaying new content to connected clients. Invalidation is carried out by changing the application data, which in turn marks the information received by the client as out-of-date.

How is browser cache invalidated?

To invalidate cache is to remove the cache entries. There is no actual way to truly programatically do this for a browser. What we are actually talking about here is asking the web-server for a new file due the name of the file changing, not forceably removing the original file.


2 Answers

I would think the simplest way would be to make sure you know the time the cache was created. When you make changes, save the current state of the page in localStorage, along with the time of the cache. When you load the page, you get the cached data, check it's time to see if it is more recent than your localStorage version. If it is, use the cache, if not, reload your data from localStorage since it has the cached data PLUS your changes already.

like image 74
dave Avatar answered Oct 19 '22 14:10

dave


Your question is too long, let me summarize the facts.

  1. You have a lot of information in the database
  2. Direct search query takes several minutes
  3. To provide fast search, you use cache which is updated two times a day
  4. When user changes the data, database is updated and cache is not, so web page shows outdated information from cache.

This looks like a typical cache using scenario and the solution is obvious: you should update the cache with deltas as soon as database is changed. The real implementation will depend on your application architecture and cache structure.

The typical workflow for your problem would be:

def updateRequest(Request req) {
    def tx = db.startTransaction();
    tx.execute(createUpdate(req.getData()));
    tx.commit(); // if transaction fails, cache is not updated
    cache.update(req.getData()); // can be done in background, if you return delta 
}
like image 2
AdamSkywalker Avatar answered Oct 19 '22 14:10

AdamSkywalker