Cassandra
database management
data overwriting
Cassandra writes
database architecture

When are rows overwritten in cassandra

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Cassandra is a distributed NoSQL database known for its scalability, high availability, and performance. It's designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. One of the key features of Cassandra is its eventual consistency model, which implies that while updates are made, consistency across replicas is achieved eventually.

A common point of confusion among Cassandra users is the concept of overwriting rows. Let's dive into the technicalities to understand when rows are overwritten in Cassandra.

The Write Path in Cassandra

Before discussing overwriting, it's crucial to understand how writes work in Cassandra:

  1. Client Request: A client sends a write request to any node in the cluster (it's an elastic system, so any node can handle any request).
  2. Commit Log: The write operation is first recorded in the commit log for durability.
  3. Memtable: It is an in-memory data structure where the data is written next. Once the memtable is full, it is flushed to disk in the form of an SSTable (Sorted String Table).
  4. SSTables: Immutable data files on disk, sorted by partition.

Overwriting Rows

In Cassandra, data is stored in a form of rows, but in contrast to traditional relational databases, Cassandra's writes are upserts (updates or inserts). This particular aspect leads to a unique approach in handling row overwrites:

  • Timestamp-Based Writes: Each writes operation is timestamped. If multiple updates to the same cell occur, the cell with the latest timestamp prevails. Hence, overwriting happens based on this timestamp mechanism rather than direct replacement.
  • Idempotence: Because of the upsert behavior, writing the same row again leads to the data being overwritten, but only if the new write has a more recent timestamp.
  • TTL (Time to Live): Rows in Cassandra can be set with a TTL, after which they are considered deleted. Writing a new row with the same primary key, but without TTL or with a different TTL, can result in the old data being marked for deletion after its TTL expires.

Example: Write and Overwrite

Consider a table users with the columns: user_id (primary key), name, and email. Let's illustrate overwriting with an example:

  1. Initial Write:
cql
    INSERT INTO users (user_id, name, email) VALUES (1, 'Alice', '[email protected]');
  1. Overwrite:
cql
    INSERT INTO users (user_id, name, email) VALUES (1, 'Alice', '[email protected]');

In this example, the row with user_id 1 is overwritten. The email field changes because the newer insert carries a more recent timestamp.

Compaction and Overwrites

Cassandra uses a process called compaction to merge SSTables and remove obsolete data:

  • Tombstones: Deleted data doesn't immediately disappear but is marked with a tombstone. During compaction, these tombstones help remove the actual data.
  • Update Overwrites: Data write with a newer timestamp during compaction will overwrite older data with the same key.

Conclusion

In Cassandra, rows are "overwritten" through a combination of upserts with timestamp-based conflict resolution and the compaction process, which clears outdated data.

Summary Table

ConceptDescriptionBehavior
Write PathSteps involved in a writeCommit Log ➔ Memtable ➔ SSTable
Timestamp-Based ResolutionLatest timestamp winsResolves conflicts
IdempotenceRepeated writes have no side effects if sameOverwrites if the latest
TTL and ExpiryData expires based on TTLResets if re-written without TTL
CompactionMerges and cleans up SSTablesRemoves obsolete entries

Understanding the internal mechanics of Cassandra's write and overwrite behavior can significantly influence designing your application for optimal data retention and consistency.


Course illustration
Course illustration

All Rights Reserved.