Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Redis partition


May 16, 2021 Redis


Table of contents


Redis partition

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

The advantages of partitioning

  • By utilizing the values of multiple computers' memory, we are allowed to construct larger databases.
  • Multi-core and multiple computers allow us to extend computing power, and multiple computers and network adapters allow us to extend network bandwidth.

Insufficient partitions

Some of the features of redis do not perform very well in partitioning:

  • Operations involving multiple keys are usually not supported. F or example, when two sets map to different redis instances, you cannot intersect them.
  • Redis transactions involving multiple keys cannot be used.
  • When using partitioning, data processing is complex, such as when you need to work with multiple rdb/aof files and back up persistent files from multiple instances and hosts.
  • Increasing or deleting capacity is also complex. M ost redis clusters support the ability to increase and remove transparent data balance of nodes at runtime, but other systems such as client partitions, agents, etc. do not support this feature. However, a technique called presharding can help.

The type of partition

Redis has two types of partitions. A ssuming that there are four Redis instances of R0, R1, R2, R3, and something like user:1, user:2 that represent multiple keys for a user, there are several different ways to choose which instance the key is stored in. That is, there are different systems to map a key to a Redis service.

Range partition

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

For example, users with IDs from 0 to 10,000 are saved to instance R0, users with IDs from 10001 to 20,000 are saved to R1, and so on.

This approach is feasible and, in practice, is not enough to have a interval-to-instance mapping table. T his table is managed and requires a mapping of various objects, which is usually not a good approach for Redis.

Hash partition

Another partitioning method is the hash partition. T his applies to any key and does not object_name: this form is as simple as described below:

  • Convert key to a number with a hash function, such as the crc32 hash function. E xecuting crc32 (foobar) on key foobar outputs an integer similar to 93024922.
  • By modeling this integer and converting it into a number between 0-3, you can map the integer to one of the four Redis instances. 9 3024922 % 4 s 2, which means that key foobar should be saved in the R2 instance. Note: The molding operation is the remaining part of the removal and is usually implemented with the %operator in a variety of programming languages.