Skip to content

BUG: Inconsistent TTL on Session Expiration Sets leading to increasing orphaned keys #3477

Open

Description

@RoshanKumarChoudhary

The current implementation of MinuteBasedRedisSessionExpirationStore does not apply a “safety net” Time-To-Live (TTL) to the expiration tracking SET.

Looking at the save method, we can see that the line

this.redis.boundSetOps(expirationsKey).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);

does not set the expiry when the key is created for the first time. It only updates the TTL for subsequent keys that fall within the same minute.

I assume this behavior is intentional—to refresh the TTL for an already existing SET key. However, the issue is that when the SET is saved for the first time, it has no TTL applied, which can lead to orphaned keys accumulating over time.

The following snippet shows where the TTL should likely be added:

BoundSetOperations<String, Object> expireOperations = this.redis.boundSetOps(expireKey);
expireOperations.add(keyToExpire);

It seems that the following line is missing:

expireOperations.expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);

Could you please confirm if my understanding is correct and whether this could be a potential bug in the implementation?

Activity

  1. changed the title [-]Inconsistent TTL on Session Expiration Sets leading to increasing orphaned keys[/-] [+]BUG: Inconsistent TTL on Session Expiration Sets leading to increasing orphaned keys[/+] on Oct 15, 2025
  2. PRAHLAD09-dev commented on Jul 24, 2026

    @PRAHLAD09-dev

    I looked into this and can confirm the bug — I traced it in MinuteBasedRedisSessionExpirationStore.save(): the expirationsKey (wrapper key) gets .expire() called on it, but the expireKey's BoundSetOperations (the per-minute bucket holding actual session IDs) never does, in either the save() or remove() paths. So a newly created bucket has no TTL and can persist indefinitely if cleanupExpiredSessions() ever misses it. I'd like to send a PR adding .expire() right after expireOperations.add(keyToExpire) — I'll make sure it doesn't reset the TTL unnecessarily on every write to an existing bucket. Happy to also add a regression test. Let me know if this direction works.

  3. PRAHLAD09-dev commented on Aug 8, 2026

    @PRAHLAD09-dev

    I traced through this — getExpirationKey() in MinuteBasedRedisSessionExpirationStore delegates to lookupExpirationKey, which is wired to this::getExpirationsKey in the constructor. So expirationsKey and expireKey in save() resolve to the identical Redis key string — the .expire() call already covers the key used by .add(). I don't think this is a bug; happy to be corrected if I'm missing something.

  4. jhonatanhulse commented on Oct 29, 2026

    @jhonatanhulse

    @PRAHLAD09-dev @RoshanKumarChoudhary

    I can confirm the bug. The save() method is trying to expire a key that doesn't exist yet.

    If you look at

    else {
    BoundValueOperations<String, Object> valueOps = RedisIndexedSessionRepository.this.sessionRedisOperations
    .boundValueOps(sessionKey);
    valueOps.append("");
    valueOps.expire(sessionExpireInSeconds, TimeUnit.SECONDS);
    }

    we can see that the method createShadowKey() first adds the key by calling valueOps.append("") and then it adds the TTL with valueOps.expire(sessionExpireInSeconds, TimeUnit.SECONDS).

    The save() method should do the same but it is not:

    			// RedisIndexedSessionRepository.java line 1037
    			// expirationsKey == expireKey
    			String expirationsKey = getExpirationsKey(toExpire);
    			long sessionExpireInSeconds = session.getMaxInactiveInterval().getSeconds();
    			long fiveMinutesAfterExpires = sessionExpireInSeconds + TimeUnit.MINUTES.toSeconds(5);
    			// At this point the expirationsKey doesn't exist, but we are trying to expire it
    			// that is why it fails. We need to add the key first and later expire it
    			this.redis.boundSetOps(expirationsKey).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
    
    			// expireKey == expirationsKey
    			String expireKey = getExpirationKey(toExpire);
    			BoundSetOperations<String, Object> expireOperations = this.redis.boundSetOps(expireKey);
    			// Here we are finally adding the key but we are not calling the expire
    			expireOperations.add(keyToExpire);
    		}

    To fix this bug the code should look like this:

    			String expirationsKey = getExpirationKey(toExpire);
    			long sessionExpireInSeconds = session.getMaxInactiveInterval().getSeconds();
    			long fiveMinutesAfterExpires = sessionExpireInSeconds + TimeUnit.MINUTES.toSeconds(5);
    
    			BoundSetOperations<String, Object> setOps = this.redis.boundSetOps(expirationsKey);
    			setOps.add(keyToExpire);
    			setOps.expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
    		}

    The expected behavior described in https://docs.spring.io/spring-session/docs/current/api/org/springframework/session/data/redis/RedisIndexedSessionRepository.html confirms what I'm saying and the fix I'm suggesting:

    Image

    The last expire EXPIRE spring:session:expirations:1439245080000 2100 is not being executed.

  5. added a commit that references this issue on Oct 30, 2026
    9c19b07
  6. junbrother93 commented on Dec 2, 2026

    @junbrother93

    We hit the same issue on spring-session-data-redis 4.1.1 (Spring Boot 4, repository-type: indexed).

    In MinuteBasedRedisSessionExpirationStore#save, EXPIRE is sent to the
    expirations: key before SADD, so when the first session lands in a new
    minute bucket the EXPIRE is a no-op on a missing key and the SADD then creates
    the set without a TTL. (The class Javadoc of RedisIndexedSessionRepository
    documents SADD followed by EXPIRE.)

    On a long-running server the per-minute cleanup task deletes the bucket, so it
    is mostly invisible. On local dev servers that are stopped within the session
    timeout, the bucket is never cleaned up. We found expirations: and
    index:* keys with TTL -1 left in a shared Redis (20 orphaned keys across
    several developers' namespaces).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: waiting-for-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions