Databases

Apache Cassandra

A distributed wide-column database with no single point of failure, built for massive write volumes.

What it is

Cassandra was built at Facebook, combining ideas from Amazon Dynamo and Google Bigtable. It is now an Apache project. A cluster consists of equal nodes with no master: any node accepts a request.

Data is distributed by partition key through a consistent hashing ring and replicated to several nodes. The consistency level is tunable per query, from ONE to QUORUM and ALL.

Writes are very fast (LSM tree). The price is a restricted query model: you design the schema around queries, with no joins or arbitrary filters. The CQL language looks like SQL.

Use it when

  • Very large write streams: events, metrics, IoT, messages.
  • You need high availability and multiple data centers.
  • Queries are known in advance and simple by key.

Avoid it when

  • Ad hoc analytics, joins and aggregations: PostgreSQL or ClickHouse.
  • Small projects: the operational complexity is excessive.

Key concepts

Partition key
Decides which nodes hold the data.
Clustering columns
Define row order inside a partition.
Consistency level
How many replicas must answer a request.
Tombstone
A deletion marker that lives until compaction.

Example

A table built for the "latest user events" query.

Interview questions

  1. 1. What is a consistency level?

    The number of replicas that must confirm a read or write. QUORUM for both reads and writes gives strong consistency, ONE gives speed with possible staleness.

  2. 2. Why do you design tables around queries in Cassandra?

    Data is distributed by partition key and there are no joins or arbitrary filters. Each query type often gets its own table.

  3. 3. What is a tombstone?

    A deletion marker that is replicated and removed at compaction. Too many of them slow reads.

Interview questions from the bank

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).