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
Interview questions
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. 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. 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
- How is data modeled in Cassandra and why do you design queries first and tables after? What are the partition key and the clustering key?MiddleDatabases
- A candidate says: PostgreSQL is CA, MongoDB is CP, Cassandra is AP. What is wrong with that claim? Explain CAP and PACELC in a way that helps configure a real system.SeniorDatabases
Related technologies
- Amazon DynamoDBA fully managed AWS key-value and document database with predictable latency and automatic scaling.
- MongoDBThe most popular document NoSQL database, storing data as JSON-like documents (BSON).
- Apache KafkaKafka is a distributed event log for high-throughput data streaming with durable storage.
- ClickHouseA columnar database for fast analytics (OLAP) over billions of rows.
Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).