How to pass TTL in Cassandra Java Driver QueryBuilder?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
TTL is Cassandra's built-in expiration mechanism: you write a value, attach a number of seconds, and Cassandra automatically expires that data later. In Java, the exact QueryBuilder syntax depends on the driver generation you are using, so the safest answer is to show the current driver 4 style first and then note the older driver 3 form. The core idea is always the same: TTL is part of the write statement, not a separate option passed at execute time.
Using TTL with the current QueryBuilder API
In the DataStax and Apache Java driver 4 line, usingTtl(...) is available directly on insert and update builders. You can pass a literal number of seconds or a bind marker.
This writes the row with a one-hour TTL. Using a bind marker is helpful when different rows need different retention windows.
Updating data with a TTL
TTL is not limited to inserts. You can also apply it to updates. That is useful for expiring refresh tokens, cache rows, or rolling activity markers.
In driver 4, a TTL of 0 has a special meaning: it removes the TTL for the written cells instead of making them expire immediately. That detail matters when you are intentionally overriding a table-level or previously written TTL.
Older driver syntax looks different
If you are maintaining code on the older com.datastax.driver.core package, the QueryBuilder API uses using(ttl(...)) instead of usingTtl(...).
That difference is why many answers online appear to disagree with each other. They are often correct for different driver generations. Check the imported package names before copying an example.
What TTL really applies to
TTL applies to the values you write in that statement. It is not a query timeout, and it is not a server-side scheduler. Cassandra stores expiration metadata with the cells and discards them when the TTL has elapsed. If you update only some columns in a row, the TTL affects the columns you touched, not every column in the row by default.
This is also why TTL belongs in the statement builder. The retention rule is part of the data mutation itself.
Common Pitfalls
The biggest pitfall is mixing driver 3 and driver 4 examples. If your imports start with com.datastax.oss.driver, use usingTtl(...). If they start with com.datastax.driver.core, the older using(ttl(...)) syntax is likely the one you need.
Another mistake is binding parameters in the wrong order. Prepared statement bind markers are positional, so the TTL marker must be bound where it appears in the query.
Developers also assume TTL behaves like a row-wide expiration switch. In Cassandra, TTL is attached to written cells, so partial updates can produce rows whose columns expire at different times.
Finally, remember that 0 is not "expire now" in the driver 4 QueryBuilder API. It removes the TTL on the written data.
Summary
- In Java driver 4, pass TTL with
usingTtl(...)on the QueryBuilder statement. - Use a bind marker when TTL varies per write, and a literal when it is fixed.
- In older driver 3 code, the equivalent syntax is
using(ttl(...)). - TTL is part of the mutation and applies to the cells written by that statement.
- Be careful with bind order and the special meaning of a TTL value of
0.
Related reading
- How to perform a mysqldump without a password prompt?
- How to perform better document version control on Excel files and SQL schema files
- How to perform OR condition in django queryset?
- How to persist a property of type ListString in JPA?
- How to pause / sleep thread or process in Android?
- how to pause and resume @KafkaListener using spring-kafka
- How to persist data in a dockerized postgres database using volumes
- How to persist data using a postgres database, Docker, and Kubernetes?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.