简体   繁体   English

在本地缓存Redis集群

[英]Cache a redis cluster locally

I have a scenario where we want to use redis , but I am not sure how to go about setting it up. 我有一个要使用redis的场景,但是我不确定如何进行设置。 Here is what we want to achieve eventually: 这是我们最终要实现的目标:

  1. A redundant central redis cluster where all the writes will occur with servers in two aws regions. 一个冗余的中央redis群集,其中所有写入将在两个aws区域中的服务器上进行。

  2. Local redis caches on servers which will hold a replica of the complete central cluster. 服务器上的本地redis缓存将保存整个中央集群的副本。

The reason for this is that we have many servers which need read access only, and we want them to be independent even in case of an outage (where the server cannot reach the main cluster). 这样做的原因是,我们有许多只需要读访问权限的服务器,即使在发生故障(服务器无法到达主群集)的情况下,我们也希望它们是独立的。

I know there might be a "stale data" issue withing the caches, but we can tolerate that as long as we get eventual consistency. 我知道缓存可能存在“过时的数据”问题,但是只要我们能达到最终的一致性,我们就可以忍受。

What is the correct way to achieve something like that using redis ? 使用redis实现类似目标的正确方法是什么?

Thanks! 谢谢!

You need the Redis Replication (Master-Slave) Architecture . 您需要Redis复制(主从)体系结构

Redis Replication : Redis复制:

Redis主从架构

Redis replication is a very simple to use and configure master-slave replication that allows slave Redis servers to be exact copies of master servers. Redis复制使用和配置主从复制非常简单,它允许从Redis服务器成为主服务器的精确副本。 The following are some very important facts about Redis replication: 以下是有关Redis复制的一些非常重要的事实:

  • Redis uses asynchronous replication. Redis使用异步复制。 Starting with Redis 2.8, however, slaves periodically acknowledge the amount of data processed from the replication stream. 从Redis 2.8开始,从站会定期确认从复制流处理的数据量。
  • A master can have multiple slaves. 一个主机可以有多个从机。
  • Slaves are able to accept connections from other slaves. 从站能够接受其他从站的连接。 Aside from connecting a number of slaves to the same master, slaves can also be connected to other slaves in a cascading-like structure. 除了将多个从站连接到同一主站之外,从站还可以以级联结构连接到其他从站。
  • Redis replication is non-blocking on the master side. Redis复制在主端无阻塞。 This means that the master will continue to handle queries when one or more slaves perform the initial synchronization. 这意味着当一个或多个从属设备执行初始同步时,主控设备将继续处理查询。
  • Replication is also non-blocking on the slave side. 复制在从属端也是非阻塞的。 While the slave is performing the initial synchronization, it can handle queries using the old version of the dataset, assuming you configured Redis to do so in redis.conf. 当从属服务器执行初始同步时,假定您在redis.conf中配置了Redis,则它可以使用旧版本的数据集处理查询。 Otherwise, you can configure Redis slaves to return an error to clients if the replication stream is down. 否则,您可以配置Redis从属服务器,以在复制流关闭时将错误返回给客户端。 However, after the initial sync, the old dataset must be deleted and the new one must be loaded. 但是,在初始同步之后,必须删除旧的数据集,并且必须加载新的数据集。 The slave will block incoming connections during this brief window (that can be as long as many seconds for very large datasets). 从设备将在此短暂的窗口内阻止传入的连接(对于非常大的数据集,该连接可能长达几秒钟)。
  • Replication can be used both for scalability, in order to have multiple slaves for read-only queries (for example, slow O(N) operations can be offloaded to slaves), or simply for data redundancy. 复制既可以用于可伸缩性,也可以用于多个从服务器进行只读查询(例如,可以将慢速的O(N)操作卸载到从服务器上),也可以仅用于数据冗余。
  • It is possible to use replication to avoid the cost of having the master write the full dataset to disk: a typical technique involves configuring your master redis.conf to avoid persisting to disk at all, then connect a slave configured to save from time to time, or with AOF enabled. 可以使用复制来避免让主服务器将整个数据集写入磁盘的成本:一种典型的技术包括配置主服务器redis.conf以避免完全保留到磁盘,然后连接一个配置为不时保存的从服务器,或启用AOF。 However this setup must be handled with care, since a restarting master will start with an empty dataset: if the slave tries to synchronized with it, the slave will be emptied as well. 但是,必须谨慎处理此设置,因为重启的主服务器将以一个空的数据集开始:如果从服务器尝试与其同步,则从服务器也将被清空。

Go through the Steps : How to Configure Redis Replication . 完成以下步骤: 如何配置Redis复制

So I decided to go with redis-sentinel . 因此,我决定使用redis-sentinel

Using a redis-sentinel I can set the slave-priority on the cache servers to 0, which will prevent them from becoming masters. 使用redis-sentinel我可以将缓存服务器上的slave-priority设置为0,这将阻止它们成为主服务器。

I will have one master set up, and a few "backup masters" which will actually be slaves with slave-priority set to a value which is not 0, which will allow them to take over once the master goes down. 我将设置一个主服务器,以及几个“备份主服务器”,它们实际上将是从属服务器,其slave-priority设置为不为0的值,一旦主服务器发生故障,它们将接管他们。

The sentinel will monitor the master, and once the master goes down it will promote one of the "backup masters" and promote it to be the new master. 哨兵将监视主服务器,一旦主服务器崩溃,它将升级“备份主服务器”之一并将其升级为新的主服务器。

More info can be found here 更多信息可以在这里找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM