Skip to content

AxonOps — AI-Native Control Plane for Open Source Data Platforms

nodetool invalidatekeycache

Invalidates the key cache on the node.


Terminal window
nodetool [connection_options] invalidatekeycache

See connection options for connection options.

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.


Terminal window
nodetool invalidatekeycache
Terminal window
nodetool invalidatekeycache
watch -n 5 'nodetool info | grep "Key Cache"'

The key cache stores partition key to SSTable position mappings:

Cached DataPurpose
Partition key hashIdentify the partition
SSTable file referenceWhich SSTable contains the data
Position offsetByte offset within the SSTable
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)

Clear stale cache entries after significant schema modifications:

Terminal window
# After ALTER TABLE operations that affect partition structure
nodetool invalidatekeycache

If cached data may be causing issues:

Terminal window
# Clear potentially stale cache
nodetool invalidatekeycache
# Verify issue resolution

During memory emergencies (though reducing cache size may be better):

Terminal window
# Immediate memory release
nodetool invalidatekeycache

Ensure cold-cache baseline measurements:

Terminal window
# Clear all caches for baseline test
nodetool invalidatekeycache
nodetool invalidaterowcache
nodetool invalidatecountercache
# Run performance tests

AspectImpact
Key cache entriesAll cleared
Memory usageTemporarily reduced
Disk I/OIncreases (partition index reads)
Read latencyTemporarily increases
PhaseDurationCache State
Immediately after0Empty
Warming upMinutes to hoursPartially filled
Fully warmHours (workload dependent)Optimally filled

Terminal window
# Check cache status before
echo "Before invalidation:"
nodetool info | grep -A 5 "Key Cache"
# Invalidate
nodetool invalidatekeycache
# Check immediately after
echo "After invalidation:"
nodetool info | grep -A 5 "Key Cache"
monitor_keycache_refill.sh
#!/bin/bash
echo "=== 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 10
done
Terminal window
# Detailed cache statistics
nodetool info | grep -E "Key Cache|Row Cache|Counter Cache"

controlled_cache_clear.sh
#!/bin/bash
echo "=== Controlled Key Cache Clear ==="
# 1. Check current state
echo "1. Current key cache status:"
nodetool info | grep -A 2 "Key Cache"
# 2. Record baseline metrics
echo ""
echo "2. Baseline read latencies:"
nodetool proxyhistograms | head -20
# 3. Confirm
echo ""
read -p "Proceed with key cache invalidation? (y/n): " confirm
if [ "$confirm" != "y" ]; then
echo "Aborted."
exit 1
fi
# 4. Invalidate
echo ""
echo "4. Invalidating key cache..."
nodetool invalidatekeycache
# 5. Verify invalidation
echo ""
echo "5. Cache status after invalidation:"
nodetool info | grep -A 2 "Key Cache"
# 6. Monitor recovery
echo ""
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\"'"

invalidate_keycache_cluster.sh
#!/bin/bash
echo "WARNING: Invalidating key cache cluster-wide!"
echo "This will cause temporary read latency increases."
echo ""
# Get list of node IPs from local nodetool status
nodes=$(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'"

For minimal impact, invalidate one node at a time:

rolling_keycache_invalidate.sh
#!/bin/bash
# Get list of node IPs from local nodetool status
nodes=$(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."

cassandra.yaml
key_cache_size_in_mb: # Leave empty for auto (5% of heap)
key_cache_save_period: 14400 # Save interval in seconds
key_cache_keys_to_save: # Number of keys to save (empty = all)
Terminal window
# Check current size
nodetool info | grep "Key Cache"
# If cache is too small/large, adjust in cassandra.yaml and restart
# Or consider per-table caching settings

Terminal window
# Check cache size vs working set
nodetool info | grep "Key Cache"
# If entries near capacity with low hit rate,
# consider increasing cache size
Terminal window
# Check if caching is enabled for tables
nodetool describecluster
# Check table-level caching
cqlsh -e "SELECT table_name, caching FROM system_schema.tables WHERE keyspace_name = 'my_keyspace';"
Terminal window
# If key cache is using too much memory
# Consider reducing size in cassandra.yaml
# Check current usage
nodetool info | grep "Key Cache" | grep "size"

Key Cache Guidelines

  1. Avoid frequent invalidation - Let cache warm naturally
  2. Use during maintenance windows - Minimize user impact
  3. Monitor after invalidation - Track cache refill and hit rates
  4. Consider rolling approach - Invalidate one node at a time
  5. Check cache sizing - Ensure cache is appropriately sized
  6. 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

CommandRelationship
invalidaterowcacheInvalidate row cache
invalidatecountercacheInvalidate counter cache
infoView cache statistics
setcachecapacityAdjust cache sizes