本文介绍了更新数据库后如何更新Redis?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些数据缓存在redis中,如果存在则从redis中读取数据,否则从数据库中读取数据并将数据写入redis。

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.

我发现更新数据库后有几种更新Redis的方法。例如:

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


  1. 将redis中的密钥设置为过期

  2. 更新日期数据库后立即更新redis。

  3. 将数据放入MQ中,并使用使用者来更新redis。

我有点困惑,不知道如何选择。

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

您能告诉我每种方法的优点和缺点吗?最好告诉我其他方法来更新redis或推荐一些有关此问题的博客。

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.

推荐答案

应该使用问题中已经描述的第三种方法来同步实际的数据存储和缓存。

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

在将数据添加到自己的确定性存储(即您的SQL数据库),您需要将该数据排队到某些服务总线或消息队列中,并让某些异步服务使用某种后台进程来完成整个同步。

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):


  • 提出请求或处理速度变慢,因为用户需要等到数据同时存储在数据库和缓存中。

  • 在缓存过程中有失败的风险,并且没有重试策略(通常是服务总线或某些消息队列中的内置功能)。另外,此故障可能会导致部分或全部缓存损坏,并且您将无法自动轻松地安排一些任务来解决这种情况。

关于使用Redis密钥过期,这是一个好主意。由于Redis可以使用其内置机制来使密钥失效,因此您不应在整个后台过程中实现密钥失效。如果存在密钥是因为它仍然有效。

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,您将不会总是遇到这种情况(如果密钥没有过期,则意味着它不应不会被覆盖)。这可能取决于您的实际域。

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.

这篇关于更新数据库后如何更新Redis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:06
查看更多