Database Reference
In-Depth Information
Creating a keyspace
You might wonder why creating a keyspace is discussed in a chapter that is oriented more
toward system administration tasks. The reason we do this is that keyspace creation is hard
linked with the way you have set the snitch.
Unless you are using
SimpleSnitch
, you should use
NetworkTopologyStrategy
as the replica placement strategy. It needs to know the replication factor for the keyspace
for each data center.
Therefore, if you have
PropertyFileSnitch
or
GossipingPropertyFileSn-
itch
, your keyspace creation looks like the following code snippet:
CREATE KEYSPACE myks
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'DC1' : 2
};
Here,
DC1
is the data center name as defined in snitch configuration files and the value is
the replication factor in each data center.
In EC2-related snitches, such as
EC2MultiRegionSnitch
or
Ec2Snitch
, the data
center name is nothing but the name of the region as it appears in the availability zone.
Therefore, for us-east-1a, the data center is
us-east
. The code to create a keyspace (for
EC2MultiRegionSnitch
) is as follows:
CREATE KEYSPACE myks
WITH REPLICATION = {
'class' : 'NetworkTopologyStrategy',
'us-east':2,
'us-west': 1
};
Thus, if you have set the replication factor smartly, and your queries make use of the right
consistency level, your request will never have to travel beyond the one data center (if all
the replicas in that data center are up).