Redis Partitioning
# Redis Partitioning
Partitioning is the process of splitting data across multiple Redis instances, so that each instance holds only a subset of the keys.
### Advantages of Partitioning
* Allows constructing larger databases by leveraging the combined memory of multiple computers.
* Enables scaling computational power through multiple cores and computers; enables scaling network bandwidth through multiple computers and network adapters.
### Disadvantages of Partitioning
Some Redis features do not work well with partitioning:
* Operations involving multiple keys are generally unsupported. For example, if two sets are mapped to different Redis instances, you cannot perform intersection operations on these two sets.
* Redis transactions involving multiple keys cannot be used.
* Data handling becomes more complex when using partitioningβfor example, you need to manage multiple RDB/AOF files and back up persistence files from multiple instances and hosts.
* Adding or removing capacity is also more complex. Most Redis clusters support transparent data rebalancing during runtime when adding or removing nodes, but other systems such as client-side partitioning or proxy-based solutions typically lack this capability. However, a technique called presharding can help address this issue.
* * *
## Types of Partitioning
Redis supports two types of partitioning. Suppose there are four Redis instances: R0, R1, R2, and R3, and multiple keys representing users such as `user:1`, `user:2`, etc. There are various ways to determine which instance a given key should be stored inβthat is, different systems map keys to Redis servers.
### Range Partitioning
The simplest partitioning approach is range partitioning, which maps objects within a certain range to specific Redis instances.
For example, users with IDs from 0 to 10000 are stored in instance R0, users with IDs from 10001 to 20000 are stored in R1, and so on.
This approach is viable and used in practice, but its drawback is requiring a mapping table from ranges to instances. This table must be managed, and often requires additional mapping tables for various object typesβusually not ideal for Redis.
### Hash Partitioning
Another partitioning method is hash partitioning. It works for any keyβnot necessarily in the form `object_name:`βand is straightforward as described below:
* Use a hash function to convert the key into a numberβfor example, using the `crc32` hash function. Applying `crc32("foobar")` yields an integer like `93024922`.
* Apply modulo operation on this integer to map it into the range 0β3, thus assigning the key to one of the four Redis instances. `93024922 % 4 = 2`, meaning key `"foobar"` should be stored in instance R2. Note: The modulo operation returns the remainder after division and is commonly implemented using the `%` operator in many programming languages.
YouTip