Partial update in Cassandra without Codec

Ferdi Tatlisu
Trendyol Tech
Published in
3 min readApr 25, 2022

If you have a vast domain with many nested UDT's, and you don’t want to write countless codecs, you’re in the right place. Today, we are going to talk about how to achieve this without codecs.

Why did we choose partial update if you want to skip the story? Click to dive into the code.

How did we use the ORM tool?

As you can see above, a method updates the only priority column. Let's explain with simple terms, it gets a Ticket(domain) object from the database, set a new priority value, and whole ticket fields save to the database with the ORM tool.

Well, how does this give us trouble?

Let's imagine that. There is a Ticket(domain) object in the database, and it looks like the below.

Let’s send two requests at the same time. One is title-change-request and another one is priority-change-request.

Normally, the ticket(domain) should be like title = “Medium”, priority = “NORMAL” but let’s look result.

Only the priority column has changed. The main reason is two requests got the Ticket(domain) object from the database. Both of them changed only affected fields. First, title = “Medium” and priority = “CRITICAL_URGENT” saved to the database. After that, title = “Transfer Delivery” and priority = “NORMAL” saved to the database. One request is overridden when multiple transactions are made on the same ticket. Also, it can happen reverse. This is a huge problem.

Can we solve this problem with partial update?

You can see the example below. Let’s change the content of our previous method slightly.

Let's explain the method above. We create a “ticketDbQuery” object and use it with the builder pattern. We only update the priority column and execute with QUORUM which is write consistency level.

Let’s take a look TicketDbQuery class.

What is TicketDbQuery?

Let’s explain codes step by step

First, we invoke this line with our domain with that updated.

This line is essential because we use a Cassandra Converter instead of codecs. This provides us with the column and column’s value from our domain object to LinkedHashMap.

We start creating update queries. Like “Update tickets”.

We create our builder from our domain object with two methods. Because these columns have to update when the domain is updated.

“priority” is a column name in our table. We add a column to the current query when this method is invoked.

Before we created LinkedHashMap called udtObjects with our domain values, we get the “priority” value and put it to the current query. Like “Update tickets SET priority = ‘NORMAL’”.

Next, all columns that we want to update are added, when we invoked the “execute” method with consistency level. Always our domain update with only the primary key as “id”. That’s why we invoke the “whereById” method. It returns a simple statement and executes it with a consistency level you picked.

That’s all. You can execute your partial queries without codecs.

If you want to change only one field inside of the UDT object. Then you should invoke this method with columnName and fieldName.

Output:

Here is an example of the project including what you see in this article.

THE END

--

--