Answering my own question, turns out there's no need to run lua script:
If using non-reactive Redis connection:
RedisConnection conn = null;
try {
conn = connectionFactory.getConnection();
conn.setConfig("notify-keyspace-events", "Ex");
} finally {
if (conn != null) {
conn.close();
}
}
If using reactive Redis connection:
ReactiveRedisConnection conn = connectionFactory.getReactiveConnection();
conn
.serverCommands()
.setConfig("notify-keyspace-events", "Ex")
.filter(status -> status.equals("OK"))
.doFinally(unused -> conn.close())
.block(Duration.ofSeconds(5L));
Answer from Abhijit Sarkar on Stack OverflowExpired key trigger event - Spring data Redis - Stack Overflow
java - notify-keyspace-events in Redis - Stack Overflow
Spring Session Redis web session expiration notifications not delayed until application is up again? - Stack Overflow
google cloud platform - How to listen to keyspace events using Spring Data Redis with a GCP managed cluster? - Stack Overflow
How to Enable keyspace notifications for Expired key Only?
How to Enable Using Redis-CLI?
Videos
If you have this issue:
io.lettuce.core.RedisCommandExecutionException: ERR Unsupported CONFIG parameter: notify-keyspace-events
then add following configuration class in your application:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
@Configuration
public class RedisConfigureAction {
@Bean
public ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
}
In my case - I had misconfigured lettuce client with client config for standalone server connecting to sentinel node. When I have changed client configuration of sentinel connecting to sentinel node exception went away.
FYI when using ConfigureRedisAction.NO_OP you have to have configure Redis redis.conf file with config set notify-keyspace-events Egx. See Redis documentation and default implementation of ConfigureRedisAction at Spring session source code.
This problem is linked to the fact that the Redis cluster is managed, and as such remote clients can't call CONFIG on it. When enabling the Spring keyspace event listener, it tries to configure Redis to emit keyspace expiry events, by setting the notify-keyspace-events config key to "Ex".
The workaround to this is:
- Configure your MemoryStore on GCP, adding the
notify-keyspace-eventskey with "Ex" as value. - Use
@EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP, keyspaceNotificationsConfigParameter = "")for your client configuration. The explicitely empty String prevents Spring from trying to override the remote configuration.
It is also important to remember that the listener must listen notifications from all master nodes.
Please see: https://github.com/spring-projects/spring-data-redis/issues/1782
Adding lettuce properties to connect with cluster solves problem. My config:
data:
redis:
password: your-password
ssl:
enabled: true
cluster:
nodes: node-address
lettuce:
cluster:
refresh:
dynamic-refresh-sources: false
period: PT1S
pool:
max-active: 15
max-wait: -1
max-idle: -1
min-idle: 5