nodetool invalidatecountercache
Invalidates the counter cache on the node.
Synopsis
Section titled “Synopsis”nodetool [connection_options] invalidatecountercacheSee connection options for connection options.
Description
Section titled “Description”nodetool invalidatecountercache clears all entries from the counter cache on the node. The counter cache stores counter cell values, allowing faster access to frequently read counters by avoiding disk reads for their current values.
After invalidation, the cache is empty and will be repopulated as subsequent counter read operations occur.
Counter-Specific Cache
This cache only affects counter columns. If the workload does not use counters, this cache will be empty and invalidation has no effect.
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”nodetool invalidatecountercacheInvalidate and Monitor Refill
Section titled “Invalidate and Monitor Refill”nodetool invalidatecountercachewatch -n 5 'nodetool info | grep "Counter Cache"'Counter Cache Overview
Section titled “Counter Cache Overview”What the Counter Cache Stores
Section titled “What the Counter Cache Stores”| Cached Data | Description |
|---|---|
| Counter cell location | Reference to counter column |
| Current counter value | Most recent counter value |
| Merge information | Data for counter merges |
How It Improves Performance
Section titled “How It Improves Performance”Without Counter Cache: Counter Read → Read all counter shards from disk → Merge → Return value
With Counter Cache Hit: Counter Read → Return cached merged value (Avoids disk reads and merge computation)Counter Operations
Section titled “Counter Operations”Counters in Cassandra are distributed and require merging values from multiple sources:
- Local shard value
- Remote shard values
- Historical values for consistency
The counter cache stores the merged result, avoiding repeated computation.
When to Use
Section titled “When to Use”After Counter Repair
Section titled “After Counter Repair”Clear potentially inconsistent cached values:
# After running repair on counter tablesnodetool invalidatecountercacheTroubleshooting Counter Discrepancies
Section titled “Troubleshooting Counter Discrepancies”If counter values appear incorrect:
# Clear counter cachenodetool invalidatecountercache
# Re-read counters to get fresh valuesAfter Counter Table Restore
Section titled “After Counter Table Restore”Following restore from backup:
# After restoring counter datanodetool invalidatecountercacheMemory Optimization
Section titled “Memory Optimization”Release memory if counter cache is large:
# Check current sizenodetool info | grep "Counter Cache"
# Clear if necessarynodetool invalidatecountercacheImpact Assessment
Section titled “Impact Assessment”Immediate Effects
Section titled “Immediate Effects”| Aspect | Impact |
|---|---|
| Counter cache entries | All cleared |
| Memory usage | Temporarily reduced |
| Counter read latency | Temporarily increases |
| Disk I/O for counters | Increases |
Recovery Timeline
Section titled “Recovery Timeline”| Phase | Duration | Cache State |
|---|---|---|
| Immediately after | 0 | Empty |
| Warming up | Minutes | Partially filled |
| Fully warm | Hours | Optimally filled |
Impact Scope
Impact is limited to counter column workloads. Non-counter reads are unaffected.
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 "Counter Cache"
# Invalidatenodetool invalidatecountercache
# Check immediately afterecho "After invalidation:"nodetool info | grep -A 5 "Counter Cache"Monitor Refill Progress
Section titled “Monitor Refill Progress”#!/bin/bashecho "=== Counter Cache Refill Monitor ==="echo "Press Ctrl+C to stop"echo ""
while true; do info=$(nodetool info 2>/dev/null | grep -A 2 "Counter 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 10doneWorkflow: Counter Cache Management
Section titled “Workflow: Counter Cache Management”#!/bin/bashecho "=== Counter Cache Management ==="
# 1. Check current stateecho "1. Current counter cache status:"nodetool info | grep -A 3 "Counter Cache"
# 2. Check if counters are in useentries=$(nodetool info 2>/dev/null | grep -A 1 "Counter Cache" | grep "entries" | awk '{print $4}')if [ "$entries" = "0" ]; then echo "" echo "Counter cache is empty - no counters in use or caching disabled." echo "Invalidation would have no effect." exit 0fi
# 3. Proceed with invalidationecho ""echo "2. Invalidating counter cache..."nodetool invalidatecountercache
# 4. Verifyecho ""echo "3. Cache status after invalidation:"nodetool info | grep -A 3 "Counter Cache"
# 5. Monitor refillecho ""echo "4. Monitoring refill (30 seconds)..."for i in {1..6}; do sleep 5 entries=$(nodetool info 2>/dev/null | grep -A 1 "Counter Cache" | grep "entries" | awk '{print $4}') echo " Entries: $entries"done
echo ""echo "=== Complete ==="Cluster-Wide Operations
Section titled “Cluster-Wide Operations”Invalidate on All Nodes
Section titled “Invalidate on All Nodes”#!/bin/bashecho "Invalidating counter cache cluster-wide..."# 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 invalidatecountercache 2>/dev/null && echo "invalidated" || echo "FAILED""done
echo ""echo "Monitor recovery with: nodetool info | grep 'Counter Cache'"Rolling Invalidation
Section titled “Rolling Invalidation”#!/bin/bash# rolling_countercache_invalidate.sh# Get list of node IPs from local nodetool status
nodes=$(nodetool status | grep "^UN" | awk '{print $2}')WAIT_TIME=120 # 2 minutes between nodes
for node in $nodes; do echo "$(date): Invalidating counter cache on $node..." ssh "$node" "nodetool invalidatecountercache"
echo "Waiting ${WAIT_TIME}s for cache warmup..." sleep $WAIT_TIME
hit_rate=$(ssh "$node" "nodetool info 2>/dev/null | grep -A 2 "Counter Cache" | grep "hit rate" | awk '{print $4}')" echo " Hit rate: $hit_rate"done
echo "Rolling invalidation complete."Configuration
Section titled “Configuration”Counter Cache Settings
Section titled “Counter Cache Settings”counter_cache_size_in_mb: # Default: min(2.5% of heap, 50MB)counter_cache_save_period: 7200 # Save interval in secondscounter_cache_keys_to_save: # Keys to save on shutdownCheck Configuration
Section titled “Check Configuration”# View current counter cache configurationnodetool info | grep "Counter Cache"
# Check cassandra.yamlgrep counter_cache /etc/cassandra/cassandra.yamlTroubleshooting
Section titled “Troubleshooting”Counter Cache Always Empty
Section titled “Counter Cache Always Empty”# Check if any tables use counterscqlsh -e "SELECT keyspace_name, table_name FROM system_schema.columns WHERE type = 'counter' ALLOW FILTERING;"
# If no counter tables, cache will be emptyLow Hit Rate
Section titled “Low Hit Rate”# Check cache size vs counter working setnodetool info | grep "Counter Cache"
# Consider increasing cache size if entries are at capacity# but hit rate is lowCounter Values Incorrect After Invalidation
Section titled “Counter Values Incorrect After Invalidation”# Run repair on counter tablesnodetool repair -pr my_keyspace counter_table
# Then allow cache to rebuild with correct valuesCounter Best Practices
Section titled “Counter Best Practices”Counter Cache Guidelines
- Understand counter workload - Only useful if using counter columns
- Invalidate after repair - Ensure cached values are consistent
- Monitor hit rates - Low hit rate may indicate sizing issue
- Consider alternatives - For simple counting, regular columns may be better
- Test impact - Measure latency impact before production invalidation
Counter Limitations
Remember that Cassandra counters have specific limitations:
- Cannot be part of primary key
- Can only increment/decrement (no set)
- Subject to consistency challenges
- Counter cache helps mitigate read overhead
Related Commands
Section titled “Related Commands”| Command | Relationship |
|---|---|
| invalidatekeycache | Invalidate key cache |
| invalidaterowcache | Invalidate row cache |
| info | View cache statistics |
| setcachecapacity | Adjust cache sizes |
| repair | Repair data including counters |