Tuesday, September 25, 2012

Apache Cassandra Failover and load balancing

Apache Cassandra Failover and Load Balancing

I have 3-node cluster, my RF=3, W=ONE and R=QUORUM, once a node is down, another two node can work properly, if the downed node comes back, it also get update.

To solve load balancing, only need to change one line of the codes(from last tutorial) to :
//load balancing build a connection pool
cassandraHostConfigurator = new CassandraHostConfigurator("192.168.0.1:9160,192.168.0.2:9160,192.168.0.3:9160");
Hector will do load balancing internally.

To solve failover problem: please check here.
Be aware of the relationship between Replication factor and Consistency Level, please check here.

W+R > N.  N is replication factor

I'm using cassandra(1.1.5), so I modified
http://ac31004.blogspot.ca/2010/08/consistencylevel-in-hector-and.html's code to:



import me.prettyprint.cassandra.service.OperationType;
import me.prettyprint.hector.api.ConsistencyLevelPolicy;
import me.prettyprint.hector.api.HConsistencyLevel;

// W+R > N .  where N is Replication Factor
public final class MyConsistencyLevel implements ConsistencyLevelPolicy
{
    @Override
    public  HConsistencyLevel get(OperationType op)
    {
       switch (op)
       {
          case READ:
              return HConsistencyLevel.QUORUM;
          case WRITE:
              return HConsistencyLevel.ONE;
          default:
              return HConsistencyLevel.QUORUM;
       }
    }

    @Override
    public HConsistencyLevel get(OperationType op, String cfName)
    {
       return HConsistencyLevel.QUORUM;
    }
}

Add or modify those code in CassandraTest.java(you can find it in last tutorial.)

       ConsistencyLevelPolicy consistencyLevelPolicy = new MyConsistencyLevel();

        Keyspace kpo = HFactory.createKeyspace(keySpace, cluster);
        // set CL
        kpo.setConsistencyLevelPolicy(consistencyLevelPolicy);



1 comment:

  1. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in APACHE CASSANDRA kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on APACHE CASSANDRA We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Saurabh Srivastava
    MaxMunus
    E-mail: saurabh@maxmunus.com
    Skype id: saurabhmaxmunus
    Ph:+91 8553576305 / 080 - 41103383
    http://www.maxmunus.com/

    ReplyDelete