In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the example analysis of Redis request routing, the article is very detailed, has a certain reference value, interested friends must read it!
Request redirection
Since it is redirection, we should understand that redirection refers to redirecting the original request to another place in some way, so what exactly does redirection in Redis refer to? We know that any key in Redis is stored in the slot, and we know from the last few articles that in the Redis cluster environment, the slot is evenly distributed among multiple master nodes, so when we execute any Redis command, it is very different from a single node, because the cluster environment first calculates the slot corresponding to the key, and then finds the corresponding node according to the slot. If, after calculation, the slot of the current key is the current node, then the command is processed directly. If it is not the current node after calculation, Redis will display a MOVED redirection error, that is, telling the client what the correct node of the slot corresponding to the key is. This is called the redirection of Redis. Let's demonstrate what has been said above.
Let's start a new node 6387. Because the node is not added to the cluster, we can directly return success when we execute the set command.
Let's execute the same command in the cluster.
We see that when executing the same command in a clustered environment, Redis prompts for a MOVED error. The reason is the same as in the above introduction. Let's take a look at the execution flow of MOVED redirection. This is shown in the following figure:
In Redis, we can use the cluster keyslot {key} command to view the slots corresponding to key, and then check the slots that each node is responsible for through the cluster nodes command, so that we can know which node to execute on when we execute the command. I also take the key above as an example. The specific operation is shown in the following figure:
We use the cluster keyslot {key} command to see that the slot of the key is 12691. Then we know through the cluster nodes command that node 6382 is responsible for slots 12288-16383. So when we execute the command, we can only succeed if we execute it on 6382 nodes, and executing Redis on other nodes will report a MOVED redirection error. Next, we will execute the above failed command on the 6382 node.
When we use redis-cli to execute commands in Redis, we can add the-c parameter, which automatically redirects, simplifying our manual redirection.
The execution of the key command is mainly divided into two steps: calculating the slot and finding the node where the slot is located. Next, we will introduce these two aspects in detail.
Calculation slot
Redis calculates the hash value of the key based on the use of the CRC16 function, and then takes the remainder of 16383 with the hash value, so that each key can be mapped to the range of 0-16383 slots.
Slot node lookup
After Redis calculates the slot corresponding to the key, it needs to find the node where the slot is located. Each node in the cluster knows the information about the slots of all nodes through message exchange. According to the MOVED redirection mechanism, the client can randomly connect to any node in the cluster where the Redis acquires the key. This kind of client is called the zombie client. Its advantage is that the code is easy to implement and has little impact on the client protocol. It only needs to send the request again according to the redirection information. But this also has its drawback, that is, every time you execute a key command, you have to redirect to the Redis to find the node that you want to execute the command. This adds additional IO overhead. It is precisely because of this drawback that Redis provides another technology implementation, also known as Smart (smart) client, in the next article, we will introduce the content related to Smart.
The above is all the contents of the article "sample Analysis of Redis request routing". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.