Spring Session With Redis

Spring Session:  

Spring Session provides an API and implementations for managing a user’s session information. It also provides transparent integration with HttpSession and WebSocket . For more info. Please refer this slide click here


Redis : 

Redis is an advanced key-value store. It is similar to memcached but the dataset is not volatile, and values can be strings, exactly like in memcached, but also lists, sets, and ordered sets. All this data types can be manipulated with atomic operations to push/pop elements, add/remove elements, perform server side union, intersection, difference between sets, and so forth. Redis supports different kind of sorting abilities.

Installation : https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis


Download the sample code from here

GOAL:  Replace the default HttpSession implementation by Spring Session


  • Spring session can be implemented using two methods – Java Based Configuration and XML based configuration.
  • XML based configuration is used.

Steps to Implement :

  1. Update pom.xml : add these jars in the pom.
    1. spring-session : To enable spring session
    2. spring-session-data-redis : Provide libraries to connect web and REDIS DB

  2. Update web.xml
    1. Add Spring Session Filter Chain –  Ensures that our Servlet Container (i.e. Tomcat) uses our springSessionRepositoryFilter for every request. The DelegatingFilterProxy will look up a Bean by the name of springSessionRepositoryFilter and cast it to a Filter. For every request that DelegatingFilterProxy is invoked, the springSessionRepositoryFilter will be invoked.
  3. Add session context file
    1. Create a session.xml context file.
      <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
      <bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/>
    2. Terms:
      1. RedisHttpSessionConfiguration: We use the combination of <context:annotation-config/> and  RedisHttpSessionConfiguration because Spring Session does not yet provide XML Namespace support.This creates a Spring Bean with the name of springSessionRepositoryFilter that implements Filter. The filter is what is in charge of replacing the HttpSession implementation to be backed by Spring Session. In this instance Spring Session is backed by Redis.
      2. JedisConnectionFactory: We create a RedisConnectionFactory that connects Spring Session to the Redis Server. We configure the connection to connect to localhost on the default port (6379) For more information on configuring Spring Data Redis, refer to the reference documentation.


Some Useful Redis Commands

S. No Command Description Sample Output
1. redis-cli To enter into redis console ok
2. keys * Shows all keys stored in redis DB 1) “spring:session:expirations:14403548400002) “spring:session:sessions:3b606f6d-3d30-4afb-bea6-ef3a4adcf56b
3. monitor To check all the values stored inside a session ok
4. hgetall SESSION_ID To check all the keys stored inside a session eg . hgetall spring:session:sessions:3b606f6d-3d30-4afb-bea6-ef3a4adcf56b
5. FLUSHALL Remove all keys from the DB ok

Reference :


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s