Home›Learn›Cassandra
Course · Spread the dataLesson 20 of 330 read

Cassandra

How does Cassandra take so many writes, and why is it so picky about queries?

This page is about a database built for writes spread across many machines, with no primary. I show you where a write goes (commit log, memtable, SSTable), how the partition key decides which machines hold your data, and why deletes and queries the key cannot answer cause most of the trouble. After it you will be able to design a table from its queries, pick a consistency level, and read nodetool's view of the cluster.

Cassandra: in one line

A wide-column database: rows grouped into partitions by a key, partitions spread across a ring of equal nodes, every write appended and never updated in place.

Good at: Huge write rates, data that grows forever, several data centres, no single node that must stay up. Time series, messages, event history.

Runs as: one Java process per node; every node is equal; replication per keyspace, consistency per query

The architecture: a ring of equal nodes

There is no primary and no failover. Any node can take a request and pass it to the nodes that own the data.

The same layers as a still diagram, with every part named:

Cassandra: architecture

Write path on each replica

Processone Java process per node · any node can coordinate a request · the partition key's hash picks the replicas
→
Memorya memtable per table, holding recent writes in sorted order · key cache · a bloom filter per SSTable
→
Diskcommitlog/ segments · data/<keyspace>/<table>/ SSTables (Data.db, an index, Filter.db, Statistics.db) · hints/
Who does what: The coordinator hashes the partition key to a token and sends the request to the replicas that own that token. Each replica appends to the commit log, adds the write to its memtable and acknowledges. Memtables flush to immutable SSTables, and compaction merges SSTables and finally drops deleted data.
Where the data lives

A log, a memtable, and files that never change

PlaceWhat it holdsWhat to watch
commitlog/every write, appended before it is acknowledgedcommitlog_sync is periodic by default, flushing every 10 seconds, so a power cut on every replica at once could lose the last seconds. The batch and group modes wait for the disk.
Memtablerecent writes, sorted, in memoryFlushed to an SSTable when full. Big memtables mean more commit log to replay after a crash.
SSTablesimmutable sorted files; a read may have to look in severalSSTables per read in nodetool tablehistograms. Compaction keeps the count down.
Tombstonesmarkers for deleted data, kept until compaction after gc_grace_seconds (10 days by default)Reads that scan many tombstones get slow, and fail at tombstone_failure_threshold (100,000).
hints/writes for a replica that was down, kept up to max_hint_window (3 hours)Longer outages need nodetool repair to catch up.
Follow it

Follow one write at QUORUM, and one node down

Replication factor 3, one chat message, written at QUORUM. Step through it, then tick the box.

A replica is down and you write at QUORUM with three replicas. Does the write succeed?

Yes. Two of three is a quorum. The coordinator keeps a hint for the missing replica. At consistency ALL the same write would fail. Tick the box in the widget to step through it.

The settings that matter

Choose these per keyspace and per query

SettingDefaultWhat it trades
Replication factorset per keyspaceHow many copies. Three per data centre, with NetworkTopologyStrategy, is the usual choice.
Consistency levelper query; often LOCAL_ONE in the driversONE: fastest, can read stale data. QUORUM or LOCAL_QUORUM: reads see acknowledged writes. ALL: fails if any replica is down.
commitlog_syncperiodic, every 10 sbatch or group waits for the disk before acknowledging. Periodic relies on other replicas for safety.
Compaction strategySizeTiered is the long-standing defaultLeveled for read-heavy tables, TimeWindow for time series with TTLs. The Unified strategy in 5.0 can behave like either.
gc_grace_seconds864000, 10 daysHow long deletes are remembered. Run a full repair more often than this.
Partition keyyour choice, per tableDecides which nodes hold a row and how big a partition gets. The most important decision in a Cassandra design.
What breaks first

The failures I would plan for

What happensWhat you seeWhat I do
A partition that grows foreverOne node hot, slow reads, compaction strugglingAdd a time bucket to the partition key, such as (conversation_id, month). Keep partitions well under 100 MB.
Tombstones read in a rangeSlow reads, then TombstoneOverwhelmingExceptionModel reads so they do not scan deleted rows. TimeWindow compaction for data that expires with a TTL.
Repair not run within gc_grace_secondsDeleted data comes backSchedule repair. Cassandra 6.0 builds repair in.
A query the key cannot answerRejected unless you add ALLOW FILTERING, then a scan of every nodeA second table keyed for that query, or a storage-attached index (5.0) for selective filters.
Read, change, writeLost updates: writes are last-write-wins by timestampWrite without reading first. Use counters, or lightweight transactions (IF ...) where you truly need compare-and-set.
Lightweight transactions everywhereWrites several times slowerUse them only for the few rows that need them.
Clocks out of stepAn older write wins over a newer oneNTP everywhere, and let the server assign timestamps.
Where it fits in a design

Endless writes, read by key

Use it for

Not for

  • Money with transactions across rows: Payments and ledgers
  • Ad hoc queries and reports: Analytics
  • Small datasets: one PostgreSQL is simpler
  • Joins, or unique constraints across rows
Check it yourself

The commands I run first

nodetool status                         # every node: up or down, load, ownership
nodetool tablestats chat.messages       # SSTables, partition sizes, tombstones per read
nodetool tablehistograms chat messages  # SSTables per read, partition size percentiles
nodetool compactionstats                # compactions running and pending
nodetool tpstats                        # thread pools, dropped messages, hints
nodetool repair -pr chat                # repair this node's primary ranges
cqlsh> CONSISTENCY QUORUM;
cqlsh> TRACING ON;                      # then run a query: where it spent its time, replica by replica
Measured on this site

Notebooks 7 and 9, Cassandra 5.0.9 in CI

Versions and licences. Cassandra 5.0 (September 2024; 5.0.9 in August 2026) added storage-attached indexes, vector search, the Unified Compaction Strategy, and trie-based memtables and SSTables. Cassandra 6.0 is in alpha in 2026 with Accord, ACID transactions across partitions, and transactional cluster metadata; check its status before you plan on it.

Your turn

A chat app stores messages with PRIMARY KEY (channel_id, sent_at). One channel has 50 million messages, and reads for it are slow on the same few nodes. What is wrong, and what would you change?

Write your answer down first. Then open mine.

Show my answer

What is wrong: the partition key is channel_id alone, so the whole channel is one partition on the same three replicas. At about 300 bytes a message that is roughly 15 GB in one partition: one hot set of nodes, long compactions, and reads that walk a huge partition.

What I would change: add a time bucket, PRIMARY KEY ((channel_id, day), sent_at), or month for quieter channels, so each partition stays well under 100 MB. The app reads the latest bucket first and walks back.

And: old buckets can expire with a TTL and TimeWindow compaction, so retention is files being dropped, not millions of tombstones.

Connected

Where this shows up

The same mechanism, hot keys and hot partitions, sharding and partitioning, elsewhere on the site: in real incidents, in measurements, and in designs.

Check yourself · 4 questions

Check yourself

Pick an answer. You will see whether it is right, and why.

1. What decides which nodes store a row?

2. Three replicas. You write at QUORUM and read at QUORUM. Does the read see the write?

3. What is a tombstone?

4. Why must repair run within gc_grace_seconds?

Part 1 of 4 · Learn · 23 pagesAll pages

How do databases actually work? From one row on disk to transactions: the words you need, PostgreSQL, MySQL and MongoDB from the files up, then Redis, Cassandra and vector search, side by side, and the query and concurrency traps.