Chapter 26: Column-Family and Graph Databases

Column-Family: Cassandra

Designed for massive write throughput across many nodes. Used by Netflix, Apple, Discord.

-- Cassandra CQL (looks like SQL but isn't)
CREATE TABLE sensor_data (
    sensor_id UUID,
    timestamp TIMESTAMP,
    value     DOUBLE,
    PRIMARY KEY (sensor_id, timestamp)
) WITH CLUSTERING ORDER BY (timestamp DESC);

-- Writes are FAST (append-only, no read-before-write)
INSERT INTO sensor_data (sensor_id, timestamp, value)
VALUES (uuid(), toTimestamp(now()), 23.5);

Key Properties

Graph Databases: Neo4j

Optimized for traversing relationships. Queries like "friends of friends" that would require recursive JOINs in SQL are trivial.

// Cypher query language (Neo4j)

// Create nodes and relationships
CREATE (alice:Person {name: 'Alice'})-[:WORKS_AT]->(ericsson:Company {name: 'Ericsson'})
CREATE (bob:Person {name: 'Bob'})-[:WORKS_AT]->(ericsson)
CREATE (alice)-[:FRIENDS_WITH]->(bob)

// Find friends of friends
MATCH (me:Person {name: 'Alice'})-[:FRIENDS_WITH*2]->(fof)
RETURN fof.name

// Shortest path between two people
MATCH path = shortestPath(
  (a:Person {name:'Alice'})-[*]-(b:Person {name:'Dave'})
)
RETURN path

Graph DB Use Cases

Key Takeaways