How to update redis after updating database?

25,616

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.

Share:
25,616
mengying.ye
Author by

mengying.ye

Updated on March 30, 2020

Comments

  • mengying.ye
    mengying.ye about 4 years

    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.