27

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.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
mengying.ye
  • 837
  • 2
  • 10
  • 21

2 Answers2

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.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • 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? Thanks – mengying.ye Apr 18 '16 at 14:01
  • 1
    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 ) – Raghavendra Oct 27 '16 at 06:53
  • 1
    Can Golang be used for asynchronous service which does the whole synchronization in background? – Gaurav Dave Nov 23 '17 at 09:55
0

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

minhhungit
  • 180
  • 4
  • 25