I’m using Redis as nosql database (and really enjoying it) but there are some cases where it’s not the more handy (I know that not every data model can be used with nosql, but still…).
I’d like to know if there is a proper way to perform the following in Redis or if I definitively need to go for a RDMS instead.
Let’s say I have 2 objets:
- User
- Asset
an Asset can be either a “flat”, a “building” or a “house”.
I use a redis list named “assets” that gathers the assets like:
assets = [asset:1, asset:2, ....]
Each assets is a hash like:
asset:1 = {type: flat, value: 150000$}
How can I get the list of all the assets of type “flat” ?
The easiest way would be to iterate through the list of assets and check the type of each one, that’s linear so that’s seems quite good.
Is there a better solution ?
It could be usefull to have a new list: “flats” that would contains the references of the assets with type “flat” but this news list would cause an overhead as we will need to be sure it’s synchronized.
flats = [asset:1, asset:5, ...]
But this lead to data replication… which is bad… Is this still the way to go with nosql ?
Any other suggestions ?
I usually build a second key that contains an index. So, when you write a new asset, you write it to your set of assets and to your set of specific asset types. It’s overhead, but it’s the same overhead you’d experience in other databases, it’s just up front. Indexing always requires an additional write, Redis just makes it really obvious that this is happening.
Also, I would just store the id in your “assets” set, rather than asset:1, just store 1