Alexandr Alexandr - 1 year ago 83
Java Question

How to enable load balance in Jedis?

I want to use Jedis for Redis cluster or just for Redis servers. I wonder to know how to enable Round Robin for read operations. Doesn't matter what I will use Redis cluster or Redis servers with Sentinel. I will have several masters and each master will have it's own slaves. How to distribute read operations among all slaves for a particular hash slot (or for a particular shard)?

Answer Source

Load balancing is a very significant problem in Redis. One consideration is to define what load balancin means.

Load balancing mechanisms in IMKVS can be examined under three lenses if we only consider the intrinsic factors of uneven load distribution:

  1. load balancing based on key popularity: The frequency that specific items are invoked seems to be related to the performance of the IMKVS node and system.

  2. load balancing based on key distribution: In many systems the distribution of the keys in the available nodes are inherently posing a load balancing challenge. The examination of the problem in isolation from the previous as well as from the capabilities of the underlying nodes can be risky, however it is far more simple in terms of implementation, even though based on mere statistics.

  3. load balancing based on query volume: The amount of keys requested concurrently from a IMKVS deployment and consequently from a node seems to be a metric of interest in some load balancing deployments.

So to summarize, investigation of the load balancing problem requires the examination of at least these factors:

  • frequency of incoming queries

  • volume of incoming queries

  • the way the keys are distributed in the system

  • key length and value length


According to my experiments:

  • The response time (RT) to get one particular key hardly changed, as the number of keys changed in the node.
  • The RT grew rapidly, with an increased length of the key.
  • The difference of response times between the volumes of the values is insignificant and thus the value size does not affect RT.
  • The growth rate of RT is proportional the number of client calls.

The distribution of the keys plays an important role in the load balancing factor. Thus, in the last experiment my aim was to observe how response time changes through the migration of keys from one node to another in Redis Cluster. For the needs of my experiment i loaded 33000 in one master (M1) node and 0 in the other master (M2). I made 4 concurrent client calls in M1 and i started migration at time 7 and stopped at time 36.

enter image description here

Ι observed that the RT for 4 concurrent client calls averaged a 25 ms. In the beginning of the migration process the response times present a sudden increase while at the end of the process we notice a reduction in the order of magnitude of 9ms. This improvement indicates a 3x speedup. The spikes are due to the overhead of the migration command. This command actually executes a DUMP+DEL operation in the source instance, and a RESTORE operation in the target nstance. So, it is clear that with the migration of keys from one heavy loaded node to another the response time is significantly reduced.

From the above, it is clear that with the distribution of read requests among nodes, it is no certain that you will have a load distirbution (balancing) in Redis because the results show that:

  • factors such as key popularity and value volume seem to have a minor effect

  • key distribution and key length are contributing factors to the load balancing problem and impact the cluster’s RTs

  • RT can be reduced by migrating the keys from one node to another, in such way that each node in the cluster has the same number of keys

Thus, you need to create a mechanism that combines all these factors. You can start with an algorithm that distirbutes the keys among the nodes.

The steps of the algorithm are as follows:

  1. Find the crowd of masters

  2. Obtain the total number of keys they hold

  3. For each master node store the hostname, port and number of keys

  4. Calculate the keys that each master must hold so that the distribution of keys to be balanced (total keys of cluster / number of masters)

  5. Find which master nodes must take or give keys and the total amount of keys that they must give/take

  6. Characterize masters as source or target nodes depending on whether they are receiving or giving away keys respectively

  7. Start migrating from source node to target nodes, first the hashslots and then the relevant keys and iterate until all the masters have the same amount of keys

And then you can create the next algorihms in order to create a mechanism that provides LB in order to minimize the RT.

Note: further information i will add in half a month in my paper as well as a link to Github with my algorithm implementation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download