Redis集群明细文档
Redis目前版本是没有提供集群功能的,如果要实现多台Redis同时提供服务只能通过客户端自身去实现(Memchached也是客户端实现分布式
Redis目前版本是没有提供集群功能的,如果要实现多台Redis同时提供服务只能通过客户端自身去实现(Memchached也是客户端实现分布式)。目前根据文档已经看到Redis正在开发集群功能,其中一部分已经开发完成,但是具体什么时候可以用上,还不得而知。文档来源:
一、介绍
该文档是开发之中的redis集群实现细节。该文档分成两个部分,第一部分为在redis非稳定版本代码分支上已经实现的,另外一部分为还需要去实现的。在未来若集群实现设计变更这些都可能被修改,但是相对来说,未实现的部分相较于已经实现的部分被修改的可能性更大些。该文档包括了实现客户端需要的各种细节,但是客户端作者需要注意这些细节都有可能被修改。
二、什么是Redis集群
集群是独立服务器关于分布式与容错实现的一个子集。在集群之中没有中心节点与代理节点,设计的主要目的之一就是线性可伸缩的扩展(即随意增删节点)。集群为了保证数据的一致性而牺牲容错性,所以当网络故障和节点发生故障时这个系统会尽力去保证数据的一致性和有效性。(这里我们认为节点故障是网络故障的一种特殊情况)
为了解决单点故障的问题,我们同时需要masters 和 slaves。 即使主节点(master)和从节点(slave)在功能上是一致的,甚至说他们部署在同一台服务器上,,从节点也仅用以替代故障的主节点(即备节点不会被使用除非主节点发生故障而用来代替主节点)。 实际上应该说 如果对从节点没有read-after-write(写并立即读取数据 以免在数据同步过程中无法获取数据)的需求,那么从节点仅接受只读操作。
三、已经实现的子集
集群实现了在非分布式版本上的所有单个命令。复杂多命令操作例如集合sets的交集并集还没有实现。通常情况下理论上对于不在同一个节点上的操作不会被实现。
将来可能增加一种叫"Computation Node"的新节点类型(计算节点),这种节点主要用来处理在集群中multi-key的只读操作。但是对于multi-key的只读操作不会以集群传输到Computation Node节点再进行计算的方式实现。
(有点绕啊,看英文吧:In the future it is possible that using the MIGRATE COPY command users will be able to use Computation Nodes to perform multi-key read only operations in the cluster, but it is not likely that the Redis Cluster itself will be able to perform complex multi key operations implementing some kind of transparent way to move keys around.)
集群中的节点不像独立服务器那样支持多个数据库,将会只有数据库0,并且不支持select命令。
四、集群协议中的客户端与服务器角色
在集群中的节点,负责存储数据,了解集群的状态以及包括把数据key映射到对应的节点。集群中的任何一个节点都能够自动发现其他节点和检测到故障的节点,并且在必要是时候执行备节点提升为主节点的操作。
集群节点使用TCP bus(连接总线)和二进制协议进行互联并对任务进行分派。即每一个节点都使用TCP bus连接到集群中的其他节点。各节点使用gossip 协议发送消息:
1)传播消息(propagate information)以发现新节点,
2)发送ping packets给集群其他节点以检测其他节点是否工作正常,
3)发送集群消息需要特定的信号条件
集群连接总线也可以是在各个节点间传播PUB/SUB消息。
当集群中的节点不能满足客户端的请求时,可能会使用errors -MOVED and -ASK命令来告知重定向到可用节点。理论上客户端允许向集群中的任意节点发送请求并在必要时会得到重定向应答,即客户端不必关心集群的状态。然而客户端可以缓存数据keys与节点的映射关系以改免去服务器端再进行重定向,这在一定程度上可以提高性能。
五、关键字分布式模型
key空间被分割成16384 个slots(节点),实际上集群最大节点数为16384 个。然后建议的最大值为小几百个节点。所有的主节点将处理4096个百分比的slot。当集群稳定时(即没有正在转移某个slot到另外一个节点),则某个slot必定只被某个节点处理,然后某个节点可以同时处理多个槽。
effectively setting an upper limit for the cluster size of 16384 nodes (however the suggested max size of nodes is in the order of ~ 1000 nodes).
映射键值到指定槽值的算法如下:
HASH_SLOT = CRC16(key) mod 4096
在该文档的附注1之中有CRC16算法介绍。
使用12满分的CRC16的16位输出,在我们的测试之中CRC16能够很好的将各种类型的key映射到4096的空间之中。
六、集群节点的属性
在集群中的每个节点都有其在集群中唯一的ID,其ID为160比特随机数的十六进制表示。节点首次启动时即获取ID,节点将获取其ID并保存在其配置文件之中,并且将一直使用该ID,直到该配置文件被系统管理员删除。
节点的ID作为集群中的节点识别,一个节点可能修改IP或地址但是不必须修改节点名称。集群也能够检测到节点IP、PORT的变化然后通过连接总线发送变更的协议通知。
每个节点都有一些相关的信息,被其他节点所知道:
1)该节点的IP地址与端口
2)一些标志位
3)该节点服务的key-slot
4)最后一次通过集群连接总线发送ping的时间
5)最后一次收到pong的时间
6)该节点的备份节点数
7)如果其为备份节点,则其主节点的ID(若该节点为主节点则该值为0000000
通过CLUSTER NODES命令可以获取该集群中的所有节点信息,包括主节点与备份节点。
如下为一个示例:
$ redis-cli cluster nodes
d1861060fe6a534d42d8a19aeb36600e18785e04 :0 myself - 0 1318428930 connected 0-1364
3886e65cc906bfd9b1f7e7bde468726a052d1dae 127.0.0.1:6380 master - 1318428930 1318428931 connected 1365-2729
d289c575dcbc4bdd2931585fd4339089e461a27d 127.0.0.1:6381 master - 1318428931 1318428931 connected 2730-4095
上述显示各项信息依次为:ID、IP:PORT、 FLAGS、最近发送PING的时间,最近接受到PONG时间、连接状态、slots
Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里
推荐阅读:
Ubuntu 12.10下安装Redis(图文详解)+ Jedis连接Redis
Redis系列-安装部署维护篇
CentOS 6.3安装Redis
更多详情见请继续阅读下一页的精彩内容:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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: 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.

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.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
