sewenew / redis-plus-plus

Redis client written in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

about the connection load balancing

1261385937 opened this issue · comments

hi ,
I found the load balancing of connections to slave is std::shuffle , this may be cause all connections to just one slave node if has a small number of slaves.
Actually, it happeds to me. I use sentinel mode , one master and two slaves. Master has 5 connections , slave 1 has 10 connections and slave 2 has no connections . slave 1 node cpu cost is high , but slave 2 node cpu is so free.

it is better if use round robin for valid slave nodes ?

@1261385937 The current strategy, I.e. randomly pick one slave and only one, is based on the following reasons:

  • keep data consistency: since the synchronization from master is asynchronous, multiple slaves might different copies of data. If we use round robine way to get data from different slaves, the data inconsistencies problem might be worse, I.e. first call to get a key, return the value, second call returns nil, third call returns a value again.
  • avoid too many connections: if we use round robin way, we need to keep a connection pool for each slave. So there will be N * M * S connections (N is the number of client, M is the number of slaves and S is the size of the connection pool).
  • Normally, there’re multiple clients (on different hosts), and each client connects to a random slave. So there won’t be a load balance problem.

However, in your case, I think you only have a single client on a single host, so only one slave has connections. You can solve the problem by creating multiple Redis objects, and randomly pick one to send requests.

There was a plan to have customized strategy. However, it needs too many changes, so the priority of this feature is low. If there’s some progress on it, I’ll let you know.

Regards

thanks, i will take your suggest