Juzer Ali
Juzer Ali

Reputation: 4167

Can multiple Spring sessions be backed by same redis instance?

We have a couple of web applications written on Java Spring, we are using spring-data-redis and @EnableRedisHttpSession. I was wondering what are the spring session internals. Would it check redis database for duplicate session keys before creating a new session?

I looked at spring documentation and also did a google search but couldn't get a definitive answer.

Upvotes: 5

Views: 3247

Answers (2)

Juzer Ali
Juzer Ali

Reputation: 4167

Found the solution after going through spring session project's github issues. Answer provided by @Avnish doesn't work because in cluster configuration redis does not provide databases, there is just a single database 0 and SELECT commands are not supported.

[email protected] solves this issue by providing session namespaces. If you are using @EnableRedisHttpSession annotation, you can add redisNamespace property to it. Or you can add the key in spring.session.redis.namespace property in your .properties or .yml file.

Upvotes: 5

Avnish
Avnish

Reputation: 1321

As far as as spring-session is concerned, it'll assume that another application is part of the cluster and will try to reuse existing session if found for given id, although very unlikely that two different applications will generate same session ids considering it's generated via random UUID. Following are the options that you can go with to safe guard yourself anyway.

If you are using spring boot, use different value of spring.redis.database property for each of your application (details here, search for "# REDIS")

If you are using spring-data-redis directly then you should be setting this value directly in the JedisConnectionFactory bean that you are using in your application. For XML configuration, following would do:

<bean id="jedisConnectionFactory" 
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  <property name="database" value="1" />
</beans>

Hope it helps!!

Upvotes: 1

Related Questions