Skip to content

Cassandra Wide-Row Modeling ​

Query-First Design ​

CRITICAL: Design tables for specific queries (not entities).

Example: User events

Access pattern 1: Get all events for user (sorted by time descending)

sql
-- cqlsh (Cassandra 5.0); chạy sau `USE <keyspace>;`
CREATE TABLE events_by_user (
    user_id UUID,
    event_time TIMESTAMP,
    event_type TEXT,
    event_data TEXT,
    PRIMARY KEY ((user_id), event_time)
) WITH CLUSTERING ORDER BY (event_time DESC);

INSERT INTO events_by_user (user_id, event_time, event_type, event_data)
VALUES (123e4567-e89b-12d3-a456-426614174000, '2025-01-09 10:00:00', 'login', '{}');

-- Query
SELECT * FROM events_by_user WHERE user_id = 123e4567-e89b-12d3-a456-426614174000;

Access pattern 2: Get all events of a type (sorted by time)

sql
-- cqlsh (Cassandra 5.0); chạy sau khối tạo events_by_user
-- Cần bảng riêng, ứng dụng ghi cả hai bảng (BATCH). Partition = loại sự kiện + ngày để không phình vô hạn
-- (materialized view mặc định tắt từ Cassandra 4.0 vì experimental; ở 5.0 xét thêm SAI index)
CREATE TABLE events_by_type_day (
    event_type TEXT,
    day DATE,
    event_time TIMESTAMP,
    user_id UUID,
    event_data TEXT,
    PRIMARY KEY ((event_type, day), event_time, user_id)
) WITH CLUSTERING ORDER BY (event_time DESC, user_id ASC);

INSERT INTO events_by_type_day (event_type, day, event_time, user_id, event_data)
VALUES ('login', '2025-01-09', '2025-01-09 10:00:00', 123e4567-e89b-12d3-a456-426614174000, '{}');

-- Query
SELECT * FROM events_by_type_day WHERE event_type = 'login' AND day = '2025-01-09';

Bucketing Pattern (Avoid Large Partitions) ​

Problem: Sensor sends data forever → partition grows unbounded

Bad:

sql
-- minh hoạ, không chạy độc lập
PRIMARY KEY ((sensor_id), timestamp)
-- Partition = một sensor, không có chặn trên: lớn mãi theo thời gian
-- (ngưỡng "quá lớn" phải đo bằng nodetool tablehistograms; con số ~100 MB hay gặp là ví dụ minh hoạ)

Good: Bucket by time

sql
-- minh hoạ, không chạy độc lập
PRIMARY KEY ((sensor_id, date), timestamp)
-- Partition = sensor + day (bounded)
-- New partition every day

Example:

sql
-- cqlsh (Cassandra 5.0); chạy sau `USE <keyspace>;`
CREATE TABLE sensor_readings (
    sensor_id UUID,
    date DATE,
    timestamp TIMESTAMP,
    temperature DECIMAL,
    PRIMARY KEY ((sensor_id, date), timestamp)
) WITH CLUSTERING ORDER BY (timestamp DESC);

INSERT INTO sensor_readings (sensor_id, date, timestamp, temperature)
VALUES (123e4567-e89b-12d3-a456-426614174000, '2025-01-09', '2025-01-09 10:30:00', 25.5);

-- Query: Get readings for sensor on 2025-01-09
SELECT * FROM sensor_readings
WHERE sensor_id = 123e4567-e89b-12d3-a456-426614174000 AND date = '2025-01-09';

-- Query: Get recent readings (last hour)
SELECT * FROM sensor_readings
WHERE sensor_id = 123e4567-e89b-12d3-a456-426614174000
  AND date = '2025-01-09'
  AND timestamp > '2025-01-09 10:00:00';

Phải nhớ:

  • Thiết kế bảng theo truy vấn: mỗi truy vấn đọc một partition; truy vấn thứ hai cần bảng thứ hai do ứng dụng ghi (hoặc SAI index), không dựa vào materialized view mặc định tắt.
  • Partition key phải có chặn trên: thêm bucket như (sensor_id, date); chọn cỡ bucket bằng cách đo partition thật với nodetool tablehistograms.
  • Clustering key quyết định thứ tự và tính duy nhất trong partition: thiếu clustering thì mỗi lần ghi đè cùng một hàng.