Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra: Batch with conditions cannot span multiple tables

I am trying to execute 3 conditional inserts to different tables inside a batch by using the Cassandra cpp-driver:

BEGIN BATCH 
insert into table1 values (...) IF NOT EXISTS 
insert into table2 values (...) IF NOT EXISTS 
insert into table3 values (...) IF NOT EXISTS 
APPLY BATCH

But I am getting the following error:

Batch with conditions cannot span multiple tables

If the above is not possible in Cassandra, what is the alternative to perform multiple conditional inserts as a transaction and ensure that all succeed or all fail?

like image 872
georgeliatsos Avatar asked Mar 11 '23 11:03

georgeliatsos


1 Answers

I'm afraid there are no alternatives. Conditional statements in a BATCH environment are limited to a single table only, and I don't think there's room for changes in future.

This is due to how Cassandra works internally: a batch containing a conditional update (it is called lightweight transaction) can only be used in one partition because they are based on the Paxos implementation, because the Paxos itself works at partition level only. Moreover, in a batch with multiple conditional statements in the same BATCH, all the conditions must be verified to the batch succeed. Even if one (and only) conditional update fails, the entire batch will fail.

You can read more about BATCH statements in the documentation.

You'd basically get a performance hit for the conditional update, and a performance hit for a batched operation, and C* stops you to get so far.

It seems to me you designed it RDBMS-like. A No-SQL alternative solution, I don't know if it can be applied to your use case though, you could denormalize your data in a 4th table that combines all the other 3 tables, and then supply a single update to this 4th table.

like image 73
xmas79 Avatar answered Apr 06 '23 09:04

xmas79