Skip to content

RedisHash Crud Repositories where the Id is a hash-tag #3260

@jamie-teqplay

Description

@jamie-teqplay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions