TTL and Batches in Cassandra (CQL Explained with Examples)
Cassandra TTL and Batch Operations Diagram — CQL Expiration and Atomic Writes Workflow
Overview
When working with large-scale data in Apache Cassandra, managing data lifecycle and performing efficient bulk updates become essential. Two powerful features in Cassandra’s Query Language (CQL) — TTL (Time to Live) and Batches — help address these needs.
TTL automatically expires data after a defined time period.
Batches allow executing multiple write operations in a single, atomic request.
This article explains how TTL and Batches work, their best practices, and common use cases.
TTL (Time to Live) is a CQL feature that lets you set an expiration time (in seconds) for individual columns or entire rows. After the TTL duration expires, Cassandra automatically deletes the data during compaction.
INSERT INTO users (user_id, name, email)
VALUES (1, 'Shubham', 'shubham@example.com')
USING TTL 86400;
🕐 Here, the row will automatically expire after 86400 seconds (24 hours).
You can also apply TTL during updates:
UPDATE users USING TTL 3600
SET email = 'shubham@orientalguru.co.in'
WHERE user_id = 1;
This updates the email and sets it to expire in 1 hour.
To view the remaining TTL for a column:
SELECT TTL(email) FROM users WHERE user_id = 1;
This command returns the number of seconds left before expiration.
To remove TTL from a row, simply overwrite it without the USING TTL clause:
INSERT INTO users (user_id, name, email)
VALUES (1, 'Shubham', 'shubham@example.com');
TTL is stored as a timestamp (expiration time) with the column data.
When the TTL expires, the data isn’t immediately removed — it’s marked as tombstoned.
During compaction, these tombstones are purged from storage.
Each column can have its own TTL.
If a column with TTL is updated, its timer resets.
TTL doesn’t affect existing non-TTL columns in the same row.
| Use Case | Description |
|---|---|
| Caching layer | Automatically remove outdated cache entries. |
| Session management | Auto-expire inactive sessions after a set period. |
| Temporary data | Delete OTPs, logs, or notifications after a few hours or days. |
| IoT data | Auto-clean old sensor readings. |
A batch in Cassandra allows grouping multiple write operations into a single statement. They are mainly used for atomicity (ensuring all writes succeed together) rather than for performance improvement.
BEGIN BATCH
INSERT INTO users (user_id, name) VALUES (1, 'Shubham');
UPDATE users SET email = 'shubham@orientalguru.co.in' WHERE user_id = 1;
APPLY BATCH;
✅ Both statements execute atomically — either both succeed or both fail.
| Type | Description |
|---|---|
| Logged Batch | Default type; ensures all mutations succeed together. |
| Unlogged Batch | No atomicity guarantee; faster for operations on multiple partitions. |
| Counter Batch | Used when batching counter updates. |
BEGIN BATCH
INSERT INTO orders (order_id, item) VALUES (1001, 'Laptop');
INSERT INTO orders (order_id, item) VALUES (1002, 'Mouse');
APPLY BATCH;
Ensures both rows are written atomically.
BEGIN UNLOGGED BATCH
INSERT INTO sales (region, product, amount) VALUES ('Asia', 'Laptop', 50000);
INSERT INTO sales (region, product, amount) VALUES ('Europe', 'Mouse', 1500);
APPLY BATCH;
Used when writing to different partitions, as atomicity isn’t required.
| Feature | Best Practice | Avoid |
|---|---|---|
| TTL | Use for expiring temporary data. | Setting TTL on high-volume writes; can create tombstone overhead. |
| Batches | Use for small atomic writes (same partition). | Large batches or spanning many partitions. |
Batch ≠ Performance Optimization. Batches don’t reduce network round trips if you write to multiple partitions.
Use unlogged batches only for logically grouped writes.
Avoid large batches (> 50 KB data or 100 operations).
Monitor tombstone warnings caused by expired TTLs.
BEGIN BATCH
INSERT INTO temp_sessions (session_id, user_id, created_at)
VALUES (111, 1, toTimestamp(now())) USING TTL 3600;
INSERT INTO temp_sessions (session_id, user_id, created_at)
VALUES (112, 2, toTimestamp(now())) USING TTL 3600;
APPLY BATCH;
This batch inserts multiple temporary sessions that auto-expire in 1 hour.
Cassandra’s TTL feature simplifies automatic data expiration, while batches help execute grouped writes atomically. By using these tools wisely, developers can manage data efficiently and maintain high performance.
Key takeaways:
TTL is perfect for temporary data management.
Batches ensure consistency, not speed.
Avoid large batches and excessive TTL use to prevent performance degradation.