1

I found the following question (Spring Redis - Indexes not deleted after main entry expires) about a problem with expiration of indexes in Redis.

The problem is that the main and :phantom entries expire and are being deleted correctly, but the corresponding :idx entries survive orphaned in Redis.

One of the proposed solutions was to enable KeyspaceEvents, so that Redis automatically removes indexes of expired entries during the cleanup job.

Unfortunately this solution will not work for our Spring Boot application, as we are using Redis Enterprise as a provided service inside a cloud environment, which does not allow us to make any configuration changes (CONFIG command is disabled).

Here what I tried:

@Configuration
@EnableRedisRepositories(enableKeyspaceEvents = RedisKeyValueAdapter.EnableKeyspaceEvents.ON_STARTUP)
public class RedisConfiguration {...}

Edit:
I thought this was working for my local Redis docker image, but I was wrong! And on our provided Redis (Enterprise) service it can't even be setup with the following message:
Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'CONFIG'...

Can anybody give me a hint on how to get the indexes deleted?

We currently do not have many :idx entries, but they must/should be deleted together with the :phantom entry, to avoid keeping any 'orphaned' entries.

Thanks in advance.

NanSil
  • 156
  • 2
  • 10
  • AFAIK keyspace notifications are supported in the Enterprise version - it would be best if you could direct this question to support@redislabs.com with the details of the service provider and cloud environment that you're using. – Itamar Haber Jan 22 '18 at 17:12
  • Thanks, I definitively will write them! The point is that enabling the KeyspaceEvents requires the `CONFIG` command to be enabled, which is not the case for secured Redis environments... :-/ – NanSil Jan 23 '18 at 13:09

1 Answers1

5

I could find the solution to delete the keys :phantom and :idx.

In the Redis configuration class, the following should be put:

@Configuration
@EnableRedisRepositories(enableKeyspaceEvents = EnableKeyspaceEvents.ON_STARTUP, basePackages = {
    "com.aaaaa.bbbbb.persistence.model.repository" }, keyspaceNotificationsConfigParameter = "")

When you set the "keyspaceNotificationsConfigParameter" attribute to the empty string, the CONFIG command that does not work in AWS Redis is not executed, but in this way, the Expiration Event Listener is instantiated.

This attribute brings a default value (Ex), which causes the CONFIG command to be executed.

This happens by the following spring code:

public void init() {
    if (StringUtils.hasText(keyspaceNotificationsConfigParameter)) {
        RedisConnection connection = listenerContainer.getConnectionFactory().getConnection();

        try {
            Properties config = connection.getConfig("notify-keyspace-events");

            if (!StringUtils.hasText(config.getProperty("notify-keyspace-events"))) {
                connection.setConfig("notify-keyspace-events", keyspaceNotificationsConfigParameter);
            }

        } finally {
            connection.close();
        }
    }
    doRegister(listenerContainer);
}

How this condition is not met

if (StringUtils.hasText(keyspaceNotificationsConfigParameter)) {

the CONFIG command is not executed.

I think Spring should improve this, and not make that flow, based on setting an attribute with an empty string.

The only thing that is also needed is that in the AWS ElastiCache (Redis) a value is set to the "notify-keyspace-events" parameter, such as AKE, which means that all events will be notified.

  • I tried this. I am not getting error during startup But it didnt cleanup indexes created by spring-data-redis. Do you have any idea on how to fix this ? – Raashith Sep 05 '20 at 16:26