andbi
andbi

Reputation: 4454

How interchangeable are Redis HASH Key and Field?

I'm planning to use Redis hashes as map of maps in the following way: [version ->[key -> serialized data]]

Application and data design suggest using Redis key as version and Redis field as key. In our case that would mean storing only a few dozens of Redis keys with tens of millions fields each.

Seems asymmetrical to the use case scenarios from Redis documentation. Is it okay in terms of performance for simple get/set/delete operations? No key expiration and other Redis advanced techniques will be used.

Upvotes: 1

Views: 404

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 49932

You can certainly use the Hash data structure to store your data this way. This design adds an additional operation to get/set the value of each field compared to just using simple KV strings (i.e. version:key as key and data as value). OTOH, by using simple String KV, you'll end up having a lot (few dozens times tens of millions) keys, each having an overhead greater than the approach you've suggested (more on the same at http://redis.io/topics/memory-optimization#using-hashes-to-abstract-a-very-memory-efficient-plain-key-value-store-on-top-of-redis).

That said, the Hash approach is valid and used extensively. The performance lost due to the extra operations is usually negligible, especially considering the savings in space.

Note: your question does not specify why/how you need to fetch the serialized data. Do you need to fetch all the keys under a given version? Maybe all the versions of a given key? Both? Other? Depending on your use case's requirements, there may be better alternatives.

Upvotes: 3

Related Questions