This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Clustering solutions

Sentinel of Redis solves the problem that the master/slave replication failure cannot be migrated automatically, but the write performance and storage capacity of the master node are still limited by the limited capacity of the Redis single machine, so the Redis cluster is used to solve this problem, and the data of Redis is distributed to multiple machines according to certain rules.

Redis cluster solution

The Redis Cluster Cluster mode usually has the following features: high availability, scalability, distributed, and fault tolerance. There are generally two Redis distributed schemes:

Client partitioning scheme

The client has already decided which Redis node the data will be stored in or read from. The main idea is to use hash algorithm to hash the key of Redis data. Through hash function, the specific key will be mapped to the specific Redis node.

The representative of client partition scheme is Redis Sharding, which is the Redis multi-instance Cluster method widely used in the industry before the emergence of Redis Cluster. Java Redis client driver library Jedis, support Redis Sharding function, namely ShardedJedis and ShardedJedisPool combined with cache pool.

advantages

No third-party middleware, controllable partition logic, simple configuration, no association between nodes, easy linear expansion, strong flexibility.

disadvantages

Clients cannot dynamically add or delete service nodes. Clients need to maintain distribution logic by themselves. If no connection is shared between clients, connections are wasted.

Proxy partitioning scheme

The client sends the request to a proxy component, which parses the client’s data, forwards the request to the correct node, and finally returns the result to the client.

  • Advantages: Simplified client distributed logic, transparent client access, low switching cost, and proxy forwarding and storage separation.

  • Disadvantages: An additional layer of agents, which increases the complexity of architecture deployment and performance costs.

The main schemes of proxy partition are Twemproxy and Codis.

Twemproxy

Twemproxy, also known as Nutcraker, is an open source redis and Memcache intermediate proxy server application for Twitter. Twemproxy as a proxy, can accept access from multiple programs, according to the routing rules, forward to the background of each Redis server, and then back to the original route. Twemproxy has a single point of failure problem, so it needs to combine Lvs and Keepalived to make a high availability solution.

  • Advantages: wide application range, high stability, high availability of intermediate agent layer.

  • Disadvantages: Smooth horizontal capacity expansion/reduction, no visual management interface, unfriendly operation and maintenance, failure, cannot automatically transfer.

Codis

Codis is a distributed Redis solution, and for upper-layer applications, there is no difference between connecting to CoDIS-Proxy and connecting directly to native Redis-Server. The Codis layer will handle the forwarding of requests, non-stop data migration and other work. Codis uses a stateless proxy layer where everything is transparent to the client.

advantages

It realizes the high availability of upper Proxy and bottom Redis, data sharding and automatic balance, provides command line interface and RESTful API, provides monitoring and management interface, and can dynamically add and delete Redis nodes.

disadvantages

The deployment architecture and configuration are complex and do not support cross-equipment room, multi-tenant, or authentication management.

Cluster Cluster solution

Cluster mode is a distributed service Cluster composed of multiple node groups, and Redis data is distributed in these nodes. The cluster can perform automatic failover without Sentinel Sentinel. To use clusters, enable cluster-enable in the Redis configuration file.

Each node will communicate with each other. The gossip protocol is used to exchange node metadata information. Each node is connected to all other nodes. To ensure high availability, a cluster must have at least six nodes, that is, three active nodes and three slave nodes.

The client requests any random Redis instance, and Redis forwards the request to the correct Redis node. Redis Cluster implements a hybrid form of query routing, but rather than directly forwarding requests from one Redis node to another Redis node, it redirects directly to the correct Redis node with the help of the client.

Core principles

Redis Cluster uses hash slots in data fragments to store and read data. The cluster predivides 16,384 slots, and each node is responsible for part of the slots. When a data needs to be placed in a Redis Cluster, the Cluster by default decides which slot to place a key in based on the value of CRC16(Key) mod 16384.

Common Configurations
  • Cluster-enabled yes: Enable cluster mode (cluster)

  • Cluster-config-file: specifies the location of the cluster configuration file to record cluster node information. When starting in cluster mode, the system first looks for a cluster configuration file. If so, the system starts using the configuration in the file. If not, the system initializes the configuration and saves it to the file

  • Cluster-node-timeout time: node connection timeout time

  • Cluster-announce-ip IP: IP address of the current node in the cluster

  • Cluster-announce-port Port: indicates the cluster node mapping port

advantages

Without a central node, data is distributed on multiple Redis instances according to slots, enabling smooth node expansion/reduction, high availability and automatic failover, and low operation and maintenance costs.

disadvantages

Heavy reliance on Redis-Trib tools, lack of monitoring management, need to rely on Smart Client (maintenance connection, cache routing table, MultiOp and Pipeline support).

  • The detection of the Failover node is slower than that of the ZooKeeper node.
  • The Gossip messages have some overhead.
  • Cold and hot data cannot be statistically distinguished.