This behavior in Cassandra seems counter-intuitive and I want to know why this is happening, and possibly work around this.
Imagine I have a table with three columns: pk
, the primary key, a text
type, foo
, a bigint
, and bar
, another text
.
insert into keyspace.table (pk, foo, bar) values ('first', 1, 'test') using ttl 60;
This creates a row in my table that has a time-to-live of 60 seconds. Looking at it, it looks like this:
pk | foo | bar
------------------
first | 1 | test
Now I do:
update keyspace.table using ttl 10 set bar='change' where pk='first';
And then, watching the row, I see it undergo the following changes:
pk | foo | bar
--------------------
first | 1 | change
first | 1 | <<null>> // after 10 seconds
<< deleted >> // after the initial 60 seconds
All well and good. What I wanted was for bar
's time-to-live to change, but nothing else, especially not the primary key. This behavior was expected.
However, if my update doesn't have a ttl
in it, or it's set to 0:
update keyspace.table set bar='change' where pk='first';
Then I see this behavior over time instead.
pk | foo | bar
--------------------
first | 1 | change
first | 0 | change // after the initial 60 seconds
In other words, the row is never deleted. foo
hadn't been changed, so its time-to-live was still in effect and after it passed the value was deleted (set to 0). But pk
did have its time-to-live changed. This is totally unexpected.
Why does the primary key's time-to-live change only if I don't specify the time-to-live in the update? And how can I work around this so that the primary key's time-to-live will only change if I explicitly say to do so?
Edit I also found that if I use a time-to-live that's higher than the initial one it also seems to change the time-to-live on the primary key.
update keyspace.table using ttl 70 set bar='change' where pk='first';
pk | foo | bar
--------------------
first | 1 | change
first | 0 | change // after the initial 60 seconds
<< deleted >> // after the 70 seconds