In Redis, partitioning refers to splitting data into multiple instances. This means that each Redis holds a section of your entire data set. It is a handy feature, especially when dealing with large datasets.
Let us look at various types of partitioning in Redis and the cost and benefits of each.
Types of Redis Partitioning
There are two partitioning mechanisms in Redis:
The best way to explain partitioning is to use an analogy. Assume you have four Redis instances: R0, R1, R2, and R3. Also, assume you have to store keys in the format of user:[id].
You can use range partitioning to split data into the available Redis instances.
In range partitioning, you map a section of your data into a specific instance. For example, you can take the keys from user:[id<0> – id<1000>] and map it to instance R0. Instance r1 can hold the subset of keys from user[id<1001> – id<2000>] and so on.
This is the simplest and most applicable form of partitioning in Redis. However, it does provide a limitation.
Range partitioning requires a table that maps a specific range to its corresponding cluster.
The mapping table requires to be managed and applied for each partitioned data type.
The second alternative to range partitioning is hash partitioning. Redis uses a hashing function to convert a key into a number in this partitioning method.
For example, if we have a key “fact,” we can convert it into a number using a hashing function such as CRC-16.
This should return the number as 8167.
Redis then applies a modulo operation to the number and returns the result from 0 to 3.
The above operation should return 1. Redis then uses this value to determine which cluster the key should be stored in. In our example, the key “fact” is stored in the instance R1.
NOTE: This is not how hashing operation is performed in Redis. The above is just an analogy.
Advantages of Partitioning.
- Partitioning allows multiple instances to share the memory and store data.
- Allow scaling of computation power.
Disadvantages of Partitioning
- Multi-key operation is unsupported.
- You cannot perform a Redis transaction using multiple keys.
- Introduces ambiguity when dealing with persistence.
- Shrinking or growing data proves difficult.
In this guide, you discovered the various mechanism for Redis partitioning. Check the documentation to learn more.