4

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.

Juzer Ali
  • 4,109
  • 3
  • 35
  • 62

2 Answers2

4

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.

spring-session@1.1.0.RELEASE 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.

Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
  • 1
    Can anyone confirm that setting spring.session.redis.namespace in application.yml works? I cannot seem to get this set dynamically at runtime, which I need. – Marc Zampetti Jul 15 '16 at 16:51
1

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!!

Juzer Ali
  • 4,109
  • 3
  • 35
  • 62
Avnish
  • 1,241
  • 11
  • 19