This policy is datacenter aware and routes queries to the local nodes first. There are two constructors for instantiating this class:

  • DCAwareRoundRobinPolicy(String localDc): This specifies the name of the local data center
  • DCAwareRoundRobinPolicy(String localDc, int usedHostsPerRemoteDc): This can configure the number of nodes to try in a remote data center

To use this policy, use the withLoadBalancingPolicy() method to set the LB policy, as follows:

Cluster cluster = Cluster.builder().addContactPoint("") .withLoadBalancingPolicy( DCAwareRoundRobinPolicy.builder() .withLocalDc("localDc") .withUsedHostsPerRemoteDc(2) .allowRemoteDCsForLocalConsistencyLevel() .build()).build();

Get Learning Apache Cassandra - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.