4

I have a java application running as a microservice that receives events for keys that expired in Redis (Redis is configured with "config set notify-keyspace-events Ex"). These events are read in the application using a MessageListener.

My problem is that when multiple versions of the application are running all instances receive the Redis notification and perform the same action. But this is not my intent, only one action must be performed. In my case, each key that expires in Redis must be logged once in the database.

Is there a way to configure Redis to send the event only to one instance of the same microservice? Or, the event sent by Redis can be read by multiple listeners of the multiple instances of the microservice and then can I imprement something smart, maybe using RabbitMq, to generate only one message that is processed only once?

Rodolfo
  • 149
  • 1
  • 9

4 Answers4

0

I think event listeners should also use another message queue rather than Redis.

WonChul Heo
  • 242
  • 1
  • 12
0

I think Redis Pub/Sub system can not support this.

My workaround is store the <expire_key, value> in another Redis Sets/Hashes, when a client receive the expired event, it start a transaction, get and delete the expire_key, then do my custom callback action depend whether the expire_key exists.

menya
  • 1,459
  • 7
  • 8
0

You will probably need to implement your own Redis Module that will listen on key space notification and push to Redis stream.

Redis Streams can be consumed using consumers group , that does exactly what you want.

EDIT: Reliable keyspace notification can be easily achieved using RedisGears see: Can redis key space notifications be pushed to the redis stream instead of pub/sub channel

Guy Korland
  • 9,139
  • 14
  • 59
  • 106
0
@EnableRedisHttpSession(redisNamespace = "spring:session:oauth2")

You can try this for each service with different redisNamespace.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36