编程知识 cdmana.com

Redis fragmentation mechanism

Why fragmentation is needed

If you need to store huge amounts of memory data , If you only use one redis, There is no guarantee that redis The efficiency of work . A lot of time is wasted in addressing , So we need a mechanism to meet this requirement .
Using the fragmentation mechanism to achieve :
image.png

Redis Build in pieces

Construction precautions

Redis The startup of the service depends on redis.conf Configuration file for , If you need more than one redis You need more than one redis.conf Configuration of .
Prepare port number :
1.6379
2.6380
3.6381

Sharding implementation

image.png
Modify port number : Modify the respective port number
1. Into their own redis.conf In file
2. Modify the respective port number
image.png
start-up redis The server
image.png
Verify that the server is running properly
image.png

Notes on slicing

1. Problem description :
When you start multiple redis After the server , More than one redis There is no necessary connection for the time being , Each is a separate entity , You can store data .
As shown in the figure :
2. If the partition is operated in the way of program , To put 3 too redis As a whole , So it's totally different from the above operation , There won't be a key Save to more than one at the same time redis The phenomenon of .
image.png

Introduction case of segmentation

/**
     *  test Redis Fragmentation mechanism 
     *  reflection : shards  How to determine which one should be stored in redis What about China? ???
     */
    @Test
    public void testShards(){
        List<JedisShardInfo> shards = new ArrayList<>();
        shards.add(new JedisShardInfo("192.168.126.129",6379));
        shards.add(new JedisShardInfo("192.168.126.129",6380));
        shards.add(new JedisShardInfo("192.168.126.129",6381));
        // Ready to slice objects 
        ShardedJedis shardedJedis = new ShardedJedis(shards);
        shardedJedis.set("shards","redis Slice test ");
        System.out.println(shardedJedis.get("shards"));
    }

Uniformity hash Algorithm

General hash yes 8 by 16 Forbidden number .0---9 A--F The combination is 2^32.
If you do the same data hash operation , The result must be the same .
A data 1M And data 1G Of hash The speed of operation is the same .

Uniformity hash Algorithm is introduced

The consistency hash algorithm 1997 Proposed by MIT in , It's a special hash algorithm , The purpose is to solve the problem of distributed cache . [1] When removing or adding a server , Be able to change the mapping relationship between existing service requests and processing request servers as little as possible . Consistent hashing solves the problem of simple hashing algorithms in distributed hashes ( Distributed Hash Table,DHT) Dynamic scaling and other problems in .
image.png

characteristic

Balance

Concept : Balance means hash The results should be distributed equally among the nodes , In this way, the load balancing problem is solved from the algorithm .( Roughly average )
Problem description : It's all due to the nodes hash How to calculate . So it may appear as shown in the figure ., It leads to serious load imbalance .
image.png
resolvent : Introduce virtual nodes
image.png

monotonicity

characteristic : Monotonicity refers to adding or deleting nodes , It does not affect the normal operation of the system .
image.png

Dispersion

Decentralization refers to the fact that data should be stored in distributed cluster nodes ( The node itself can have backup ), It's not necessary for every node to store all the data .

SpringBoot Integrate Redis Fragmentation

Edit profile

#  To configure redis Single server 
redis.host=192.168.126.129
redis.port=6379

#  To configure redis Fragmentation mechanism 
redis.nodes=192.168.126.129:6379,192.168.126.129:6380,192.168.126.129:6381

Edit configuration class

@Configuration
@PropertySource("classpath:/properties/redis.properties")
public class JedisConfig {

    @Value("${redis.nodes}")
    private String nodes;  //node,node,node.....

    // To configure redis Fragmentation mechanism 
    @Bean
    public ShardedJedis shardedJedis(){
        nodes = nodes.trim();   // Remove extra space on both sides 
        List<JedisShardInfo> shards = new ArrayList<>();
        String[] nodeArray = nodes.split(",");
        for (String strNode : nodeArray){   //strNode = host:port
            String host = strNode.split(":")[0];
            int port = Integer.parseInt(strNode.split(":")[1]);
            JedisShardInfo info = new JedisShardInfo(host, port);
            shards.add(info);
        }
        return new ShardedJedis(shards);
    }
   }

modify AOP The injection term

image.png

版权声明
本文为[Xiaofei]所创,转载请带上原文链接,感谢

Scroll to Top