| name | cassandra |
| description | Apache Cassandra is a distributed NoSQL database designed for high availability and linear scalability.
Learn CQL (Cassandra Query Language), data modeling with partition keys, replication strategies,
and integration with Node.js using the DataStax driver.
|
| compatibility | macos, linux |
| metadata | {"author":"terminal-skills","version":"1.0.0","category":"data","tags":["cassandra","nosql","distributed-database","cql","nodejs"],"source":{"repository":"https://github.com/TerminalSkills/skills","path":"skills/cassandra","license_path":"LICENSE","commit":"77fa0150aa0921f892420d3ec2a9204e3124b71e"}} |
Cassandra
Apache Cassandra is a peer-to-peer distributed database that provides high availability with no single point of failure. Data is distributed across nodes using consistent hashing.
Installation
docker run -d --name cassandra -p 9042:9042 cassandra:4
docker exec -it cassandra cqlsh
npm install cassandra-driver
pip install cassandra-driver
CQL Basics
CREATE KEYSPACE IF NOT EXISTS myapp
WITH replication = {
'class': 'NetworkTopologyStrategy',
'datacenter1': 3
}
AND durable_writes = true;
USE myapp;
Data Modeling
CREATE TABLE users (
email text PRIMARY KEY,
name text,
created_at timestamp
);
CREATE TABLE posts_by_user (
user_id uuid,
created_at timestamp,
post_id uuid,
title text,
body text,
PRIMARY KEY (user_id, created_at)
) WITH CLUSTERING ORDER BY (created_at DESC);
CREATE TABLE sensor_readings (
sensor_id text,
day text,
reading_time timestamp,
value double,
PRIMARY KEY ((sensor_id, day), reading_time)
) WITH CLUSTERING ORDER BY (reading_time DESC);
CRUD Operations
INSERT INTO users (email, name, created_at)
VALUES ('alice@example.com', 'Alice', toTimestamp(now()));
SELECT * FROM users WHERE email = 'alice@example.com';
SELECT * FROM posts_by_user
WHERE user_id = 550e8400-e29b-41d4-a716-446655440000
AND created_at > '2026-01-01'
LIMIT 20;
UPDATE users SET name = 'Alice Smith' WHERE email = 'alice@example.com';
DELETE FROM users WHERE email = 'alice@example.com';
BEGIN BATCH
INSERT INTO posts_by_user (user_id, created_at, post_id, title) VALUES (?, ?, ?, ?);
UPDATE user_stats SET post_count = post_count + 1 WHERE user_id = ?;
APPLY BATCH;
Node.js Driver
const { Client, types } = require('cassandra-driver');
const client = new Client({
contactPoints: ['localhost'],
localDataCenter: 'datacenter1',
keyspace: 'myapp',
queryOptions: { consistency: types.consistencies.localQuorum },
});
async function main() {
await client.connect();
await client.execute(
'INSERT INTO users (email, name, created_at) VALUES (?, ?, ?)',
['bob@example.com', 'Bob', new Date()],
{ prepare: true }
);
const result = await client.execute(
'SELECT * FROM users WHERE email = ?',
['bob@example.com'],
{ prepare: true }
);
console.log(result.rows[0]);
const query = 'SELECT * FROM posts_by_user WHERE user_id = ?';
for await (const row of client.stream(query, [userId], { prepare: true })) {
console.log(row.title);
}
await client.shutdown();
}
main().catch(console.error);
Python Driver
from cassandra.cluster import Cluster
from cassandra.query import SimpleStatement, ConsistencyLevel
cluster = Cluster(['localhost'])
session = cluster.connect('myapp')
session.execute(
"INSERT INTO users (email, name, created_at) VALUES (%s, %s, toTimestamp(now()))",
('alice@example.com', 'Alice')
)
stmt = SimpleStatement(
"SELECT * FROM users WHERE email = %s",
consistency_level=ConsistencyLevel.LOCAL_QUORUM
)
row = session.execute(stmt, ('alice@example.com',)).one()
print(row.name)
cluster.shutdown()
Replication and Consistency
Consistency Levels:
- ONE: Fast, low consistency. Good for logs/metrics.
- QUORUM: Majority of replicas. Balanced read/write.
- LOCAL_QUORUM: Majority in local datacenter. Best for multi-DC.
- ALL: All replicas must respond. Slowest, strongest consistency.
Rule of thumb: Write CL + Read CL > Replication Factor = strong consistency
Example: RF=3, Write=QUORUM(2), Read=QUORUM(2) → 2+2 > 3 ✓
Operations
docker exec cassandra nodetool status
docker exec cassandra nodetool ring
docker exec cassandra nodetool repair myapp
docker exec cassandra nodetool compact myapp posts_by_user
docker exec cassandra nodetool snapshot myapp -t backup_20260219