Overview
When preparing to send a request to ScyllaDB/Cassandra, the load balancing policy constructs a plan of targets (node + optional shard) to contact. The first elements in the plan are the preferred targets (e.g., those with lowest latency or those that are replicas for the data).LoadBalancingPolicy Trait
Any type implementing theLoadBalancingPolicy trait can be used:
Key Methods
- pick(): Returns the first (best) node to contact for a request
- fallback(): Returns the remaining nodes to try if the first fails
- on_request_success(): Called after each successful request
- on_request_failure(): Called after each failed request
DefaultPolicy
The driver providesDefaultPolicy, a sophisticated policy with multiple features:
Features
- Token awareness: Routes requests to replicas that own the data
- Datacenter awareness: Prefers nodes in a specified datacenter
- Rack awareness: Can prefer nodes in a specific rack within a datacenter
- Datacenter failover: Optionally allows requests to remote datacenters
- LWT optimization: Routes LWT queries deterministically to avoid Paxos conflicts
Basic Configuration
Datacenter Awareness
Prefer nodes in a specific datacenter:- Nodes in that datacenter are treated as “local”
- Other nodes are treated as “remote”
- By default, remote nodes are excluded from plans
Rack Awareness
Prefer nodes in a specific rack within a datacenter:Token Awareness
Token awareness routes requests to replicas that own the data:- Reduced latency by avoiding coordinator hops
- Better load distribution
- Improved throughput
Datacenter Failover
Allow requests to remote datacenters when local nodes are unavailable:Latency Awareness
Latency awareness penalizes slow nodes (use with caution):Replica Shuffling
Control whether replicas are shuffled:Using with Execution Profiles
Attach a load balancing policy via execution profile:Custom Load Balancing Policy
Implement theLoadBalancingPolicy trait for custom behavior:
Routing Information
The policy receives routing information about the statement:Best Practices
- Enable token awareness for better performance
- Set preferred datacenter to match your application’s location
- Enable datacenter failover for better availability
- Avoid latency awareness unless benchmarks prove it beneficial
- Use rack awareness when deploying across availability zones
Next Steps
- Retry Policy - Control retry behavior
- Execution Profiles - Group execution options
