You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BUG: Inconsistent TTL on Session Expiration Sets leading to increasing orphaned keys #3477
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:
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
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.
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.
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 == expireKeyStringexpirationsKey = getExpirationsKey(toExpire);
longsessionExpireInSeconds = session.getMaxInactiveInterval().getSeconds();
longfiveMinutesAfterExpires = 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 itthis.redis.boundSetOps(expirationsKey).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS);
// expireKey == expirationsKeyStringexpireKey = getExpirationKey(toExpire);
BoundSetOperations<String, Object> expireOperations = this.redis.boundSetOps(expireKey);
// Here we are finally adding the key but we are not calling the expireexpireOperations.add(keyToExpire);
}
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).
Activity
[-]Inconsistent TTL on Session Expiration Sets leading to increasing orphaned keys[/-][+]BUG: Inconsistent TTL on Session Expiration Sets leading to increasing orphaned keys[/+]PRAHLAD09-dev commentedon Jul 24, 2026
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.
PRAHLAD09-dev commentedon Aug 8, 2026
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.
jhonatanhulse commentedon Oct 29, 2026
@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
spring-session/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisIndexedSessionRepository.java
Lines 956 to 961 in e87be43
we can see that the method
createShadowKey()first adds the key by callingvalueOps.append("")and then it adds the TTL withvalueOps.expire(sessionExpireInSeconds, TimeUnit.SECONDS).The
save()method should do the same but it is not:To fix this bug the code should look like this:
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:
The last expire
EXPIRE spring:session:expirations:1439245080000 2100is not being executed.Fix TTL application to session expirations
junbrother93 commentedon Dec 2, 2026
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).