Hello! Welcome to your next lesson in our module on Distributed Data Storage & Systems.
In the previous lesson, we explored document databases like MongoDB. We saw how their flexible, JSON-like document model and application-driven schema design are powerful for a wide range of use cases. We also touched upon how they scale using sharding.
Today, we shift our focus to another major category of NoSQL databases: column-family stores. Our goal is to describe the architecture and query patterns for column-family databases, with Apache Cassandra as our primary example.
Coming from a background in software and enterprise architecture, you'll find Cassandra's design philosophy fascinating. While MongoDB prioritizes flexibility, Cassandra is engineered for extreme write performance, scalability, and fault tolerance, often at the cost of query flexibility. Understanding this trade-off is fundamental to choosing the right tool for the job.
From Documents to Wide Columns
Unlike relational databases with their rigid schemas, or document databases with their nested objects, column-family stores organize data into what can be visualized as a "map of maps."

Here's the basic hierarchy:
- A Keyspace is like a database schema in the relational world.
- A Table (historically called a Column Family) resides within a keyspace.
- A Row is a container for data, identified by a unique Partition Key.
- Within a row, data is stored in Columns, which are simple name-value pairs.
The key difference is that each row within the same table can have a different set of columns. This is why they are often called "wide-column" stores. You don't pre-define all possible columns; you add them as needed.

