简体   繁体   English

Redis 复制和redis sharding(集群)的区别

[英]Redis replication and redis sharding (cluster) difference

  1. Anyone know the difference between redis replication and redis sharding?有人知道redis复制和redis分片之间的区别吗?
  2. What are they use for?它们有什么用? Redis stores data in memory, how does this affect replication/sharding? Redis 将数据存储在内存中,这如何影响复制/分片?
  3. Is it possible to use both of them together?可以同时使用它们吗?

Sharding is almost replication's antithesis, though they are orthogonal concepts and work well together.分片几乎是复制的对立面,尽管它们是正交的概念并且可以很好地协同工作。

Sharding, also known as partitioning, is splitting the data up by key;分片,也称为分区,是将数据按键拆分; While replication, also known as mirroring, is to copy all data.而复制也称为镜像,就是复制所有数据。

Sharding is useful to increase performance, reducing the hit and memory load on any one resource.分片有助于提高性能,减少任何一种资源的命中和内存负载。 Replication is useful for getting a high availability of reads.复制对于获得读取的高可用性很有用。 If you read from multiple replicas, you will also reduce the hit rate on all resources, but the memory requirement for all resources remains the same.如果从多个副本读取,也会降低所有资源的命中率,但所有资源的内存需求保持不变。 It should be noted that, while you can write to a slave, replication is master->slave only.应该注意的是,虽然您可以写入从属设备,但复制只能是主设备-> 从设备。 So you cannot scale writes this way.所以你不能以这种方式扩展写入。

Suppose you have the following tuples: [1:Apple], [2:Banana], [3:Cherry], [4:Durian] and we have two machines A and B. With Sharding, we might store keys 2,4 on machine A;假设您有以下元组:[1:Apple], [2:Banana], [3:Cherry], [4:Durian] 并且我们有两台机器 A 和 B。使用分片,我们可以将密钥 2,4 存储在机器A; and keys 1,3 on machine B. With Replication, we store keys 1,2,3,4 on machine A and 1,2,3,4 on machine B.和密钥 1,3 在机器 B 上。通过复制,我们将密钥 1,2,3,4 存储在机器 A 上,将密钥 1,2,3,4 存储在机器 B 上。

Sharding is typically implemented by performing a consistent hash upon the key.分片通常通过对密钥执行一致的散列来实现。 The above example was implemented with the following hash function h(x){return x%2==0?A:B}.上面的例子是用下面的哈希函数 h(x){return x%2==0?A:B} 实现的。

To combine the concepts, We might replicate each shard.为了结合这些概念,我们可能会复制每个分片。 In the above cases, all of the data (2,4) of machine A could be replicated on machine C and all of the data (1,3) of machine B could be replicated on machine D.在上述情况下,机器 A 的所有数据 (2,4) 都可以复制到机器 C 上,机器 B 的所有数据 (1,3) 都可以复制到机器 D 上。

Any key-value store (of which Redis is only one example) supports sharding, though certain cross-key functions will no longer work.任何键值存储(Redis 只是其中的一个示例)都支持分片,但某些交叉键功能将不再起作用。 Redis supports replication out of the box. Redis 支持开箱即用的复制。

In simple words, the fundamental difference between the two concepts is that Sharding is used to scale Writes while Replication is used to scale Reads.简单来说,这两个概念的根本区别在于,Sharding 用于扩展 Writes,而 Replication 用于扩展 Reads。 As Alex already mentioned, Replication is also one of the solutions to achieve HA.正如 Alex 已经提到的,Replication 也是实现 HA 的解决方案之一。

Yes, they are both typically used together if you consider how shards can be replicated across nodes in a cluster.是的,如果您考虑如何跨集群中的节点复制分片,它们通常会一起使用。

With regard to your third question, instead of the RAM-flush option, it is a better idea to use the Redis Append Only File (AOF).关于您的第三个问题,最好使用 Redis Append Only File (AOF),而不是 RAM-flush 选项。 At only a minor cost (in terms of write speed), you get a lot more reliability of your writes.只需很小的成本(就写入速度而言),您就可以获得更高的写入可靠性。 It is quite like the mysql binary log.它很像 mysql 二进制日志。 The 1 fsync/second is the recommended option to use. 1 fsync/second 是推荐使用的选项。

Using Replication and Sharding Together同时使用复制和分片

If you want both high availability and improved performance, both replication and sharding can be used together to provide this.如果您既希望获得高可用性又希望提高性能,则可以同时使用复制和分片来实现这一点。 With sharding, you will have two or more instances with particular data based on keys.通过分片,您将拥有两个或多个基于键的特定数据实例。 You can then replicate each of these instances to produce a database that is both replicated and sharded, which will provide for reliability and speed at the same time!然后,您可以复制这些实例中的每一个,以生成一个既可复制又可分片的数据库,这将同时提供可靠性和速度!

在此处输入图片说明

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

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