In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces you how to deeply understand Codis in Redis. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
Scene
In big data's high concurrency scenario, using a single redis instance, no matter how high the performance of redis, will become very difficult.
First of all, the larger the amount of data, the greater the memory consumption of redis, which further causes the rdb file to be too large, which will make the master-slave synchronization time too long. At the same time, when the instance is restarted, loading too large rdb will also make the startup time longer. [related recommendation: Redis video tutorial]
Secondly, in the use of CPU, the Redis of a single instance can only use one CPU core, and a core should have too much data, which will also seem inadequate.
Therefore, we need a cluster solution to spread the huge amount of data from one instance to multiple instances. from the popularity of Redis to the official support of their own Cluster solution, third parties are also developing their own components to support clusters, and Codis is one of them.
Codis is developed in GE language, acts as an agent between Redis and the client, and uses Redis protocol, so the client connects directly to Codis and sends instructions to it. Codis is responsible for forwarding instructions to Redis, and finally receives the returned result and returns it to the client.
The Redis instances of the Codis agent form a Redis cluster. When there is not enough space in the cluster, you can dynamically expand the capacity and continue to add Redis instances. At the same time, the sdk used by the client does not need to be changed, only from the original connection redis to the connection codis.
Codis can also adopt a cluster to ensure its own high availability. Because it is stateless and is only responsible for forwarding content, adding multiple Codis without side effects can also ensure the improvement of QPS. When one of the Codis dies, you can also use others.
Principle
Codis forwards a specific Key to a specific Redis instance, and each instance in the cluster saves a portion of the Key to reduce the pressure on other instances. At the same time, the data of all instances add up to a complete piece of information.
Codis defaults to 1024 slots (slot). Each Redis instance in the cluster corresponds to a part of the slot. Codis maintains the corresponding relationship between the slot and the Redis instance in memory.
The number of slots is 1024 by default, which can be changed. If there are more cluster nodes, you can increase the number.
When receiving the key sent by the client, Codis performs the crc32 operation on the key to get a hash value
Then take the integer value after hash to 1024 (number of slots) to get a remainder, which is the slot to which the Key will be saved. With the slot, you can find which redis instance the key should be sent to.
Pseudo code:
Hash = crc32 (command.key) # calculate hash value slot = hash% 1024 # get slot redisInstance = slots [slot] .redis # get redis instance redis.do (command) # execute command to copy code
Cluster slot synchronization
The mapping relationship between Redis and slots is stored in Codis memory, so Codis clusters need to consider ensuring the synchronization of slot mappings in each node, so Codis uses Zookeeper and Etcd distributed configuration storage middleware to persist slot mappings to ensure data synchronization between Codis clusters.
As shown in the following figure, Codis stores the slot relationship in Zookeeper and provides a Dashboard to observe and modify the slot relationship. When a change occurs, the Codis Proxy listens for the change and resynchronizes the slot relationship.
Expand the capacity
When the existing cluster does not meet the business needs, you need to add new instances to the cluster. At this time, the slot mapping relationship needs to be redistributed, and a part of the slot needs to be allocated to the new node.
Codis adds a new SLOTSSCAN instruction, which can traverse all the key under the specified slot, scan all the key of the slot to be migrated through this instruction, and then traverse each key one by one to migrate to the new node.
During the migration, Codis continued to provide services, and a request came and hit the slot being migrated. Since the slot now corresponds to the new and old nodes, Codis cannot determine whether the key has migrated from the old node to the new node.
In this case, Codis immediately forces a single migration of the current key, and when the migration is complete, the request is forwarded to the new Redis instance.
Pseudo code:
Slot_index = crc32 (command.key)% 1024if slot_index in migrating_slots: doMigratingKey (command.key) redis = slotts [slot _ index] .new _ rediselse: redis = slotts [slot _ index] .redis copy code
Like the Scan instruction of Redis itself, SLOTSSCAN cannot avoid the duplication of scanned data, but this does not affect the correctness of the migration, because a single key is deleted from the old instance immediately after migration and cannot be scanned again.
Automatic equalization slot
For each new instance, if it is too troublesome to manually maintain the mapping relationship of slot, Codis provides automatic balancing. This feature will observe the number of slot corresponding to each Redis instance when the system is relatively idle. If it is not balanced, it will automatically balance and migrate data.
Shortcoming
Codis brings expansion benefits to Redis, but it also causes some side effects.
Transactions are not supported
A transaction may operate on multiple key, but the transaction can only be completed in a single instance, but because the key is scattered in different instances, Codis cannot support transaction operations.
Rename is not supported
Rename names one key to another key, but the two key may not hash out of the same slot, but on the slot of different instances, so rename is not supported.
Official list of unsupported instructions: https://github.com/CodisLabs/codis/blob/master/doc/unsupported_cmds.md
Capacity expansion Catton
During the expansion process of Codis, the data migration is to migrate the whole key directly. For example, in a hash structure, Codis will pull all the content directly by hgetall and use hmset to put it into the new node.
If the content of the hash is too large, it will cause stutters. Officials recommend that the total size of a single collection structure should not exceed 1MB. In business, large data can be split into multiple small ones through bucket storage to make a compromise.
Network overhead
Because Codis acts as a network Proxy between the client and the Redis instance, with one more layer, the network overhead is naturally higher, which is slightly lower than the performance of directly connecting to the Redis.
Middleware operation and maintenance cost
Codis cluster configuration requires the use of Zk or Etcd, which means that the introduction of Codis cluster requires the introduction of other middleware, increasing the cost of operation and maintenance machine resources.
Advantages
Codis leaves the problem of distributed consistency to a third party (ZK or Etcd), which saves the maintenance work and reduces the complexity of the implementation code.
In order to achieve decentralization, the official Cluster of Redis introduces Raft and Gossip protocols, as well as a large number of configuration parameters that need to be tuned, resulting in a sharp increase in complexity.
Batch acquisition
For batch operations, such as using mget to obtain the values of multiple key, these key may be scattered in multiple instances. Codis groups key by instance, then calls mget on each instance one by one, and finally returns the summary to the client.
Other featur
Codis provides Dashboard interface and Codis-fe to manage clusters. It can also add groups, nodes, perform automatic balancing, and check slot status and redis instances corresponding to slot. These features make operation and maintenance more convenient and easy.
Codis is to make up for the fact that Redis officials do not provide the concept of cluster. Now that Redis officials provide Cluster functions, official support naturally has more advantages than third parties.
At the same time, third-party software also needs to pay real-time attention to the official release of a variety of new features, and Cluster must be real-time compatible with new features, so it is more recommended to use the official Cluster,Codis as a knowledge point to understand, some ideas coincide with Cluster.
On how to in-depth understanding of the Codis in Redis to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.