How to update redis after updating database?
Asked Answered
R

2

28

I cache some data in redis, and reading data from redis if it's exists, otherwise reading data from database and write the data in redis.

I find that there are several ways to update redis after updating database.For example:

  1. set keys in redis to expired
  2. update redis immediately after updating datebase.
  3. put data in MQ and use consumer to update redis.

I'm a little confused and don't know how to choose.

Could you tell me the advantage and disadvantage of each way and it's better to tell me other ways to update redis or recommend some blog about this problem.

Rumania answered 30/3, 2016 at 8:2 Comment(3)
You should format your questions so others can easily read them. Check how I fixed your question's body and do it this way next time...Gine
thanks. I will pay attention to that next time.Rumania
No problem ;) It's just if you post questions this way it'll be hard that you get answers!Gine
N
23

Actual data store and cache should be synchronized using the third approach you've already described in your question.

As you add data to your definitive store (i.e. your SQL database), you need to enqueue this data to some service bus or message queue, and let some asynchronous service do the whole synchronization using some kind of background process.

You don't want get into this cases (when not using a service bus and asynchronous service):

  • Make your requests or processes slower because the user needs to wait until the data is both stored in your database and cache.
  • Have the risk of a fail during the caching process and not being able to have a retry policy (which is usually a built-in feature in a service bus or some message queues). Also, this failure can end up in a partial or complete cache corruption and you won't be able to automatically and easily schedule some task to fix this situation.

About using Redis key expiration, it's a good idea. Since Redis can expire keys using its built-in mechanism, you shouldn't implement key expiration from the whole background process. If a key exists is because it's still valid.

BTW, you won't be always on this case (if a key isn't expired it means that it shouldn't be overwritten). It might depend on your actual domain.

Nashua answered 30/3, 2016 at 8:21 Comment(3)
hi,I have another question,if I updated redis failed or MQ breakdown,it will cause dirty read, I want to know how to deal with dirty read? ThanksRumania
try to use persistent MQ with acknowledgement. 1.in case of redis failed there is be no ack to the MQ so it will automatically retry. 2.in case of MQ breakdown it will try when MQ is up again as it is persistent.(it is a bit slower )Ramonramona
Can Golang be used for asynchronous service which does the whole synchronization in background?Tragic
H
0

You can create an api to interact with your redis server, then use SQL CLR to call the call api

Halfway answered 13/3, 2022 at 3:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.