Alter a keyspace

Either the strategy class or replication factor of a keyspace can be modified.

To change a simple replication factor, either increase or decrease the value to change the total number of copies of keyspace data stored in a cluster. Changing the replication factor of a keyspace impacts each node that the keyspace replicates to (or no longer replicates to), and data will require time to redistribute.

The most common replication strategy modification is to switch the strategy class from SimpleStrategy to NetworkTopologyStrategy (NTS). When the NetworkTopologyStrategy class is set, the replication_factor must be modified to specify one or more datacenters.

Altering the distribution of nodes within multiple datacenters when data is present should be accomplished by adding a datacenter. Then add data to the new nodes in the new datacenter and remove nodes from the old datacenter.

Follow this procedure to prepare all affected nodes for this change.

You cannot insert data into a table in a keyspace that uses NetworkTopologyStrategy unless you define the datacenter names in the snitch properties file or you use a single datacenter that is named when the keyspace was created.

See Data replication for more information.

Prerequisites

  • A keyspace must exist.

  • A full repair must be run on the keyspace if you add datacenters or reduce the replication factor.

Change simple replication_factor value

Use the ALTER KEYSPACE CQL command to change the value of a keyspace using the SimpleStrategy class:

  ALTER KEYSPACE cycling
  WITH REPLICATION = {
      'class' : 'SimpleStrategy', 
      'replication_factor' : 2, 
  };

Now, a datacenter must be named, and not just a replication factor.

Change the replication strategy and the replication_factor values

Update a keyspace in the cluster and change its replication strategy options to either increase or decrease the replication factor.

  ALTER KEYSPACE cycling
  WITH REPLICATION = {
      'class' : 'NetworkTopologyStrategy', 
      'DC1' : 3, 
      'DC2' : 2
  };

Verify the case of the datacenter name before using this command, as datacenter names are case-sensitive.

Run full repair, if required

Run a full repair of the keyspace on all nodes belonging to the datacenters where the replication factor has been increased. Also, run a full repair of the keyspace in the datacenters that have been added to the replication list.

nodetool repair --full -pr <keyspace_name>

If you decrease the replication factor, run a cleanup of the keyspace on all nodes belonging to the datacenters that had their replication factor decreased. Also, run a cleanup of the keyspace on the datacenters that were removed from the replication list. This action will free up the space held by the now unused replicas of the data.

nodetool cleanup <keyspace_name>

See also:

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com