When are rows overwritten in cassandra
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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:
- 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).
- Commit Log: The write operation is first recorded in the commit log for durability.
- 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).
- 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:
- Initial Write:
- Overwrite:
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
| Concept | Description | Behavior |
| Write Path | Steps involved in a write | Commit Log ➔ Memtable ➔ SSTable |
| Timestamp-Based Resolution | Latest timestamp wins | Resolves conflicts |
| Idempotence | Repeated writes have no side effects if same | Overwrites if the latest |
| TTL and Expiry | Data expires based on TTL | Resets if re-written without TTL |
| Compaction | Merges and cleans up SSTables | Removes 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.
Related reading
- When does DynamoDB throttle request?
- When does SQLiteOpenHelper onCreate / onUpgrade run?
- When I remove rows in Cassandra I delete only columns not row keys
- When is data consistency not an issue?
- When might 2 phase commit not make progress?
- When should I use a composite index?
- When should I use a NoSQL database instead of a relational database? Is it okay to use both on the same site?
- When should I use CROSS APPLY over INNER JOIN?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.