update operation cache and persist out of sync

1.2k Views Asked by At

I have person repository with the basic operation: Add, Get, Update.

I am using Redis cache and MongoDB Persist.

When there is update person request: 1. I update cache (if key not exists, It will be added) 2. I update Mongo.

But what happened is if the key exists in redis but update redis will fail? (The data will be out of syc)

I am happy to know what is the best practice for this case?

Thanks

1

There are 1 best solutions below

3
On

When you update data, the best practice is that just delete the key from redis and then update the key in mongodb. It is more efficient and more safe and more simple than sync data updating between redis and mongo. Sync updating should make them being in a transaction and may need the distributing lock which has much cost and make the code difficult to write. If you query the same key next time, it will get the data from mongo and store it to redis. So you could think about that is a lazy update on cache.

And because in many cache scenarios, the data query times should be much more than the data updating times, so lazy updating would have a good performance. And the aim that you use cache is just to speed up the query and should not pay more attention to update. Also because you have already done the update into persistent db, so you do not need to update the volatile cache at the same time. Just delete it.

UPDATE FOR YOUR COMMENT:

if we first delete the key in redis and then update data in mongdb, then the problem you said would be avoided.

(1)->delete from redis->(2)->update in mongo->(3)

whenever your app failed in point (1),(2) or (3), the data consistence will not be break. if app failed in point (1), then the data will not update in mongo too. If app failed in point (2), the data will not update in mongo, because we just delete the key from redis, so there is no data conflict when restart. (3) is too.

You may also wonder if the operation delete from redis failed, how to deal with it? If so, then you redis server is down, you would have a bigger problem than data syncing : go to start your redis service.

This will be another problem, when you take redis service back, should you restore the cache data to redis? My answer is no. If your redis turn on the persist, it will restore it for you, you should flush the cache db if you very concern the data consistence problem. It will surface a process which query key would be all missed, we could call it "cold start", but after cold start, everything will be same as old.