-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
Description:
I’m trying to use Spring Data Redis @RedisHash repositories where the entity ID contains a hash-tag (e.g., user:{UUID}) to control key slotting in Redis Cluster. The goal is to leverage Redis Cluster hash-tags for key distribution to for implementation to leverage Redis locking that need these keys on the same redis node, but I can’t find any guidance or examples on how to achieve this with Spring Data Redis repositories.
What I’ve Tried:
I searched a lot for solutions the one thing i could find in official documentation is configuring a KeyspaceConfiguration
but here i could not find a solution to use a field from the data class in the key
i also could not get it to overwrite the default key from the @redisHash annotation
the only sollution i found is to manual create all the operations as an example
private fun key(id: Int): String = "$applicationName:$store:{$id}"
fun save(entity: StopState): StopState {
val k = key(entity.mmsi)
println("Saving hash key: $k")
val map: Map<String, Any?> = objectMapper.convertValue(entity)
// Write all fields to hash
redisTemplate.opsForHash<String, Any>().putAll(k, map.filterValues { it != null })
return entity
}but i really want to rely on the Spring data Repositories for ease of development
Problem:
CRUD operations don’t seem to respect the hash-tag formatting in the ID, or I can’t find a way to enforce it consistently for Redis Cluster slotting.
Question:
Is it possible to use hash-tags in entity IDs with Spring Data Redis CrudRepository?
Are there any recommended approaches or workarounds for this scenario?
Environment:
Spring Boot version: 3.4.6