Redis partition

Partitioning is the process of splitting data into multiple Redis instances, so each instance holds only a subset of keys.

Advantages of Partitioning

  • By taking advantage of the sum of the memory of multiple computers, it allows us to construct larger databases.
  • Multiple cores and multiple computers allow us to expand computing power; multiple computers and network adapters allow us to expand network bandwidth.

Inadequate partition

Some features of redis don't work very well with partitioning:

  • Operations involving multiple keys are generally not supported. For example, when two sets are mapped to different redis instances, you cannot perform the intersection operation on the two sets.
  • Redis transactions involving multiple keys cannot be used.
  • When using partitions, data processing is more complex, for example you need to process multiple rdb/aof files and backup persistent files from multiple instances and hosts.
  • Adding or removing capacity is also more complicated. Most redis clusters support the ability to add and delete nodes at runtime with transparent data balancing, but other systems like client partitions, proxies, etc. do not support this feature. However, a technique called presharding can help.

Partition type

Redis has two types of partitions. Suppose there are 4 Redis instances R0, R1, R2, R3, and multiple keys representing users such as user:1, user:2, there are different ways to select which instance the key is stored in for a given key . That is, there are different systems for mapping a key to a Redis service.

range partition

The easiest way to partition is by range, which is to map a range of objects to a specific Redis instance.

For example, users with IDs from 0 to 10000 are saved to instance R0, users with IDs from 10001 to 20000 are saved to R1, and so on.

This method is feasible, and used in practice, the disadvantage is that there must be a mapping table from the range to the instance. This table needs to be managed, and also needs a mapping table of various objects, which is usually not a good method for Redis.

hash partition

Another partitioning method is hash partitioning. This works for any key, and doesn't need to be of the form object_name: as simple as described below:

  • Convert the key to a number with a hash function, such as the crc32 hash function. Executing crc32(foobar) on key foobar will output an integer like 93024922.
  • 对这个整数取模,将其转化为0-3之间的数字,就可以将这个整数映射到4个Redis实例中的一个了。93024922 % 4 = 2,就是说key foobar应该被存到R2实例中。注意:取模操作是取除的余数,通常在多种编程语言中用%操作符实现。