skip to Main Content

I am using redis for storing session in my Spring Boot app. I am also using Spring Session library to achieve this. I have two different redis instances namely US redis and EU redis. Once someone enters my app(after logging in of course), I want to store their sessions in both US redis and EU redis servers. Mind you that these both redis instances are masters and are not in any master-slave setup. This is what I have so far in my spring setup.

@EnableRedisHttpSession
@Configuration
public class RedisConfig{

}

Note: removed other security code for brevity.

According to @EnableRedisHttpSession docs, we need to add one RedisConnectionFactory instance, however Spring Boot by default does this.

So right now, once the user logs in, their session is stored in US redis(I added US redis related information in application.yml).

However whenever a session is stored in US redis, I want to replicate the same in EU redis server. How can I achieve this? Do I need to create another RedisConnectionFactory bean and manually save it? If manually save it, how to do so?

Note: This use case might not be following the best design practices(that is we are storing user session in multiple places). However I do understand this, but I do have a use case for this.

2

Answers


  1. There is a built in solution for that, but you can always tweak the spring code or as you wrote sent an explicit set to the remote Redis.

    You might want to consider using some Active-Active replication mechanism like CRDT

    Login or Signup to reply.
  2. Replication is done by the underlying connection. For example, if you are using Lettuce with Spring Session Redis, then you would set it up with a Master Slave setup. In Spring you can refer to Connecting to Redis section of the documentation.

    Spring Boot provides simple mechanism for setting up master/slave via properties. This article has a nice summary. In short, add the properties to application.yml

    redis:
      master:
        host: localhost
        port: 6379
      slaves:
        - host: localhost
          port: 16379
        - host: localhost
          port: 26379
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search