apache/cassandra · error · RuntimeException

KeyCacheKeysToSave must be non-negative.

Error message

KeyCacheKeysToSave must be non-negative.

What it means

CacheService.setKeyCacheKeysToSave sets the maximum number of key-cache entries persisted per save; negative counts are rejected with an untyped RuntimeException before DatabaseDescriptor is updated and keyCache saving is rescheduled. Typically invoked via the CacheService JMX mbean.

Source

Thrown at src/java/org/apache/cassandra/service/CacheService.java:258

    }

    public void setRowCacheKeysToSave(int count)
    {
        if (count < 0)
            throw new RuntimeException("RowCacheKeysToSave must be non-negative.");
        DatabaseDescriptor.setRowCacheKeysToSave(count);
        rowCache.scheduleSaving(getRowCacheSavePeriodInSeconds(), count);
    }

    public int getKeyCacheKeysToSave()
    {
        return DatabaseDescriptor.getKeyCacheKeysToSave();
    }

    public void setKeyCacheKeysToSave(int count)
    {
        if (count < 0)
            throw new RuntimeException("KeyCacheKeysToSave must be non-negative.");
        DatabaseDescriptor.setKeyCacheKeysToSave(count);
        keyCache.scheduleSaving(getKeyCacheSavePeriodInSeconds(), count);
    }

    public int getCounterCacheKeysToSave()
    {
        return DatabaseDescriptor.getCounterCacheKeysToSave();
    }

    public void setCounterCacheKeysToSave(int count)
    {
        if (count < 0)
            throw new RuntimeException("CounterCacheKeysToSave must be non-negative.");
        DatabaseDescriptor.setCounterCacheKeysToSave(count);
        counterCache.scheduleSaving(getCounterCacheSavePeriodInSeconds(), count);
    }

    public void invalidateKeyCache()

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Supply a non-negative count; use 0 if the saving limit should be effectively disabled.
  2. Clamp the input with Math.max(0, count).
  3. Correct the upstream metric/calculation that produced the negative value.
  4. Add pre-call validation in any JMX automation that writes cache settings.

Example fix

// before
if (unlimited) mbean.setKeyCacheKeysToSave(-1);
// after
mbean.setKeyCacheKeysToSave(unlimited ? 0 : count);
Defensive patterns

Strategy: validation

Validate before calling

if (count < 0) throw new IllegalArgumentException("KeyCacheKeysToSave must be >= 0");
cacheService.setKeyCacheKeysToSave(count);

Prevention

When it happens

Trigger: Calling setKeyCacheKeysToSave(count) with count < 0 via JMX or internal callers.

Common situations: Automated tuning pipelines that derive KeyCacheKeysToSave from measurements which can go negative; manual JMX edits; assuming -1 means unlimited (it does not).

Related errors


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/0b2fcd33e3a2d29c. Report an issue: GitHub.