TTL and Batches in Cassandra (CQL Explained with Examples)

10/12/2025

Cassandra TTL and Batch Operations Diagram — CQL Expiration and Atomic Writes Workflow

Go Back

TTL and Batches in Cassandra (CQL Explained with Examples)

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.


 Cassandra TTL and Batch Operations Diagram — CQL Expiration and Atomic Writes Workflow

🔹 What is TTL (Time to Live) in Cassandra?

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.

Syntax

INSERT INTO users (user_id, name, email)
VALUES (1, 'Shubham', '[email protected]')
USING TTL 86400;

🕐 Here, the row will automatically expire after 86400 seconds (24 hours).


Using TTL with UPDATE

You can also apply TTL during updates:

UPDATE users USING TTL 3600
SET email = '[email protected]'
WHERE user_id = 1;

This updates the email and sets it to expire in 1 hour.


Checking TTL

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.


Remove TTL (Make Data Permanent)

To remove TTL from a row, simply overwrite it without the USING TTL clause:

INSERT INTO users (user_id, name, email)
VALUES (1, 'Shubham', '[email protected]');

⚙️ How TTL Works Internally

  1. TTL is stored as a timestamp (expiration time) with the column data.

  2. When the TTL expires, the data isn’t immediately removed — it’s marked as tombstoned.

  3. During compaction, these tombstones are purged from storage.

Important Notes

  • 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.


🧩 Common Use Cases for TTL

Use CaseDescription
Caching layerAutomatically remove outdated cache entries.
Session managementAuto-expire inactive sessions after a set period.
Temporary dataDelete OTPs, logs, or notifications after a few hours or days.
IoT dataAuto-clean old sensor readings.

🦎 Batches in Cassandra

What are Batches?

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.

Syntax Example

BEGIN BATCH
INSERT INTO users (user_id, name) VALUES (1, 'Shubham');
UPDATE users SET email = '[email protected]' WHERE user_id = 1;
APPLY BATCH;

✅ Both statements execute atomically — either both succeed or both fail.


Batch Types

TypeDescription
Logged BatchDefault type; ensures all mutations succeed together.
Unlogged BatchNo atomicity guarantee; faster for operations on multiple partitions.
Counter BatchUsed when batching counter updates.

Logged Batch Example

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.


Unlogged Batch Example

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.


⚡ Performance Considerations

FeatureBest PracticeAvoid
TTLUse for expiring temporary data.Setting TTL on high-volume writes; can create tombstone overhead.
BatchesUse for small atomic writes (same partition).Large batches or spanning many partitions.

Important Notes

  • 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.


🚀 Combining TTL and Batch Example

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.


🧠 Conclusion

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.