Home >Database >Redis >Learn more about Codis in Redis

Learn more about Codis in Redis

青灯夜游
青灯夜游forward
2022-01-06 09:54:122947browse

This article will take you to understand Codis in Redis and introduce the principles of Codis. I hope it will be helpful to you!

Learn more about Codis in Redis

Scenario

In a big data high concurrency scenario, using a single redis instance will become very difficult even if the performance of redis is high,

First of all, the larger the amount of data, the larger the memory occupied by redis, which further causes the RDB file to be too large. This situation will make the master-slave full synchronization time too long. At the same time, when the instance is restarted, the RDB that is too large will be loaded. It will also make the startup time longer. [Related recommendations: Redis Video Tutorial]

Secondly, regarding the use of CPU, a single instance of Redis can only use one CPU core. Too much data in one core will also appear It is beyond our capabilities,

Therefore, we need a cluster solution to disperse the huge amount of data from one instance to multiple instances. From the popularity of Redis to the official support of its own Cluster solution, third parties are also developing their own support. Codis is one of the components of the cluster.

Codis is developed using the Go language and acts as an agent between Redis and the client. It uses the Redis protocol, so the client can directly connect to Codis and send instructions to it. , Codis is responsible for forwarding instructions to Redis, and finally receiving the return results and returning them to the client.

Learn more about Codis in Redis

The Redis instance represented by Codis forms a Redis cluster, which can be used when the cluster space is not enough. At the same time, 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. You only need to change the original connection from redis to codis.

Codis itself can also Use a cluster to ensure its own high availability. Since it is stateless, it is only responsible for forwarding content. Adding multiple Codis has no side effects and can ensure the improvement of QPS. When one of the Codis fails, you can also use another one. .

Learn more about Codis in Redis

Principle

Codis forwards a specific Key to a specific Redis instance. Each instance in the cluster saves a part of the Key, reducing the pressure on other instances. At the same time, the data of all instances are added up to form a complete piece of information.

Codis is divided into 1024 slots by default. Each Redis instance in the cluster corresponds to a part of the slots. Codis will maintain the corresponding relationship between slots and Redis instances in the memory.

The number of slots is 1024 by default and can be changed. If there are many cluster nodes, the number can be increased.

Learn more about Codis in Redis

When receiving the key sent by the client, Codis performs a crc32 operation on the key to obtain a hash value,

and then the hashed The integer value is modulo 1024 (the number of slots) to get a remainder, which is the slot where the key will be saved. With the slot, you can find which redis instance the key should be sent to.

Pseudocode:

hash = crc32(command.key) # 计算hash值
slot = hash % 1024 # 取模得到槽位
redisInstance = slots[slot].redis # 得到redis实例
redis.do(command) # 执行命令复制代码

Cluster slot synchronization

The mapping relationship between Redis and slots exists in Codis memory , so the Codis cluster needs to consider ensuring that the slot mapping relationship in each node is synchronized, so Codis uses Zookeeper and Etcd distributed configuration storage middleware to persist the slot mapping relationship and ensure data synchronization between Codis clusters,

As shown below, Codis stores the slot relationship in Zookeeper and provides a Dashboard to observe and modify the slot relationship. When changes occur, Codis Proxy listens to the change and resynchronizes the slot relationship.

Learn more about Codis in Redis

Expansion

When the existing cluster does not meet business needs, you need to add a new one When the instance is added to the cluster, the slot mapping relationship needs to be redistributed, and a part of the slots needs to be allocated to the new node.

Codis has added a new SLOTSSCAN instruction, which can traverse all keys under a specified slot. Use this instruction to scan out all keys in the slot to be migrated, and then traverse each key one by one to migrate to the new node.

During the migration process, Codis continued to provide external services. At this time, a request came to the slot being migrated. Since the slot now corresponds to the old and new nodes, Codis cannot determine whether the key has There is no migration from the old node to the new node.

Therefore, in this case, Codis will immediately force a single migration of the current key. After the migration is completed, the request will be forwarded to the new Redis instance.

Pseudocode:

slot_index = crc32(command.key) % 1024
if slot_index in migrating_slots:
    doMigratingKey(command.key)
    redis = slots[slot_index].new_redis
else:
    redis = slots[slot_index].redis复制代码

SLOTSSCAN Like Redis’s own Scan command, duplication of scanned data cannot be avoided, but this will not affect the correctness of the migration, because after a single key is migrated, It is immediately deleted from the old instance and can no longer be scanned out.

Auto balancing slots

Every time a new instance is added, it would be too troublesome to manually maintain the slot mapping relationship. Codis provides automatic balancing. This function will observe the number of slots corresponding to each Redis instance when the system is relatively idle. If it is unbalanced, it will Perform automatic balancing and data migration operations.

Disadvantages

Codis brings expansion benefits to Redis, but it also causes some side effects.

Does not support transactions

A transaction may operate on multiple keys, but the transaction can only be completed in a single instance, but because the keys are scattered in different instances , so Codis cannot support transaction operations.

rename is not supported

rename names one key to another key, but the two keys may not hash into the same slot, but in in slots of different instances, so rename is not supported.

Officially provided list of unsupported instructions: https://github.com/CodisLabs/codis/blob/master/doc/unsupported_cmds.md

Expansion stuck

During the expansion process of Codis, the data migration is to directly migrate the entire key, such as a hash structure, Codis will directly hgetall pull all the content, and use hmset to put it into the new node,

If the content of the hash is too large, it will cause lag. Officials recommend that the total size of a single collection structure does not exceed 1MB. In business, large data can be split into multiple small ones through bucket storage, etc. , make a compromise.

Network overhead

Since Codis acts as a network proxy between the client and the Redis instance, there is an extra layer, and the network overhead is naturally higher, which is higher than directly connecting to Redis. Performance is slightly lower.

Middleware operation and maintenance overhead

Codis cluster configuration requires the use of Zk or Etcd, which means that when introducing a Codis cluster, other middleware must be introduced to increase operation and maintenance machine resources. cost.

Advantages

Codis hands over the distributed consistency issue to a third party (ZK or Etcd), eliminating this aspect In order to achieve decentralization, the Redis official Cluster introduced the Raft and Gossip protocols, as well as a large number of configuration parameters that need to be tuned, and the complexity increased sharply.

Batch acquisitionFor batch operations, such as using mget to obtain the values ​​​​of multiple keys, these keys may be scattered among multiple instances. Codis groups the keys according to the instances where they are located, then calls mget on each instance one by one, and finally returns the summary to the client.

Learn more about Codis in Redis

Other functionsCodis provides Dashboard interface and Codis-fe manages the cluster. You can also add groups, nodes, perform automatic balancing and other operations, and view the slot status and the redis instance corresponding to the slot. These functions make operation and maintenance more convenient and easier.

Learn more about Codis in Redis

Learn more about Codis in Redis

Learn more about Codis in Redis

Learn more about Codis in Redis

##Toward History

Codis appeared to make up for the fact that Redis officially did not provide the concept of cluster. Now Redis officially provides the Cluster function. The official support is naturally more advantageous than third-party support.

At the same time, third-party software also needs to pay attention to the new features released by the official in real time, and Cluster is definitely compatible with new features in real time, so it is more recommended to use the official Cluster. As a knowledge point in the past, Codis has certain ideas and Cluster There is overlap.

For more programming-related knowledge, please visit:

Introduction to Programming

! !

The above is the detailed content of Learn more about Codis in Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete