nodetool invalidatekeycache
Invalidates the key cache on the node.
Synopsis
Section titled “Synopsis”nodetool [connection_options] invalidatekeycacheSee connection options for connection options.
Description
Section titled “Description”nodetool invalidatekeycache clears all entries from the key cache on the node. The key cache stores partition index entries, allowing Cassandra to skip disk seeks when locating partitions within SSTables.
After invalidation, the cache is empty and will be repopulated as subsequent read operations occur. This causes a temporary performance impact until the cache is warm again.
Performance Impact
Invalidating the key cache causes temporary read latency increases as cached partition locations must be re-read from disk. Use during maintenance windows when possible.
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”nodetool invalidatekeycacheInvalidate and Monitor Refill
Section titled “Invalidate and Monitor Refill”nodetool invalidatekeycachewatch -n 5 'nodetool info | grep "Key Cache"'Key Cache Overview
Section titled “Key Cache Overview”What the Key Cache Stores
Section titled “What the Key Cache Stores”The key cache stores partition key to SSTable position mappings:
| Cached Data | Purpose |
|---|---|
| Partition key hash | Identify the partition |
| SSTable file reference | Which SSTable contains the data |
| Position offset | Byte offset within the SSTable |
How It Improves Performance
Section titled “How It Improves Performance”Without Key Cache: Read Request → Bloom Filter → Partition Index (disk) → Data (disk)
With Key Cache Hit: Read Request → Key Cache → Data (disk) (Skips partition index read)When to Use
Section titled “When to Use”After Schema Changes
Section titled “After Schema Changes”Clear stale cache entries after significant schema modifications:
# After ALTER TABLE operations that affect partition structurenodetool invalidatekeycacheTroubleshooting Stale Data
Section titled “Troubleshooting Stale Data”If cached data may be causing issues:
# Clear potentially stale cachenodetool invalidatekeycache
# Verify issue resolutionMemory Pressure
Section titled “Memory Pressure”During memory emergencies (though reducing cache size may be better):
# Immediate memory releasenodetool invalidatekeycacheBefore Performance Testing
Section titled “Before Performance Testing”Ensure cold-cache baseline measurements:
# Clear all caches for baseline testnodetool invalidatekeycachenodetool invalidaterowcachenodetool invalidatecountercache
# Run performance testsImpact Assessment
Section titled “Impact Assessment”Immediate Effects
Section titled “Immediate Effects”| Aspect | Impact |
|---|---|
| Key cache entries | All cleared |
| Memory usage | Temporarily reduced |
| Disk I/O | Increases (partition index reads) |
| Read latency | Temporarily increases |
Recovery Timeline
Section titled “Recovery Timeline”| Phase | Duration | Cache State |
|---|---|---|
| Immediately after | 0 | Empty |
| Warming up | Minutes to hours | Partially filled |
| Fully warm | Hours (workload dependent) | Optimally filled |
Monitoring Cache Status
Section titled “Monitoring Cache Status”Before and After
Section titled “Before and After”# Check cache status beforeecho "Before invalidation:"nodetool info | grep -A 5 "Key Cache"
# Invalidatenodetool invalidatekeycache
# Check immediately afterecho "After invalidation:"nodetool info | grep -A 5 "Key Cache"Monitor Refill Progress
Section titled “Monitor Refill Progress”#!/bin/bashecho "=== Key Cache Refill Monitor ==="echo "Press Ctrl+C to stop"echo ""
while true; do info=$(nodetool info 2>/dev/null | grep -A 1 "Key Cache") entries=$(echo "$info" | grep "entries" | awk '{print $4}') hit_rate=$(echo "$info" | grep "hit rate" | awk '{print $4}') size=$(echo "$info" | grep "size" | awk '{print $4}')
echo "$(date '+%H:%M:%S') - Entries: $entries, Hit Rate: $hit_rate, Size: $size" sleep 10doneCache Statistics
Section titled “Cache Statistics”# Detailed cache statisticsnodetool info | grep -E "Key Cache|Row Cache|Counter Cache"Workflow: Controlled Cache Clear
Section titled “Workflow: Controlled Cache Clear”#!/bin/bashecho "=== Controlled Key Cache Clear ==="
# 1. Check current stateecho "1. Current key cache status:"nodetool info | grep -A 2 "Key Cache"
# 2. Record baseline metricsecho ""echo "2. Baseline read latencies:"nodetool proxyhistograms | head -20
# 3. Confirmecho ""read -p "Proceed with key cache invalidation? (y/n): " confirmif [ "$confirm" != "y" ]; then echo "Aborted." exit 1fi
# 4. Invalidateecho ""echo "4. Invalidating key cache..."nodetool invalidatekeycache
# 5. Verify invalidationecho ""echo "5. Cache status after invalidation:"nodetool info | grep -A 2 "Key Cache"
# 6. Monitor recoveryecho ""echo "6. Monitoring cache refill (30 seconds)..."for i in {1..6}; do sleep 5 entries=$(nodetool info | grep -A 1 "Key Cache" | grep "entries" | awk '{print $4}') echo " $(date '+%H:%M:%S') - Entries: $entries"done
echo ""echo "=== Complete ==="echo "Continue monitoring with: watch 'nodetool info | grep -A 2 \"Key Cache\"'"Cluster-Wide Operations
Section titled “Cluster-Wide Operations”Invalidate on All Nodes
Section titled “Invalidate on All Nodes”#!/bin/bashecho "WARNING: Invalidating key cache cluster-wide!"echo "This will cause temporary read latency increases."echo ""
# Get list of node IPs from local nodetool statusnodes=$(nodetool status | grep "^UN" | awk '{print $2}')
for node in $nodes; do echo -n "$node: " ssh "$node" "nodetool invalidatekeycache" 2>/dev/null && echo "invalidated" || echo "FAILED"done
echo ""echo "Monitor recovery with: nodetool info | grep 'Key Cache'"Rolling Invalidation
Section titled “Rolling Invalidation”For minimal impact, invalidate one node at a time:
#!/bin/bash# Get list of node IPs from local nodetool statusnodes=$(nodetool status | grep "^UN" | awk '{print $2}')WAIT_TIME=300 # 5 minutes between nodes
for node in $nodes; do echo "$(date): Invalidating key cache on $node..." ssh "$node" "nodetool invalidatekeycache"
echo "Waiting ${WAIT_TIME}s for cache to warm..." sleep $WAIT_TIME
# Check hit rate before proceeding hit_rate=$(ssh "$node" "nodetool info" 2>/dev/null | grep -A 1 "Key Cache" | grep "hit rate" | awk '{print $4}') echo " Hit rate: $hit_rate"done
echo "Rolling invalidation complete."Configuration
Section titled “Configuration”Key Cache Size
Section titled “Key Cache Size”key_cache_size_in_mb: # Leave empty for auto (5% of heap)key_cache_save_period: 14400 # Save interval in secondskey_cache_keys_to_save: # Number of keys to save (empty = all)Adjust Cache Size
Section titled “Adjust Cache Size”# Check current sizenodetool info | grep "Key Cache"
# If cache is too small/large, adjust in cassandra.yaml and restart# Or consider per-table caching settingsTroubleshooting
Section titled “Troubleshooting”Low Hit Rate After Warmup
Section titled “Low Hit Rate After Warmup”# Check cache size vs working setnodetool info | grep "Key Cache"
# If entries near capacity with low hit rate,# consider increasing cache sizeCache Not Refilling
Section titled “Cache Not Refilling”# Check if caching is enabled for tablesnodetool describecluster
# Check table-level cachingcqlsh -e "SELECT table_name, caching FROM system_schema.tables WHERE keyspace_name = 'my_keyspace';"Memory Issues
Section titled “Memory Issues”# If key cache is using too much memory# Consider reducing size in cassandra.yaml
# Check current usagenodetool info | grep "Key Cache" | grep "size"Best Practices
Section titled “Best Practices”Key Cache Guidelines
- Avoid frequent invalidation - Let cache warm naturally
- Use during maintenance windows - Minimize user impact
- Monitor after invalidation - Track cache refill and hit rates
- Consider rolling approach - Invalidate one node at a time
- Check cache sizing - Ensure cache is appropriately sized
- Document the reason - Record why cache was invalidated
When NOT to Invalidate
- During peak traffic hours
- Without understanding the performance impact
- Routinely (let cache manage itself)
- On all nodes simultaneously in production
Related Commands
Section titled “Related Commands”| Command | Relationship |
|---|---|
| invalidaterowcache | Invalidate row cache |
| invalidatecountercache | Invalidate counter cache |
| info | View cache statistics |
| setcachecapacity | Adjust cache sizes |