Redis partition
Partitioning is the process of splitting data into multiple Redis instances, so each instance only saves a subset of the key.
Advantages of Partitioning
Allows us to construct larger databases by utilizing the sum of the memory of multiple computers.
Allows us to expand computing power through multi-cores and multiple computers; allows us to expand network bandwidth through multiple computers and network adapters.
Disadvantages of partitioning
Some features of redis do not perform very well in terms of partitioning:
Involving multiple keys The operation is generally not supported. For example, when two sets are mapped to different redis instances, you cannot perform an 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 back up persistent files from multiple instances and hosts.
Adding or deleting capacity is also more complicated. Most redis clusters support the ability to add and delete nodes at runtime for transparent data balancing, but other systems such as client partitions and proxies do not support this feature. However, a technique called presharding can help with this.
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 and user:2. For a given key, there are many different ways to choose which instance the key should be stored in. . In other words, there are different systems for mapping a certain key to a certain Redis service.
Range Partitioning
The simplest partitioning method is by range partitioning, which is to map a certain range of objects to a specific Redis instance.
For example, users with IDs from 0 to 10000 will be saved to instance R0, users with IDs from 10001 to 20000 will be 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 requires mapping tables for 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 does not need to be object_name:
Convert the key to a number using a hash function, such as using the crc32 hash function. Executing crc32(foobar) on key foobar will output an integer similar to 93024922.
Take the modulo of this integer and convert it into a number between 0 and 3. This integer can be mapped to one of the four Redis instances. 93024922 % 4 = 2, which means that key foobar should be stored in the R2 instance. Note: The modulo operation is the remainder of division, which is usually implemented using the % operator in many programming languages.