Chapter 9. Reading and Writing Data

Now that we understand the data model and how to use a simple client, we’ll dig deeper into the different kinds of queries you can perform in Cassandra to read and write data. We’ll also take a look behind the scenes to see how Cassandra handles your read and write queries. 

As with the previous chapter, we’ve included code samples using the DataStax Java Driver to help illustrate how these concepts work in practice.

Writing

Let’s start by noting some basic properties of writing data to Cassandra. First, writing data is very fast in Cassandra, because its design does not require performing disk reads or seeks. The memtables and SSTables save Cassandra from having to perform these operations on writes, which slow down many databases. All writes in Cassandra are append-only.

Because of the database commit log and hinted handoff design, the database is always writable, and within a column family, writes are always atomic.

Insert, Update, and Upsert

Because Cassandra uses an append model, there is no fundamental difference between the insert and update operations.  If you insert a row that has the same primary key as an existing row, the row is replaced. If you update a row and the primary key does not exist, Cassandra creates it.

For this reason, it is often said that Cassandra supports upsert, meaning that inserts and updates are treated the same, with one minor exception which we’ll see on lightweight transactions.

Write Consistency Levels ...

Get Cassandra: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.