Home >Database >Redis >How many slots does redis have?

How many slots does redis have?

anonymity
anonymityOriginal
2019-06-05 10:42:463696browse

There are 16384 hash slots built into the Redis cluster. When a key-value needs to be placed in the Redis cluster, redis first uses the crc16 algorithm to calculate a result for the key, and then calculates the result against 16384 Remainder, so that each key will correspond to a hash slot numbered between 0-16383, and redis will map the hash slots to different nodes roughly equally according to the number of nodes. Redis cluster does not use consistent hashing, but introduces the concept of hash slots.

How many slots does redis have?

The Redis cluster has 16384 hash slots. Each key is checked modulo 16384 after CRC16 verification to determine which slot to place. Each node in the cluster is responsible for a part of the hash. groove. This structure makes it easy to add or delete nodes, and whether a node is added, deleted, or modified, it will not cause the cluster to become unavailable.

The advantage of using hash slot is that you can easily add or remove nodes.

When you need to add a node, you only need to move some hash slots of other nodes to the new node;

When you need to remove a node, Just move the hash slot on the removed node to other nodes;

At this point, we don’t need to stop all redis services when adding or removing nodes in the future. .

In redis, the simplest way to put a key-value pair is to set key value, as shown below:

127.0.0.1:7000> set key value
-> Redirected to slot [12539] located at 192.168.39.153:7002
OK
192.168.39.153:7002> get key
"value"
192.168.39.153:7002>

It can be seen that when we When setting the key value to value, the client was redirected to another node 192.168.39.153:7002. This is because the slot corresponding to the key is 12359, so our key-value is placed in the slot corresponding to 12359. Node, 192.168.39.153:7002.

Redis Cluster is a simple hash algorithm of crc16 made by myself, without consistent hashing. The author of Redis believes that the effect of its crc16(key) mod 16384 is already good. Although it is not as flexible as consistent hashing, the implementation is very simple, and it is also very convenient to handle when nodes are added or deleted.

Not losing data when nodes are added or deleted has nothing to do with the hash algorithm. What is required to not lose data is that one piece of data has multiple copies.

When you add a Key to Redis Cluster, it will be calculated according to crc16(key) mod 16384 which hash slot the key should be distributed to. There will be many keys and values ​​in a hash slot. You can understand it as table partition. When using redis on a single node, there is only one table, and all keys are placed in this table. After switching to Redis Cluster, 16384 partition tables will be automatically generated for you. When you insert data, it will be based on The above simple algorithm determines which partition your key should exist in. There are many keys in each partition.

The above is the detailed content of How many slots does redis have?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn