Skip to content

spring-session is saving session twice but spring-data-redis cannot guarantee to provide a proper result. #2631

Closed as not planned
@ravenadesk

Description

@ravenadesk

I am not sure if it is a bug, but I am facing the inconsistency between the two saves of spring session when using redis with replica.
About commitSession is called twice:
commitSession() called twice

In our project, we are using redis as the storage, there is one master and two replica.
When I was testing /health endpoint with a little load, sometimes the second save cannot get the right session status (taking it as not stored in redis) throwing IllegalStateException


spring-session/spring-session-data-redis/src/main/java/org/springframework/session/data/redis/RedisSessionRepository

	@Override
	public void save(RedisSession session) {
		if (!session.isNew) {
			String key = getSessionKey(session.hasChangedSessionId() ? session.originalSessionId : session.getId());
break point		Boolean sessionExists = this.sessionRedisOperations.hasKey(key);
			if (sessionExists == null || !sessionExists) {
				throw new IllegalStateException("Session was invalidated");
			}
		}
		session.save();
	}

I have tried adding a conditional break point at the line before if condition, and let it pause a little while when key does not exist. And it turns out no problem(key exists, no exception). I have also tried that remove all my replica of redis, and there is no problem at all.
So I am wondering is this twice save patten conflict against replica synchronization of redis.

Metadata

Metadata

Assignees

Labels

for: external-projectFor an external project and not something we can fix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions