Keyspaces set replication for everything inside them, and getting it wrong is painful to fix later.
cqlsh is the interactive shell for CQL (Cassandra Query Language). Run cqlsh host 9042 (9042 is the native protocol port). Useful commands: DESCRIBE KEYSPACES, DESCRIBE TABLE t, USE ks;, CONSISTENCY QUORUM;, TRACING ON;, EXPAND ON; for vertical output.
A keyspace is the top-level namespace, like a database or schema in SQL, and it owns the replication settings for all of its tables:
NetworkTopologyStrategywith a replication factor per data center, e.g.{'class': 'NetworkTopologyStrategy', 'dc1': 3, 'dc2': 3}. Use it for anything real, even with one data center, so adding a second later is easy,SimpleStrategywith onereplication_factor, only for local development.
A replication factor of 3 per data center is the norm: with QUORUM reads and writes (2 of 3) you can lose one replica and still be consistent and available.
durable_writes (default true) controls whether writes go to the commit log; leave it on.
Changing replication with ALTER KEYSPACE is allowed, but existing data isn't moved automatically; you must run a repair (or rebuild for a new data center) afterwards. Data center names must match what the nodes report via the snitch (see nodetool status).
Example
-- $ cqlsh 127.0.0.1 9042
CREATE KEYSPACE IF NOT EXISTS shop
WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': 3}
AND durable_writes = true;
USE shop;
DESCRIBE KEYSPACES; -- shop system system_auth system_schema ...
-- dev-only single node
CREATE KEYSPACE IF NOT EXISTS sandbox
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
-- later: add a second data center, then run nodetool rebuild on its nodes
ALTER KEYSPACE shop
WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': 3, 'dc2': 3};Remember
- cqlsh speaks CQL on port 9042
- A keyspace defines replication for all its tables
- Use NetworkTopologyStrategy with RF 3 per data center
- After changing replication, repair or rebuild to move data
Watch out for
- Using SimpleStrategy in production and struggling when a second data center is added.