How to update redis after updating database?

后端 未结 1 401
南旧
南旧 2021-02-04 05:40

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

相关标签:
1条回答
  • 2021-02-04 05:58

    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.

    0 讨论(0)
提交回复
热议问题