Cassandra, how is data maintained?
How is data maintained?
The Cassandra write process stores data in files called SSTables. SSTables are immutable.
Instead of overwriting existing rows with inserts or updates, Cassandra writes new timestamped versions of the inserted or updated data in new SSTables.
Cassandra does not perform delete by removing the deleted data; instead, Cassandra marks it with tombstones.
Over time, Cassandra may write many versions of a row in different SSTables. Each version may have a unique set of columns stored with a different timestamp.
As SSTables accumulate, the distribution of data can require accessing more and more SSTables to retrieve a complete row.
- To keep the database healthy, Cassandra periodically merges SSTables and discards old data. This process is called "compaction".
Compaction
- Compaction works on a collection of SSTables. From these SSTables, compaction collects all versions of each unique row and assembles one complete row, using the most up-to date version (by timestamp) of each of the row’s columns.
- The merge process is performant, because rows are sorted byt partition key within each SSTables, and the merge process does not use random I/O. The new version of each row is written to a new SSTable.
- The old versions, along with any rows that are ready for deletion, are left in the old SSTables, and are deleted as soon as pending reads are completed.
Compaction causes a temporary spike in disk space usages and disk I/O while old and new SSTables co-exist. As it completes, compaction frees up disk space occupied by old SSTables.
It improve read performance by incrementally replacing old SSTables with compacted SSTables.
Cassandra can read data directly from the new SSTable even before it finishes writing, instead of waiting for the entire compaction process to finish.
As Cassandra processes writes and reads, it replaces the old SSTables with new SSTables in the page cache. The process of caching the new SSTable, while directing reead away from the old one, it incremental it does not cause a the dramatic cache miss, Cassandra provides predictable high performance even under heave load.