nodetool setcachecapacity
Sets the capacity of the key cache, row cache, and counter cache.
Synopsis
Section titled “Synopsis”nodetool [connection_options] setcachecapacity <key-cache-capacity> <row-cache-capacity> <counter-cache-capacity>See connection options for connection options.
Description
Section titled “Description”nodetool setcachecapacity adjusts the maximum size of Cassandra's three main caches at runtime. This allows tuning cache sizes without restarting the node, useful for responding to memory pressure or optimizing for workload changes.
Runtime Change
Cache capacity changes take effect immediately but don't persist across restarts. For permanent changes, update cassandra.yaml.
Arguments
Section titled “Arguments”| Argument | Description |
|---|---|
key-cache-capacity | Maximum size of key cache in MB |
row-cache-capacity | Maximum size of row cache in MB |
counter-cache-capacity | Maximum size of counter cache in MB |
Examples
Section titled “Examples”Set All Cache Sizes
Section titled “Set All Cache Sizes”# Set key cache to 100MB, row cache to 0MB (disabled), counter cache to 50MBnodetool setcachecapacity 100 0 50Increase Key Cache
Section titled “Increase Key Cache”# Increase key cache while keeping others unchanged# First check current valuesnodetool info | grep -E "Key Cache|Row Cache|Counter Cache"
# Then set new valuesnodetool setcachecapacity 200 0 50Disable Row Cache
Section titled “Disable Row Cache”# Disable row cache by setting to 0nodetool setcachecapacity 100 0 50Cache Types
Section titled “Cache Types”Key Cache
Section titled “Key Cache”| Aspect | Description |
|---|---|
| Purpose | Caches partition key locations in SSTables |
| Benefit | Reduces partition index disk reads |
| Default | 5% of heap (capped) |
| Impact of size | Larger = more partitions cached |
Row Cache
Section titled “Row Cache”| Aspect | Description |
|---|---|
| Purpose | Caches complete partition data |
| Benefit | Eliminates disk I/O for cached partitions |
| Default | 0 (disabled) |
| Impact of size | High memory per entry, JVM pressure |
Counter Cache
Section titled “Counter Cache”| Aspect | Description |
|---|---|
| Purpose | Caches counter values |
| Benefit | Reduces counter read overhead |
| Default | min(2.5% of heap, 50MB) |
| Impact of size | Only useful with counter columns |
When to Use
Section titled “When to Use”Memory Pressure
Section titled “Memory Pressure”Reduce cache sizes during memory emergencies:
# Reduce all caches significantlynodetool setcachecapacity 50 0 25
# Monitor memorywatch -n 5 'nodetool info | grep -E "Heap|Cache"'Performance Tuning
Section titled “Performance Tuning”Increase key cache for read-heavy workloads:
# Check current hit ratesnodetool info | grep "Key Cache"
# If hit rate is low and entries at capacity, increase sizenodetool setcachecapacity 256 0 50Disable Unused Caches
Section titled “Disable Unused Caches”Free memory from unused caches:
# No counter columns? Disable counter cachenodetool setcachecapacity 100 0 0Testing Different Configurations
Section titled “Testing Different Configurations”Experiment with cache sizes without restart:
# Test larger key cachenodetool setcachecapacity 200 0 50
# Run workload, measure metrics
# Try different sizenodetool setcachecapacity 300 0 50Workflow: Cache Optimization
Section titled “Workflow: Cache Optimization”#!/bin/bashecho "=== Cache Optimization Workflow ==="
# 1. Current stateecho "1. Current cache statistics:"nodetool info | grep -E "Key Cache|Row Cache|Counter Cache" -A 1
# 2. Analyze hit ratesecho ""echo "2. Analyzing hit rates..."key_hit=$(nodetool info | grep "Key Cache" -A 1 | grep "hit rate" | awk '{print $4}')key_entries=$(nodetool info | grep "Key Cache" -A 1 | grep "entries" | awk '{print $4}')key_capacity=$(nodetool info | grep "Key Cache" -A 1 | grep "capacity" | awk '{print $4}')
echo " Key Cache: Hit Rate=$key_hit, Entries=$key_entries, Capacity=$key_capacity"
# 3. Recommendationsecho ""echo "3. Recommendations:"
# Parse hit rate (remove % if present)hit_rate_num=$(echo $key_hit | tr -d '%')
if [ "$key_entries" = "$key_capacity" ] && [ "$hit_rate_num" -lt 90 ]; then echo " - Key cache at capacity with low hit rate. Consider increasing."fi
# 4. Ask for actionecho ""read -p "Enter new cache sizes (key row counter) or 'skip': " sizes
if [ "$sizes" != "skip" ]; then echo "" echo "4. Applying new cache sizes..." nodetool setcachecapacity $sizes
echo "" echo "5. New cache configuration:" nodetool info | grep -E "Key Cache|Row Cache|Counter Cache" -A 1fiImpact Assessment
Section titled “Impact Assessment”Increasing Cache Size
Section titled “Increasing Cache Size”| Effect | Impact |
|---|---|
| Memory usage | Increases |
| Hit rate | May improve |
| GC pressure | Increases (especially row cache) |
| Read performance | May improve |
Decreasing Cache Size
Section titled “Decreasing Cache Size”| Effect | Impact |
|---|---|
| Memory usage | Decreases |
| Hit rate | May decrease |
| Evictions | Increase |
| Read latency | May increase |
Monitoring After Change
Section titled “Monitoring After Change”Watch Cache Metrics
Section titled “Watch Cache Metrics”# Monitor cache behavior after changewatch -n 10 'nodetool info | grep -E "Key Cache|Row Cache|Counter Cache" -A 1'Track Memory
Section titled “Track Memory”# Monitor heap usagewatch -n 10 'nodetool info | grep "Heap Memory"'Verify Hit Rates
Section titled “Verify Hit Rates”#!/bin/bashecho "Monitoring cache after size change..."echo "Press Ctrl+C to stop"echo ""
while true; do key_hit=$(nodetool info 2>/dev/null | grep "Key Cache" -A 1 | grep "hit rate" | awk '{print $4}') key_size=$(nodetool info 2>/dev/null | grep "Key Cache" -A 1 | grep "size" | awk '{print $4}') counter_hit=$(nodetool info 2>/dev/null | grep "Counter Cache" -A 1 | grep "hit rate" | awk '{print $4}')
echo "$(date '+%H:%M:%S') - Key: $key_hit ($key_size), Counter: $counter_hit" sleep 30doneCluster-Wide Changes
Section titled “Cluster-Wide Changes”Set on All Nodes
Section titled “Set on All Nodes”#!/bin/bashKEY_CACHE=$1ROW_CACHE=$2COUNTER_CACHE=$3
if [ -z "$KEY_CACHE" ] || [ -z "$ROW_CACHE" ] || [ -z "$COUNTER_CACHE" ]; then echo "Usage: $0 <key_cache_mb> <row_cache_mb> <counter_cache_mb>" exit 1fi
echo "Setting cache capacity cluster-wide..."echo "Key: ${KEY_CACHE}MB, Row: ${ROW_CACHE}MB, Counter: ${COUNTER_CACHE}MB"
# 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 setcachecapacity $KEY_CACHE $ROW_CACHE $COUNTER_CACHE" && echo "OK" || echo "FAILED"doneConfiguration Persistence
Section titled “Configuration Persistence”Runtime vs Permanent
Section titled “Runtime vs Permanent”| Setting Source | Persistence | Scope |
|---|---|---|
setcachecapacity | Until restart | This node only |
cassandra.yaml | Permanent | All restarts |
Making Changes Permanent
Section titled “Making Changes Permanent”# cassandra.yaml (4.1+ data size format)key_cache_size: 200MiBrow_cache_size: 0MiBcounter_cache_size: 50MiB
# Pre-4.1# key_cache_size_in_mb: 200# row_cache_size_in_mb: 0# counter_cache_size_in_mb: 50After editing, changes apply on next restart.
Troubleshooting
Section titled “Troubleshooting”Command Fails
Section titled “Command Fails”# Check JMX connectivitynodetool info
# Verify arguments are valid numbersMemory Issues After Increase
Section titled “Memory Issues After Increase”# If OOM or GC issues after increase# Reduce cache sizesnodetool setcachecapacity 50 0 25
# Or check heap usagenodetool gcstatsCache Not Filling After Increase
Section titled “Cache Not Filling After Increase”# Cache fills based on workload# Wait for reads to populate cache
# Verify caching is enabled on tablescqlsh -e "SELECT table_name, caching FROM system_schema.tables WHERE keyspace_name = 'my_keyspace';"Best Practices
Section titled “Best Practices”Cache Sizing Guidelines
- Start conservative - Begin with defaults, increase based on metrics
- Monitor hit rates - Low hit rate with full cache suggests sizing issue
- Consider heap - Caches compete with application for heap
- Row cache caution - High memory per entry, GC impact
- Update cassandra.yaml - Make permanent after testing
- Cluster consistency - Set same values across all nodes
General Recommendations
- Key cache: Let auto-sizing work (5% of heap), increase if hit rate low at capacity
- Row cache: Keep disabled (0) unless specific hot partition use case
- Counter cache: Default is usually sufficient unless heavy counter usage
Related Commands
Section titled “Related Commands”| Command | Relationship |
|---|---|
| invalidatekeycache | Clear key cache |
| invalidaterowcache | Clear row cache |
| invalidatecountercache | Clear counter cache |
| info | View cache statistics |