Cassandra Deep Dive

Apache Cassandra from the inside: the token ring, tunable consistency, the log-structured write and read paths, compaction, tombstones and repair, and how to model tables around queries.

Overview

Apache Cassandra is a distributed database that spreads rows over many ordinary servers with no leader. Every node can take any request, data is copied to several nodes, and the cluster keeps working when some of them fail. It was built at Facebook for inbox search, which needed billions of writes a day, and it is now an Apache project.

Its design joins two older ideas. From Amazon's Dynamo it takes the ring of nodes, replication and tunable consistency. From Google's Bigtable it takes the storage engine: writes go to a log and a sorted in-memory table, which is later flushed to immutable files on disk. The result is a database that writes very fast, scales by adding nodes, and runs across datacenters.

The price is that Cassandra does less for you. There are no joins, no foreign keys and no transactions across partitions. You design one table per query, a delete is a new write called a tombstone, and replicas drift apart unless you run repair. Interviewers ask about Cassandra to see if you know both halves of that bargain.

What you will be able to do

  • Explain how a partition key picks the nodes that store a row, and why nodes own many small ranges.
  • Walk through the write path and the read path, and say why writes are cheap and reads cost more.
  • Choose a consistency level and a compaction strategy for a workload, and say what each costs.
  • Explain tombstones, why deleted data can come back, and how repair prevents it.
  • Design Cassandra tables from a list of queries, and keep partitions a sensible size.

Key concepts

  • Token ring and vnodes
  • Replication and racks
  • Tunable consistency
  • Commit log and memtable
  • SSTables and Bloom filters
  • Compaction strategies
  • Tombstones and gc_grace_seconds
  • Hints and repair
  • Query-first data modeling
  • Lightweight transactions

Interview questions

  • How does Cassandra decide which nodes store a row?
  • Walk through the write path, and explain why writes are fast.
  • How does a read work, and why is it slower than a write?
  • Which consistency levels would you use, and what does W + R > N give you?
  • What are tombstones, and how can deleted data come back?
  • Compare the compaction strategies. Which would you pick for chat messages, and for a user profile table?
  • Design the tables for a chat app on Cassandra.
  • When would you not choose Cassandra?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio