【文档熟肉】Redis分区:如何在多个Redis实例之间分割数据

Partitioning: how to split data among multiple Redis instances.

分区:如何在多个Redis节点间分割数据

Partitioning is the process of splitting your data into multiple Redis instances, so that every instance will only contain a subset of your keys. The first part of this document will introduce you to the concept of partitioning, the second part will show you the alternatives for Redis partitioning.
分区是在多个节点之间分割你的数据,所以每个节点只能拥有一部分的key。文档的第一部分会向你介绍分区的概念,第二部分会向你展示Redis分区的替代方案。

Why partitioning is useful
为什么分区是有用的

Partitioning in Redis serves two main goals:
Redis服务器的分区有两个主要目标:

  • It allows for much larger databases, using the sum of the memory of many computers. Without partitioning you are limited to the amount of memory a single computer can support.
    对于极其巨大的数据库,它允许使用多个计算机内存的总和。如果没有分区你(的Redis能使用的内存)会被限制在单个计算机能够提供的内存大小上。

  • It allows scaling the computational power to multiple cores and multiple computers, and the network bandwidth to multiple computers and network adapters.
    它允许将计算能力扩展到多个核和多台计算机,并将网络带宽扩展到多台计算机和网络适配器。

Partitioning basics
分区基础

There are different partitioning criteria. Imagine we have four Redis instances R0, R1, R2, R3, and many keys representing users like user:1, user:2, … and so forth, we can find different ways to select in which instance we store a given key. In other words there are different systems to map a given key to a given Redis server.
分区条件会有差异。想象下,我们拥有4个Redis节点R0,R1,R2,R3,并且有许多代表用户的key,如user:1,user:2,…等等,我们需要使用不同的方法去找的存储一个指定的key的节点。换句话说,有不同的系统来映射一个给定的键到一个给定的Redis服务器。

One of the simplest ways to perform partitioning is with range partitioning, and is accomplished by mapping ranges of objects into specific Redis instances. For example, I could say users from ID 0 to ID 10000 will go into instance R0, while users form ID 10001 to ID 20000 will go into instance R1 and so forth.
一种最常见的分区方式就是范围分区,通过将范围对象映射到特定的Redis实例来完成。例如,ID从0到10000的用户会分到节点R0,ID从10001到20000的用户分到节点R1,等等。

This system works and is actually used in practice, however, it has the disadvantage of requiring a table that maps ranges to instances. This table needs to be managed and a table is needed for every kind of object, so therefore range partitioning in Redis is often undesirable because it is much more inefficient than other alternative partitioning approaches.
这套系统是实际中使用和工作的,不过,它有着需要记录向节点映射的表的缺点。这张表需要维护并且对于每种对象都需要这张表,因此Redis中的范围分区经常不受欢迎,因为相比其他可选的分区方法,它的效率太低了。

An alternative to range partitioning is hash partitioning. This scheme works with any key, without requiring a key in the form object_name:, and is as simple as:
范围分区的替代方法是哈希分区。这种方案适用于任何key,也不需要形如 object_name:,的key的表格,如下:

  • Take the key name and use a hash function (e.g., the crc32 hash function) to turn it into a number. For example, if the key is foobar, crc32(foobar) will output something like 93024922.
    -获取键名并使用一个哈希函数(例如,crc32哈希函数)将其转换为一个数字。例如,如果键是foobar,那么crc32(foobar)将输出类似于93024922的内容。

  • Use a modulo operation with this number in order to turn it into a number between 0 and 3, so that this number can be mapped to one of my four Redis instances. 93024922 modulo 4 equals 2, so I know my key foobar should be stored into the R2 instance. Note: the modulo operation returns the remainder from a division operation, and is implemented with the % operator in many programming languages.
    使用这个数字的模操作,以便将它转换为0到3之间的数字,这样这个数字可以映射到我的四个Redis实例之一。93024922 modulo 4 = 2,所以我知道我的key foobar应该存储在R2实例中。注意:模运算返回除法运算的余数,在许多编程语言中都是用%运算符实现的。

There are many other ways to perform partitioning, but with these two examples you should get the idea. One advanced form of hash partitioning is called consistent hashing and is implemented by a few Redis clients and proxies.
执行分区还有许多其他的方法,但是通过这两个示例,你应该可以了解其中的含义。一种高级的哈希分区被称为一致哈希,由Redis的一些客户端和代理来实现。

参考资料:
[1],Redis官方文档 Partitioning: how to split data among multiple Redis instances.

---------------未完待续--------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值