Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

fatihucar avatar image
fatihucar asked Erick Ramirez edited

Spring boot CassandraBatchOperations batch size handling

Hi,

I am developing a REST API with spring boot and open source cassandra. When an insert request comes from client to my API, I need to insert my main table and other source of truth tables in same keyspace.

I have to insert 5 - 10 partitions and the operation is need to be atomic insert. I am using CassandraBatchOperations from CassandraTemplete for batch. There is a batch size limit in cassandra (default 50kb). I want to handle this batch size. For example: Client sends an object to API for insert data to database and then API prepares the batch. I want to check this batch size before batch execute. If batch size < 50kb i will insert the data then return true as a respone to client. If batch size > 50kb then return false as a repsonse to client.

I tried many ways and i couldnt handle it.

Is anybody try this before?

Thanks.

java driverspring-bootbatch
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

steve.lacerda avatar image
steve.lacerda answered fatihucar commented

I'm not sure which version or flavor of the driver you're using, but in java, the BatchStatement class has a computeSizeInBytes method that could help you:

https://docs.datastax.com/en/drivers/java/4.9/

Alternatively, you could send the batch and if it fails with something like the following:

com.datastax.driver.core.exceptions.InvalidQueryException: Batch too large

You could then return the false flag to the client.

The second option doesn't seem ideal based on the RTT, but it may actually be more performant than the first option due to having to encode the statement and compute the size of every request.

1 comment Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Hi Steve,

When I consider about the possibility of 'batch too large' error for my cases, it has very low possibility to cause an error. So the second option might be very performent than the first option.

I am very gradeful for your advice.

Thank you.

0 Likes 0 ·