Whats the best one for table like data storage?

Whats the best one for table like data storage?

When I want to store tabular data in Redis, I usually use hashes. One hash for each row. The key is something that tells me what it is plus the primary key of the record. So, if I were storing customers my key might be something like:

> HMSET jennyco:customer:1234 name "Alice" acct_balance 1234.56 status "Gold"
> HMSET jennyco:customer:5678 name "Bob" acct_balance 78.90 status "Gold"

If I need to get a record and I have the customer id, I just go and get it:

> HGETALL jennyco:customer:1234

If you want to use another value as a key, you need to create you own index. You can use a set for each key and value and store the customer ids that match.

> SADD jennyco:customer:name:Alice 1234
> SADD jennyco:customer:name:Bob 5678
> SADD jennyco:customer:status:Gold 1234
> SADD jennyco:customer:status:Gold 5678

Now, if you want to find all the customers with Gold status, just use SSCAN or SMEMBERS (SSCAN is better) to fetch the customer ids. Then use the ids to fetch the hashes:

> SSCAN 0 jennyco:customer:status:Gold
> HGETALL jennyco:customer:1234
> HGETALL jennyco:customer:5678

Hope this helps!


Informative and detailed post around using hashes to store tabular data.

1 Like

It’s quite straightforward to map your table to Redis data structures. Hash, Sorted Set and Set are the most useful data structures in this effort. You could store every row as a Hash with a key that’s based on the table’s primary key, and have the key stored in a Set or a Sorted Set.

There is nice blog that help you to remodel your RDBMS data in Redis.

Remodel your RDBMS data in Redis

depends on your use case you also find RedisJSON or RediSearch as useful solution, as example RediSearh allows specific asks like sorting, aggregations etc.

RediSearch Documentation
RedisJSON documentation