Get the App
SLTechnology News&Howtos  ›  Database  › 

How does redis achieve load balancing?

Shulou Source: shulou.com Published: 2022-06-01 20:58:42 10月02日 Update

The following brings you how redis achieves load balancing. I hope it can give you some help in practical application. Load balancing involves many things, and there are not many theories. There are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.

Redis load balancing

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

Redis slot allocation mechanism:

In the official cluster scheme given by redis, the data is allocated according to the slot. The key of each data is mapped to a slot by the hash function. Redis-3.0.0 stipulates that there are a total of 16384 slots. Of course, this can be configured according to the user's preference. When a user put or get a data, it will first look up the corresponding slot of the data, and then find the corresponding node, and then put the data into this node. In this way, the data is evenly distributed to each node in the cluster, so as to achieve the load balance of each node and give full play to the power of the cluster.

Public static void main (String [] args) {List shards = new ArrayList (); 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);}

These are the details of how to achieve load balancing in redis. Please pay more attention to other related articles!

Tags: Equilibrium data distribution nodes clusters that is mechanisms users slots in-line below industry things books content functions can be passed power official actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Huawei Shulou Technology Apple