Table of Contents
Scenario
Principle
Home Database Redis Learn more about Codis in Redis

Learn more about Codis in Redis

Jan 06, 2022 am 09:54 AM
redis

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) # 执行命令复制代码
Copy after login

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复制代码
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to use redis lock How to use redis lock Apr 10, 2025 pm 08:39 PM

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

How to implement the underlying redis How to implement the underlying redis Apr 10, 2025 pm 07:21 PM

Redis uses hash tables to store data and supports data structures such as strings, lists, hash tables, collections and ordered collections. Redis persists data through snapshots (RDB) and append write-only (AOF) mechanisms. Redis uses master-slave replication to improve data availability. Redis uses a single-threaded event loop to handle connections and commands to ensure data atomicity and consistency. Redis sets the expiration time for the key and uses the lazy delete mechanism to delete the expiration key.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to make message middleware for redis How to make message middleware for redis Apr 10, 2025 pm 07:51 PM

Redis, as a message middleware, supports production-consumption models, can persist messages and ensure reliable delivery. Using Redis as the message middleware enables low latency, reliable and scalable messaging.

See all articles