Performance Tuning & Production Operations
Module Duration: 10-12 hours
Learning Style: Deep Technical + Hands-On Tuning + Production War Stories
Outcome: Operate Cassandra clusters at peak performance in production environments
Introduction: The Production Reality
Running Cassandra in production is vastly different from development:- Development: Single node, small dataset, tolerant of restarts
- Production: 50+ nodes, multi-TB per node, 24/7 uptime, millisecond SLAs
Part 1: The JVM - Cassandra’s Foundation
Cassandra runs on the Java Virtual Machine (JVM). JVM performance directly impacts Cassandra performance, especially around garbage collection (GC).Why GC Matters
Problem: Cassandra keeps data in memory (MemTables, caches). When JVM runs GC:- Stop-the-world pauses: Application threads freeze
- Pauses > 1 second → timeouts, failed requests
- Pauses > 10 seconds → nodes marked as DOWN by failure detector
Heap Size Configuration
Cassandra’s heap is split into two regions:
Real Example:
Garbage Collection Algorithms
Cassandra supports three main GC algorithms:1. G1GC (G1 Garbage Collector) - Recommended
Best For: Cassandra 3.0+, default choice Configuration:2. CMS (Concurrent Mark Sweep) - Legacy
Best For: Cassandra 2.x (deprecated in Cassandra 3.0+)- Fragmentation in Old Gen → Full GC (5-10 second pauses!)
- Deprecated in Java 9+
3. ZGC / Shenandoah - Experimental
Best For: Cassandra 4.0+, Java 11+, cutting-edge deployments- Sub-10ms pauses even with 100GB heaps!
- Still experimental for Cassandra
GC Logging and Monitoring
Enable GC Logging:Pause Young: Young generation collection2048M->512M: Heap before → after GC(8192M): Total heap size45.678ms: Pause duration (monitor this!)
- GCViewer (GUI):
- GCEasy (Web-based):
- Pause time p99: Should be < 200ms
- Pause frequency: Young GC every 5-10 seconds is normal
- Full GC events: Should be 0! Any Full GC is a red flag
Common GC Issues
Issue 1: Frequent Full GCs
Symptoms:- Heap too small
- Memory leak (improper cache configuration)
- Large object allocation (huge queries)
Issue 2: Long Young GC Pauses
Symptoms:Issue 3: Memory Pressure
Symptoms:- Constant GC activity
nodetool tpstatsshows dropped mutations- Heap constantly near max
Part 2: Operating System Tuning
Disk I/O Configuration
Cassandra is I/O intensive. OS settings massively impact performance.File System Choice
Mount Options (XFS):
noatime: Don’t update access time (reduces writes)nodiratime: Don’t update directory access timenobarrier: Disable write barriers (safe with battery-backed RAID)
I/O Scheduler
For SSDs:Readahead
Default: Often 8KB (too small for Cassandra)Linux Kernel Settings
Criticalsysctl Settings:
User Limits
Cassandra opens many files simultaneously:Swap Configuration
Philosophy: Minimize swap, but don’t disable entirely. Why Not Disable Swap?- Linux kernel may OOM-kill Cassandra if no swap
- Small swap (1-2GB) acts as emergency overflow
Transparent Huge Pages (THP)
Issue: THP causes GC pauses and memory fragmentation. Disable THP:CPU Governor
For Performance:Part 3: Cassandra Configuration Tuning
Compaction Strategy Selection
Choosing the right compaction strategy is critical for performance.STCS (Size-Tiered Compaction Strategy)
Best For: Write-heavy, time-series data, small tables How It Works:- Fast writes (less compaction overhead)
- Simple, predictable
- Read amplification (query may touch many SSTables)
- Temporary disk space = 2x data size during compaction
LCS (Leveled Compaction Strategy)
Best For: Read-heavy, frequently updated data How It Works:- Low read amplification (90% reads touch 1 SSTable)
- Predictable disk space usage
- More compaction overhead (impacts writes)
- More I/O intensive
TWCS (Time Window Compaction Strategy)
Best For: Time-series data with TTL How It Works:- Ultra-fast TTL deletion (drop entire SSTable)
- Minimal read amplification for time-range queries
- Only suitable for time-series with TTL
MemTable Configuration
MemTables are in-memory write buffers. Tuning them balances memory vs. flush frequency.
Recommendation: Default is usually good. Only adjust if:
- Many small writes: Increase MemTable size (reduce flush frequency)
- Memory pressure: Decrease MemTable size
Cache Configuration
Cassandra has three caches:1. Key Cache
Purpose: Cache partition key → SSTable mapping (avoid Bloom filter checks) Configuration:- Read-heavy workloads with hot partitions
- Queries by primary key
- Write-heavy workloads
- Cold data (rarely queried)
2. Row Cache
Purpose: Cache entire rows (most aggressive caching) Configuration:- Consumes heap (increases GC pressure)
- Only helps if reading exact same rows repeatedly
- Most production clusters disable this
3. Counter Cache
Purpose: Cache counter column values (counter tables only) Configuration:Commit Log Tuning
CommitLog is Cassandra’s write-ahead log. Two Modes:- Periodic (default):
- Pros: High write throughput
- Cons: Up to 10 seconds of data loss on crash
- Batch:
- Pros: Minimal data loss (2ms window)
- Cons: 30-50% lower write throughput
Concurrent Operations
Control thread pool sizes:- More CPU cores: Increase
concurrent_reads/writes - More disks: Increase
concurrent_compactors - Memory constrained: Decrease to reduce overhead
Part 4: Monitoring and Observability
Key Metrics to Monitor
1. System Metrics
CPU:2. JVM Metrics
Heap Usage:3. Cassandra Metrics
Thread Pool Stats:Dropped messages should be 0!
Table Statistics:
4. Performance Metrics
Latency:- p50 < 5ms
- p95 < 20ms
- p99 < 50ms
Monitoring Tools
1. Prometheus + Grafana (Recommended)
Architecture:- Install JMX Exporter:
- Configure JMX Exporter (cassandra_jmx.yml):
- Add to JVM Options:
- Configure Prometheus (prometheus.yml):
- Import Grafana Dashboard:
- Dashboard ID: 13183 (Cassandra Overview)
- https://grafana.com/grafana/dashboards/13183
2. DataStax OpsCenter
Commercial tool with free tier:- Visual cluster topology
- Performance graphs
- Repair scheduling
- Backup management
3. Nodetool (Built-in)
Quick Checks:Alert Thresholds
Critical Alerts:
Warning Alerts:
Part 5: Capacity Planning
Disk Capacity
Formula:- STCS: 50% (2x data during compaction)
- LCS: 10% (1.1x data)
- TWCS: 20% (1.2x data)
Memory Capacity
Formula:- Cassandra relies on OS page cache for SSTable caching
- Larger cache = fewer disk reads = better performance
CPU Capacity
Rule of Thumb: 1 CPU core per 1-2 TB of data Example:Network Capacity
Formula:Scaling Triggers
When to Add Nodes:
Scaling Example:
Part 6: Backup and Disaster Recovery
Snapshot-Based Backups
How Snapshots Work:Incremental Backups
Enable Incremental Backups:Backup to External Storage
Script Example (S3):Restore from Backup
Full Restore Process:- Stop Cassandra:
- Clear existing data:
- Restore snapshot:
- Fix ownership:
- Restart Cassandra:
- Run repair (important!):
Point-in-Time Recovery
Requirements:- Full snapshot
- Incremental backups
- CommitLog archives
Part 7: Troubleshooting Production Issues
Issue 1: High Read Latency
Symptoms:- Check SSTable Count:
- Check for Wide Partitions:
- Check Disk I/O:
- Check for Tombstones:
gc_grace_seconds
Issue 2: Write Timeouts
Symptoms:- Check Dropped Mutations:
- Check Pending Compactions:
- Check GC Pauses:
Issue 3: Node Marked as DOWN (But It’s Running)
Symptoms:- Check Failure Detector:
- Check for GC Pauses:
Issue 4: Disk Full
Symptoms:- Delete Old Snapshots:
- Clean Incremental Backups:
- Compact Tables:
- Add Nodes (long-term solution)
Issue 5: Schema Mismatch
Symptoms:- Node 10.0.1.12 has different schema
- Gossip not propagating schema updates
- Force Schema Reset:
- Restart Gossip:
- Rolling Restart (last resort):
Part 8: Advanced Production Topics
Multi-DC Latency Optimization
Problem: Cross-DC writes add 100-200ms latency Solution: UseLOCAL_QUORUM for writes:
Read Consistency Tuning
Scenario: Reads occasionally return stale data Diagnosis: Repair not running frequently enough Solutions:- Increase Read Repair Chance:
- Use Higher Consistency Level:
- Run Repair More Frequently:
Handling Large Partitions
Problem: Partition > 100MB causes:- High read latency
- Timeouts
- OOM errors
- Redesign Data Model (best):
- Add Compaction Threshold:
Connection Pool Tuning (Driver-Side)
Python Driver:Part 9: Performance Checklist
Pre-Production Checklist
-
Hardware
- SSDs for data and commitlog
- 10 Gbps network
- 64GB+ RAM per node
- 8+ CPU cores per node
-
OS Configuration
- XFS filesystem with
noatime,nodiratime - Swap disabled or swappiness=1
- THP disabled
- I/O scheduler: noop (SSD) or deadline (HDD)
- Readahead: 8-16MB
- File descriptor limits: 65536
- CPU governor: performance
- XFS filesystem with
-
JVM Configuration
- Heap: 8GB max
- G1GC enabled
- GC logging enabled
- GC pause target: 200ms
-
Cassandra Configuration
- Compaction strategy matches workload
- Concurrent operations tuned
- Caches configured
- CommitLog on separate disk (if HDD)
- NetworkTopologyStrategy for multi-DC
-
Monitoring
- Prometheus + Grafana or OpsCenter
- Alerts configured
- Log aggregation (e.g., ELK stack)
-
Backup
- Snapshot schedule configured
- Incremental backups enabled
- Restore procedure tested
-
Repair
- Automated repair schedule (weekly)
- Repair monitoring
Performance Testing
Load Testing Tools:- cassandra-stress (built-in):
- NoSQLBench:
- Throughput (ops/sec)
- Latency (p50, p95, p99, p999)
- Error rate
- Resource utilization (CPU, RAM, disk, network)
Part 10: Hands-On Exercises
Exercise 1: JVM Tuning
Scenario: Node experiencing 2-second GC pauses Task:- Analyze GC log:
cat /var/log/cassandra/gc.log - Identify problem (heap too large? Young gen too large?)
- Adjust JVM settings in
jvm11-server.options - Restart and monitor improvements
Exercise 2: Compaction Strategy Comparison
Task:- Create three identical tables with different compaction strategies (STCS, LCS, TWCS)
- Load 10GB of data into each
- Run mixed read/write workload with
cassandra-stress - Compare SSTable count, read latency, write latency
- Which strategy has lowest SSTable count?
- Which strategy has best read latency?
- Which strategy has best write throughput?
Exercise 3: Monitoring Setup
Task:- Install Prometheus + Grafana
- Configure JMX exporter on Cassandra nodes
- Import Cassandra dashboard
- Create custom alerts for:
- GC pause > 500ms
- Dropped mutations > 0
- Pending compactions > 20
Exercise 4: Backup and Restore
Task:- Create snapshot of
my_keyspace - Upload to S3 (or local directory)
- Drop table:
DROP TABLE my_keyspace.users; - Restore from snapshot
- Verify data integrity
Exercise 5: Troubleshoot Slow Queries
Scenario: Query taking 5 seconds Task:- Analyze trace output
- Identify bottleneck (tombstones? large partition? SSTable count?)
- Apply fix (repair? compaction? data model change?)
- Verify improvement
Summary & Production Best Practices
JVM:- Heap: 8GB max (25% of RAM)
- GC: G1GC with 200ms pause target
- Monitor GC logs continuously
- SSDs for all storage
- XFS with
noatime - Disable THP and swap
- I/O scheduler: noop
- Compaction strategy matches workload
- Run repair weekly (within gc_grace_seconds)
- Use LOCAL_QUORUM for multi-DC
- Monitor: GC, tpstats, compaction, latency
- 1 core per 1-2TB data
- 64GB+ RAM (8GB heap + 48GB OS cache)
- Disk: 50% headroom for compaction
- Network: 10 Gbps minimum
- Daily snapshots to external storage
- Incremental backups enabled
- Test restore procedures regularly
- High reads → Check SSTable count, tombstones
- High writes → Check pending compactions, GC
- Timeouts → Check dropped messages, disk I/O
- Node down → Check failure detector, GC pauses
What’s Next?
Module 7: Capstone Project - Building a Production System
Apply everything you’ve learned to design and implement a real-world Cassandra application at scale