spring-data-redis icon indicating copy to clipboard operation
spring-data-redis copied to clipboard

Substitute/configure DEL for UNLINK on respository.save implementations

Open gjafachini opened this issue 3 years ago • 2 comments

On update intensive applications, where CrudRepository.save/saveAll is called many times on already existing keys, the current implementation uses DEL command before SET, and this puts some pressure on the DB since is blocking command. E.g. https://github.com/spring-projects/spring-data-redis/blob/485bcad1108cdcb250157dbbc1bdad8c7fcac4af/src/main/asciidoc/reference/redis-repositories.adoc#replace-existing

Specially on larger structures, when you need to update a value many times, this behavior put a lot of pressure on the DB. Each save on existing keys executes (setting TTL as well): "DEL" "key" "HMSET" "key" "_class" "something" "id" ... "EXPIRE" "key" "259200"

Another place where DEL is used that could be substituted by UNLINK is when cleaning the secondary indexes on the ExpirationListener: org.springframework.data.redis.core.RedisKeyValueAdapter.MappingExpirationListener#onMessage

I know from Redis 6 on we can configure to do DEL as UNLINK, however this would affect all applications. Would be useful to provide configuration for using DEL or UNLINK

The versions where this is observed: <spring.boot.version>2.6.2</spring.boot.version> <artifactId>redisson-spring-boot-starter</artifactId> <redisson.version>3.16.7</redisson.version>

gjafachini avatar Mar 30 '22 23:03 gjafachini

I want to contribute to this issue Can you give me a direction?

JunggiKim avatar Oct 30 '24 06:10 JunggiKim

Hi team, @mp911de

I'd like to contribute to this issue.

My proposed implementation approach:

  • Add a configuration property (e.g., spring.data.redis.use-unlink-for-cleanup=false) with DEL as default to maintain backward compatibility
  • Modify RedisKeyValueAdapter and related deletion logic to use the configured command
  • Update both the save operation cleanup and expiration listener cleanup
  • Add comprehensive tests covering both DEL and UNLINK scenarios

Would this approach work for you? I can start working on a draft PR if you're okay with this direction. Any specific implementation guidelines or patterns I should follow for this contribution?

Thanks!

kssumin avatar May 26 '25 03:05 kssumin