Home  >  Article  >  Database  >  How redis implements load balancing

How redis implements load balancing

silencement
silencementOriginal
2019-06-06 16:44:208787browse

How redis implements load balancing

redis load balancing

When high concurrency occurs in a web project, it can be handled through load balancing. The slot allocation mechanism of redis is A load balancing mode

redis slot allocation mechanism:

In the cluster solution officially given by redis, data is allocated according to slots, and the key of each data It is mapped to a slot by the hash function. Redis-3.0.0 stipulates a total of 16384 slots. Of course, this can be configured according to the user's preferences. When the user puts or gets a piece of data, it first searches for the slot corresponding to the data, then searches for the corresponding node, and then puts the data into the node. In this way, the data is evenly distributed to each node in the cluster, thereby achieving load balancing on each node and giving full play to the power of the cluster. (redis tutorial)

public static void main(String[] args) {
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
        shards.add(new JedisShardInfo("127.0.0.1", 6379));
        shards.add(new JedisShardInfo("127.0.0.1", 6380));

        ShardedJedisPool sjp = new ShardedJedisPool(new JedisPoolConfig(), shards);
        ShardedJedis shardClient = sjp.getResource();
        try {
            shardClient.set("A", "123");
            shardClient.set("B", "234");
            shardClient.set("C", "345");

            try {
                System.out.println(shardClient.get("A"));
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                System.out.println(shardClient.get("B"));
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                System.out.println(shardClient.get("C"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sjp.returnResource(shardClient);
        }
    }

}

The above is the detailed content of How redis implements load balancing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to view redis logsNext article:How to view redis logs

Related articles

See more