This structure is highly optimized for workloads where you have massive amounts of data, particularly time-series data, event logs, or messaging systems—the exact problem Facebook designed it to solve.
The Cassandra Architecture: Scalability and Availability by Design
Cassandra's architecture is built on a few core principles that enable it to scale to petabytes of data across thousands of servers without a single point of failure.
Let's watch a video that provides an excellent overview of Cassandra's origins, data model, and the core architectural pillars that make it so robust.
Cassandra Deep Dive w/ a Ex-Meta Staff Engineer
The video "Cassandra Deep Dive" provides a concise and clear explanation of Cassandra's foundational concepts. It was created with an audience of software engineers in mind, so the level of detail should be a good fit.
Please watch from the beginning to 13:09. This covers the 'Why', the basic data model, and the three pillars of Cassandra's architecture: scalability, availability, and fault tolerance. Pay close attention to these concepts: Partition Key: How it determines where data lives. Consistent Hashing: The mechanism for distributing data across the cluster ring. Replication: How data is copied for fault tolerance. Leaderless Design & Gossip: How nodes communicate without a master.
Let's recap the architectural pillars from the video:
-
Scalability (Horizontal Scaling):
- Data is distributed across nodes using a partition key. All data with the same partition key is guaranteed to live on the same set of nodes.
- Consistent Hashing is used to map each partition key to a token on a virtual ring. Each node in the cluster is responsible for one or more ranges of these tokens. This allows nodes to be added or removed with minimal data reshuffling.
-
Availability & Fault Tolerance:
- Replication ensures that multiple copies (replicas) of your data exist on different nodes. The number of copies is defined by the Replication Factor.
- The architecture is leaderless (or peer-to-peer). There is no "master" node. Any node can coordinate a read or write request, eliminating single points of failure.
- The Gossip Protocol allows nodes to efficiently share state information about the cluster (which nodes are up/down, what token ranges they own) without a central coordinator.
Tunable Consistency and the Write Path
In our previous lessons, we discussed the CAP theorem. Cassandra is a classic AP (Available and Partition-Tolerant) system. It prioritizes keeping the system online for reads and writes, even if it means some replicas might be temporarily out of sync.
However, Cassandra's most powerful feature is tunable consistency. You, the developer, can decide the consistency level for each query.
Let's continue with the same video to understand this and see how Cassandra achieves its famously fast writes.
Cassandra Deep Dive w/ a Ex-Meta Staff Engineer
The next segment of the "Cassandra Deep Dive" video explains tunable consistency and the mechanics of the write and read paths.
Please watch from 13:09 to 24:14. This section covers: Tunable Consistency: The difference between ONE, QUORUM, and ALL consistency levels. Write Path: The roles of the Commit Log, Memtable, and SSTable. Read Path: How Cassandra assembles data from memory and disk.
The key takeaways on consistency and writes are:
- Tunable Consistency lets you balance performance and correctness. A write with
QUORUMconsistency requires a majority of replicas to respond, offering a good balance. A write withONEis faster but less consistent. - Writes are extremely fast because they involve two simple steps on the receiving node:
- An append to a log on disk (Commit Log) for durability.
- A write to an in-memory structure (Memtable).
- Data is later flushed from the Memtable to immutable files on disk called SSTables. Updates and deletes don't modify existing files; they create new entries in new SSTables with a more recent timestamp. A background compaction process merges these files and cleans up old data. This is a classic Log-Structured Merge-Tree (LSM-Tree) design, optimized for high write throughput.
Query-Driven Data Modeling: The Golden Rule of Cassandra
This is the most critical concept to internalize as a data architect. In the relational world, you normalize data to create a single source of truth. In Cassandra, you do the opposite: you design your tables to answer your application's queries.
This means you must know your queries before you design your tables. The general rule is one query per table.
This almost always requires denormalization and data duplication. If you need to look up data in two different ways, you create two tables, duplicating the data in each, with each table optimized for one of the queries. In Cassandra, storage is cheap, but slow queries (or queries that are impossible to execute) are fatal.
This next video provides an excellent, practical walkthrough of this modeling process.
#2 Data Modelling in Apache Cassandra™
The video "Data Modelling in Apache Cassandra™" by DataStax, the company behind Cassandra, provides a step-by-step guide to query-driven design.
Please watch from 53:30 to 01:09:53. The first part (until 01:02:05) contrasts relational modeling (normalization) with NoSQL's workflow-centric modeling (denormalization). The second part demonstrates a real-world example of building a data model for a video commenting system.
The key steps in Cassandra data modeling are:
- Identify Application Workflows: What does the user need to do? (e.g., "View all comments for a video," "View all comments made by a user").
- Define Queries: Translate those workflows into specific queries.
- Design Tables for Queries: Create a separate table for each query. The table's Primary Key is designed to make that one query efficient.
The Primary Key is composed of:
- Partition Key: This is what Cassandra hashes to find the right node. Your query's
WHEREclause must include the full partition key. - Clustering Columns: These columns order the data within a partition on disk. This allows for efficient range queries (e.g.,
WHERE partition_key = 'X' AND clustering_column > 'Y').
For the video comment example:
- Query 1:
SELECT * FROM comments_by_video WHERE video_id = ?- Table:
comments_by_video - Primary Key:
(video_id, comment_timestamp)wherevideo_idis the partition key andcomment_timestampis a clustering column to sort comments chronologically.
- Table:
- Query 2:
SELECT * FROM comments_by_user WHERE user_id = ?- Table:
comments_by_user - Primary Key:
(user_id, comment_timestamp)whereuser_idis the partition key.
- Table:
Notice we have two tables with duplicated data, each perfectly designed for one access pattern.
Test your understanding!
You are designing a data model for an IoT system that collects temperature readings every minute from thousands of sensors worldwide. Your primary query is to retrieve all readings for a specific sensor for a given day.
How would you define the PRIMARY KEY for your readings table? What would be the partition key and what would be the clustering column(s)? Justify your choice.
Show answer
A naive approach might be to use (sensor_id) as the partition key. However, a single sensor would generate 1,440 readings per day, and this partition would grow forever, eventually becoming too large for Cassandra to manage efficiently (an "unbounded partition").
A better approach is to use bucketing. We can create a composite partition key that includes both the sensor and a time bucket.
- Table:
sensor_readings - Primary Key:
((sensor_id, reading_date), reading_timestamp)
Let's break this down:
- Partition Key:
(sensor_id, reading_date). The double parentheses tell Cassandra to treat this composite as a single partition key. This means all data for a specific sensor on a specific day will be co-located on the same node. This perfectly matches our query pattern. The partition is now bounded (it will only ever contain one day's worth of data). - Clustering Column:
reading_timestamp. This will sort the readings chronologically within the partition, allowing us to easily retrieve them in order or query for a specific time range within that day.
The corresponding CQL query would be:SELECT * FROM sensor_readings WHERE sensor_id = 'abc-123' AND reading_date = '2023-10-27';
Conclusion
We've covered a lot of ground on Cassandra's architecture and query-driven design. It represents a significant departure from the relational and document models, purpose-built for specific, demanding workloads.
Key Takeaways:
- Cassandra is a leaderless, peer-to-peer distributed database optimized for high write throughput, availability, and linear scalability.
- Its wide-column data model allows for schema flexibility at the row level.
- Data is distributed using a partition key and consistent hashing.
- Data modeling is query-driven, requiring denormalization and data duplication to create tables that match specific access patterns.
- The
PRIMARY KEY(composed of a partition key and clustering columns) is the most critical element of table design. - Tunable consistency allows you to make per-query trade-offs between performance and data correctness.
In our next lesson, we'll explore another type of NoSQL system: key-value stores, using Redis as our example. We will see how their in-memory nature makes them ideal for use cases like caching and session management, which are very different from Cassandra's focus on durable, large-scale data persistence.