5

I use redis as my cache server when i use springboot. But i read the spring-data-redis source, when evict cache, the code is

byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet())
                    .toArray(new byte[0][]);

Redis recommends replacing the keys command with the scan command. why does the spring team not do this.

hongwei
  • 71
  • 6
  • Very good question. The KEYS command does not scale, In our application cache eviction has become a bottleneck with KEYS taking 160ms and this blocks all other redis commands so cache evictions essentially block the entire distributed application cluster. – dan carter Aug 27 '20 at 00:59

1 Answers1

1

This functionality isn't available in the current released version (2.5.5) and more likely will be included in the next release (2.6.*)

The cache implementation defaults to use KEYS and DEL to clear the cache. KEYS can cause performance issues with large keyspaces. Therefore, the default RedisCacheWriter can be created with a BatchStrategy to switch to a SCAN-based batch strategy. The SCAN strategy requires a batch size to avoid excessive Redis command roundtrips:

RedisCacheManager cm = RedisCacheManager.build(RedisCacheWriter.nonLockingRedisCacheWriter(
  connectionFactory, 
  BatchStrategies.scan(1000))
).cacheDefaults(defaultCacheConfig())

Note: The KEYS batch strategy is fully supported using any driver and Redis operation mode (Standalone, Clustered). SCAN is fully supported when using the Lettuce driver. Jedis supports SCAN only in non-clustered modes.

https://github.com/spring-projects/spring-data-redis/blob/main/src/main/asciidoc/reference/redis-cache.adoc

Oleksandr Lykhonosov
  • 1,138
  • 12
  • 25