Balancing
Balancers
The Balancer is a TarantoolClient component designed to distribute requests
between Tarantool nodes.
Concept
The balancer abstraction in Tarantool Java EE is represented by the TarantoolBalancer interface.
Let's look at how balancing works in the Java client:
- The programmer executes a request through the API of high-level clients (
TarantoolClient,TarantoolCrudClientorTarantoolBoxClient) - The high-level client requests a connection to execute the request from
TarantoolBalancer - The balancer, using its logic, requests an available node from the connection pool (
IProtoClientPool) Node availability is determined using the heartbeat mechanism - The connection pool returns an available connection to the balancer
- The balancer provides the high-level client with a connection
- The high-level client executes the request through the provided connection
- The high-level client returns a
CompletableFuture<?>with the result to the calling code
Important
The actual connection to the node via the selected connection occurs upon the first selection.
If the connection is unavailable, the balancer will attempt to select the next one from the available. If there is
no available connection, the client will throw a NoAvailableClientsException.
Available Default Implementations
By default, the following types of balancers are available:
TarantoolRoundRobinBalancer
This implementation of the TarantoolBalancer interface performs connection selection according to the following
algorithm:
- Selection of the group of connections with which the balancing starts (the order is not deterministic).
- Connection selection within the group is performed in order from first to last.
- After the last connection in the current group, the balancer moves to connections belonging to another group.
TarantoolDistributingRoundRobinBalancer
This implementation of the TarantoolBalancer interface performs connection selection according to the following
algorithm:
- Selection of the group of connections with which the balancing starts (the order is not deterministic).
- Selection of the first connection in the first group.
- Moving to the next group and selecting the first connection.
- Repeat step 3, selecting the next connection in the group.
Client Balancing Configuration
To configure balancing in the client, use the TarantoolFactory API, for example:
final TarantoolCrudClient crudClient = TarantoolFactory.crud()
.withBalancerClass(TarantoolRoundRobinBalancer.class)
//... other settings
.build();
Custom Balancers
To use your own balancer, create a class that implements the
TarantoolBalancer interface.
Important
The custom class implementing TarantoolBalancer must have a default constructor
and a constructor with the signature public <constructorName>(IProtoClientPool pool) {...}.