springgoogle-cloud-platformredisspring-data-redis

How to listen to keyspace events using Spring Data Redis with a GCP managed cluster?


I am using secondary indexes with Redis thanks to Spring Data Redis @Indexed annotations. My entry has a TTL. This has a side effect of keeping the indexes after the expiration of the main entry. This is expected, and Spring can listen to keyspace expiry events to remove those indexes once the main TTL is done.

However, enabling the listening to keyspace expiry events with Spring, I face the following error at startup:

ERR unknown command 'CONFIG'

This is how I configured the listener:

@EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP)

What can I do to make this work?


Solution

  • 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:

    1. Configure your MemoryStore on GCP, adding the notify-keyspace-events key with "Ex" as value.
    2. Use @EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP, keyspaceNotificationsConfigParameter = "") for your client configuration. The explicitely empty String prevents Spring from trying to override the remote configuration.