Apache Cassandra Fundamentals
itDatabases and data storage
Apache Cassandra Fundamentals
Apache Cassandra is an open-source, distributed NoSQL database. It stores data across a cluster instead of making one server the permanent center of every operation.
That design serves a specific need. Some applications must keep accepting reads and writes as data volume grows or individual machines fail. Cassandra distributes partitions, keeps replicas, and lets any suitable node coordinate a request.
Cassandra is not a general replacement for a relational database. It favors predictable, partition-oriented access over joins and cross-partition transactions. You get the most value when you know the application's queries before you design its tables.
The core mental model
Think in three layers:
- The data model turns an application query into a table and primary key.
- The cluster maps each partition to a token range and stores replicas on several nodes.
- The storage engine records each write in a commit log and memtable, then flushes immutable SSTables to disk.
A client can contact any node. That node becomes the coordinator for the request. The coordinator finds the replicas for the partition, sends the request, and waits for enough replica responses to satisfy the chosen consistency level.
No node owns a special primary-server role for the whole database. Replicas can accept mutations for the token ranges they hold. This multi-primary structure helps Cassandra continue operating through node failures.
From keyspace to row
A keyspace contains tables and defines replication settings. Its replication strategy determines where Cassandra places replicas. Its replication factor states how many copies Cassandra should keep.
A table contains rows and typed columns. The table's primary key has two jobs:
- The partition key decides which partition holds the row and therefore which replicas store it.
- Optional clustering columns identify and order rows inside that partition.
This distinction is fundamental. A partition is the unit Cassandra distributes through the cluster. Rows within a partition are stored in clustering order.
Suppose an application reads events for one device over time. A suitable primary key might partition by device and day, then cluster by event time. That shape lets one query reach a bounded partition and scan rows in useful order.
An unbounded partition key would let one busy device accumulate too much data in one partition. A random key per event would spread writes but make the time-range query scatter across partitions. Good modeling balances distribution with the exact read path.
Query-first modeling
Relational design usually starts with entities and normalization. Queries join related tables later. Cassandra takes the opposite route: define the application queries first, then create tables that answer them directly.
This often means storing the same business fact in more than one table. Each table serves a specific access pattern. The duplication is deliberate denormalization, not an accidental copy.
A strong Cassandra query usually supplies the partition key and reads one table. Queries that scan many partitions add coordinator work, network traffic, and latency. Cassandra does not provide distributed joins, foreign keys, referential integrity, or general cross-partition transactions.
Use Cassandra Query Language, or CQL, to define keyspaces and tables and to read and write data. CQL resembles SQL, but the familiar syntax does not create relational behavior. The primary key still controls distribution and local row order.
Replication and consistency
Cassandra hashes a partition key into a token. Token ranges map partitions to nodes. Virtual nodes let one physical node own several token ranges, which helps distribute data and load when nodes join or leave.
The replication strategy selects replica nodes for each token range. Apache recommends NetworkTopologyStrategy for production. It sets a replication factor per datacenter and can place replicas across racks to reduce correlated-failure risk.
The consistency level controls how many replica responses a coordinator needs for one operation. A smaller requirement can preserve availability when replicas are unreachable. A larger requirement asks more replicas to participate before success is returned.
This is tunable consistency, not one permanent cluster-wide setting. Applications choose an appropriate level for each operation. You must reason about both the replication factor and the read and write consistency levels.
Ordinary writes are eventually consistent across replicas. Cassandra also offers lightweight transactions for single-partition compare-and-set work when linearizable consistency is required. That stronger coordination costs more than a normal read or write.
The write and read paths
Each replica first appends a mutation to its commit log for recovery. It also applies the mutation to an in-memory memtable. When the memtable fills or is flushed, Cassandra writes an immutable SSTable to disk.
Updates do not edit an SSTable in place. New values can exist in newer SSTables while older values remain in older files. Reads may consult memtables and several SSTables, then reconcile the relevant versions.
Compaction merges SSTables and processes superseded values and deletions. It is necessary background work, but it consumes disk and input/output capacity. Cassandra's write-friendly storage path therefore creates read and write-amplification tradeoffs.
A delete writes a tombstone rather than immediately erasing every copy. Tombstones let replicas learn that old data was deleted. They are removed only when Cassandra can do so safely through compaction and the configured grace period.
Failure does not remove operations
Replication keeps data available during many node failures, but replicas can diverge. Hints may help a recovered replica receive missed writes. Hints are not a complete substitute for repair.
Repair compares replica data for shared token ranges and streams differences. Operators must schedule and monitor repair so missed writes are reconciled before old tombstones can expire. Backups, monitoring, capacity planning, security, and tested recovery remain necessary too.
Adding nodes can increase capacity and spread token ranges. It does not correct a poor partition key, remove hot partitions, or make unbounded partitions safe. Schema design and operations remain coupled.
Where Cassandra fits
Cassandra is a strong candidate when an application needs several of these properties:
- High write throughput across a growing dataset
- Continued service during node or site failures
- Replication across racks or datacenters
- Predictable queries based on known partition keys
- Horizontal growth by adding nodes
Examples include time-series events, device telemetry, activity feeds, and other workloads with stable access patterns and large write volumes. The exact fit depends on partition size, traffic distribution, latency goals, failure domains, and operational skill.
Choose another system when the main workload needs ad hoc joins, foreign-key enforcement, broad multi-row transactions, or flexible queries that cannot be designed in advance. A relational database may be clearer for transaction-heavy business data. An analytical engine may be better for large exploratory scans.
A practical learning path
Start by tracing one query from its partition key to replicas. Then model a bounded partition and learn how CQL primary keys express that model. Next, study consistency levels with the replication factor. After that, follow writes through the commit log, memtable, SSTables, and compaction.
Before operating Cassandra in production, learn topology, capacity, monitoring, repair, backup, security, and safe node lifecycle procedures. Test failures and recovery with realistic data and traffic. Cassandra rewards designs that make distribution explicit.
