Search code examples
redis.net-6.0spring-data-redis

Redis + .NET 6 - Best data type for querying all entries and updating individual entries


I recently got to know Redis, integrated it into my project and now I am facing the following use case.

My question in short:
Which data type can I use to get all entries sorted AND to be able to overwrite single entries?

My question in long:
I have a huge amount of point cloud models that I want to store and work with via Redis. My point cloud model consists of three things:

  • Unique id (stays the same)
  • Point Cloud as a string (changes over time)
  • Priority as an integer (changes over time)

Basically I would like to be able to do only two things with Redis. However, if I understand the documentation correctly, these are seen as benefits of two different data types, so I can't find a data type that exactly fits my use case. I hope, however, that I am wrong about this and that someone here can help me.

Use case:

  • Get quick all models, all already sorted
  • Overwrite/update a specific model

Sorted Sets
enter image description here
Advantage

  • Get all entries in sorted order

  • my model property Priority can be used here as a score, which determines the order.

Disadvantage

  • No possibility to access a special value via a key and overwrite it.

Hashes:
enter image description here
Advantage

  • Overwrite specific entry via Key > Field
  • Get all entries via Key

Disadvantage

  • No sorting

Solution

  • I would suggest to just use two distinct data types:

    • a hash with all the properties of your model, with the exception of the priority;
    • a sorted set which allows to easily sort your collection and deal with the scores / priorities.

    You could then link the two by storing each hash key (or a distinctive value which allows to reconstruct the final hash key) as the related sorted set member.

    For example:

    > HSET point-cloud:123 foo bar baz suppiej
    > ZADD point-clouds-by-priority 42 point-cloud:123
    

    You will keep all the advantages you mentioned, with no disadvantages at all.