Question

I'd need to understand if/how a call to MutationBatch.execute() is safe against the server running the code going down.

Have a look at the code below (copy from the Astyanax examples). I intend to use this code to modify 2 rows in 2 different column families. I need to ensure (100%) that if the server executing this code crashes/fails at any point during the execution either: - nothing is changed in the Cassandra datastore - ALL changes (2 rows) are applied to the Cassandra datastore

I'm especially concerned about the line "OperationResult result = m.execute();". I would assume that this translates into something like: write all modifications to a commit log in Cassandra and then atomically trigger a change to be executed inside Cassandra (and Cassandra guarantee execution on some server).

Any help on this is very appreciated.

Thanks, Sven.

CODE:

MutationBatch m = keyspace.prepareMutationBatch();

long rowKey = 1234;

// Setting columns in a standard column
m.withRow(CF_STANDARD1, rowKey)
    .putColumn("Column1", "X", null)
    .putColumn("Column2", "X", null);

m.withRow(CF_STANDARD1, rowKey2)
    .putColumn("Column1", "Y", null);

try {
    OperationResult<Void> result = m.execute();
} catch (ConnectionException e) {
    LOG.error(e);
} 
Was it helpful?

Solution

http://www.datastax.com/docs/0.8/dml/about_writes

In Cassandra, a write is atomic at the row-level, meaning inserting or updating columns for a given row key will be treated as one write operation. Cassandra does not support transactions in the sense of bundling multiple row updates into one all-or-nothing operation.

This means, that there is no way to be 100% sure, that mutation will update two different rows or none. But since Cassandra 0.8 you will have such guarantee at least within single row - all columns modified within single row will success or none - this is all.

You can see mutations on different rows as separate transactions, the fact that they are send within single mutation call does not change anything. Cassandra internally will group all operations together on row key, and execute each row mutation as separate atomic operation.

In your example, you can be sure that rowKey (Column1,Column2) or rowKey2(Column1) was persisted, but never both.

You can enable Hinted Handoff Writes, this would increase probability, that write will propagate with time, but again, this is not ACID DB

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top