“The ALTER KEYSPACE command allows you to modify various properties of an existing Cassandra keyspace. For example, you can use this command to change the replication strategy and number of replicas and enable or disable durable writes.”
In this post, we will walk you through how to use the ALTER KEYSPACE command to modify various properties of a Cassandra keyspace.
Let’s dive in.
Command Syntax
The ALTER KEYSPACE command follows a simple syntax as shown in the code snippet below:
Let’s take practical examples to illustrate how to use this command.
Create Sample Keyspaces
Before illustrating how to use the ALTER KEYSPACE command, let us set up sample keyspaces for illustration.
In this example, we will create two types of keyspaces. The first will use the SimpleStrategy replication strategy, and the second will use NetworkTopologyStrategy.
SimpleStrategy Keyspace
We can create a keyspace using the SimpleStrategy as shown in the code below:
... WITH REPLICATION = {
... 'class': 'SimpleStrategy',
... 'replication_factor': 1
... };
The statement above creates a keyspace with the name “simple” using the SimpleStrategy replication method and a replication factor of 1.
NetworkTopologyStrategy Keyspace
The example below creates a keyspace using the Network Topology Strategy.
... WITH REPLICATION = {
... 'class': 'NetworkTopologyStrategy',
... 'datacenter1': 1
... };
The query above creates a keyspace called production using the Network Topology Strategy.
Alter Keyspace – Change Replication Factor
To illustrate how to update the keyspace’s replication factor, we can use the keyspace “simple” we created earlier.
The command syntax is as shown:
WITH replication = {'class': ‘SimpleStrategy, 'replication_factor' : <value>};
For example:
... WITH REPLICATION = {
... 'class': 'SimpleStrategy',
... 'replication_factor': 2
... };
In this example, we increase the number of replicas from 1 to 2.
Keep in mind that increasing the number of replicas may require you to perform a full repair to redistribute the data.
Alter Keyspace – Change Replication Strategy
We can also alter the keyspace to change the replication strategy. For example, we can change the “simple” keyspace strategy to NetworkTopologyStrategy.
The command syntax is as shown:
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter_name' : N };
Example
... WITH REPLICATION = {
... 'class' : 'NetworkTopologyStrategy',
... 'datacenter1': 3};
In this case, we change the replication strategy of the “simple” keyspace to NetworkTopologyStrategy.
Alter Keyspace – Enable or Disable Durable Writes
We can also enable or disable durable writes for an existing keyspace using the ALTER KEYSPACE command.
The command syntax is as shown:
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'datacenter_name' : 3N}
AND DURABLE_WRITES = false/true ;
For example, to allow bypass the commit log for the “production” keyspace, we can run:
... WITH REPLICATION = {
... 'class' : 'NetworkTopologyStrategy',
... 'datacenter1': 3}
... AND DURABLE_WRITES = false;
The command above alters the “production” keyspace and disables durable writes.
Conclusion
In this article, you learned how to use the ALTER KEYSPACE command to alter various properties of an existing keyspace in a Cassandra cluster.