micronaut-projects / micronaut-redis

Integration between Micronaut and Redis
Apache License 2.0
30 stars 35 forks source link

Use SCAN instead of KEYS for invalidateAll implementation #543

Closed Mike-Svendsen-IL closed 2 months ago

Mike-Svendsen-IL commented 3 months ago

Feature description

The current invalidateAll implementation uses the redis KEYS command to find the list of keys to invalidate.

The official redis documentation on KEYS (https://redis.io/docs/latest/commands/keys/) specifically recommends not using it in production due to its blocking nature.

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

In addition, AWS Serverless Elasticache specifically disables the KEYS command (https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/SupportedCommands.html), most likely due to the above performance concerns. This results in the invalidateAll commands failing in a Serverless Elasticache cluster.

The Redis documentation recommends using a SCAN instead to find all of the keys.

Mike-Svendsen-IL commented 3 months ago

I'm able to work on a PR for this assuming others agree with the change.

graemerocher commented 3 months ago

sounds good. PRs welcome