Load balancing with Cassandra drivers
Cassandra drivers use load balancing policies to control the distribution of requests across a cluster. For a given query execution, the load balancing policy determines the node that coordinates the query execution and the nodes that can be used as failover hosts, if any.
The load balancing policy can be supplied in execution profiles or the driver’s root object (cluster) configuration.
Drivers offer built-in and custom load balancing policies. For Astra, only use your driver’s default load balancing policy.
Coordinator selection in load balancing policies
Drivers create and maintain connection pools for nodes that are selected by the load balancing policy.
Each time a query is executed, the load balancing policy returns a query plan that determines which hosts are eligible to receive the query. The driver uses the first host on the list to execute the request, leaving the successive hosts for retry and speculative execution.
Token awareness in load balancing policies
A token-aware load balancing policy routes requests, by priority, to the replicas that own the data being queried.
Specifically, token awareness retrieves replica nodes based on the primary key information for a given query and parameters. Selecting replicas ensures that the query’s coordinator is also the node that owns the data being written or read. This avoids an extra network connection on the server side.
Token awareness is common to all drivers.
For prepared statement executions, the key is automatically calculated to obtain accurate query routing.
Datacenter awareness in load balancing policies
A datacenter-aware load balancing policy limits requests to a specific datacenter to ensure that the data is returned to the user as efficiently as possible. For example, in a global application, requests from users in North America would be directed to a datacenter in North America. Similarly, requests from users in Europe would be routed to a datacenter in Europe.
The local datacenter and contact points are automatically provided by the database’s Secure Connect Bundle (SCB), and it is implicitly used in each driver’s default load balancing policy.
For Astra-compatible drivers, don’t explicitly set local datacenters, failover datacenters, or contact points in your driver configuration. Only use the default load balancing policy, which uses the contact points and datacenter information provided by the database’s SCB.
Configure load balancing policies
Cassandra drivers offer built-in and custom load balancing policies, and each driver has a default load balancing policy.
Default load balancing policies include token awareness and datacenter awareness. Specifically, the default policy does the following:
-
Retrieves the replicas for a given token.
-
Returns a list of hosts in the local datacenter. Hosts that contain the replicas are listed first, followed by the rest of the nodes in the specified local datacenter.
-
Uses a load-distributing algorithm to fairly distribute the load across the selected replica nodes.
For more information about load balancing policies, see your driver’s documentation.
C/C++ driver load balancing policies
For general load balancing policy information, see Load balancing configuration.
Go driver load balancing policies
Specify the load balancing policy in PoolConfig.HostSelectionPolicy.
The GoCQL driver’s default host policy is RoundRobinHostPolicy, where each host is tried sequentially for each query.
For more information, see Datacenter awareness and query routing.
Java driver load balancing policies
|
This information is for the Java driver 4.x series. If you are using an earlier version, see the documentation for your version of the Java driver. |
By default, the Java driver prevents cross-datacenter traffic.
For example, applications deployed in AWS us-east-1 only communicate with Cassandra nodes in the same datacenter.
If there are no available Cassandra nodes in the local datacenter, then the driver cannot execute any queries.
For more information, see the documentation for your version of the Java driver:
Node.js driver load balancing policies
See the documentation for your version of the Node.js driver:
Python driver load balancing policies
See the documentation for your version of the Python driver: