Anish
Anish

Reputation: 219

Which one to use Hset or HMSet in Redis?

I am kind of new to Redis. I am just trying to store values in Redis using the HashSet method available using StackExchange.Redis.StrongName assembly (Lets say I have 4000 items). Is it better to store the 4000 items individually (using HSet ) or shall I pass a dictionary (using HMSet) so it will call only Redis server call is required but a huge amount of data. Which one is better?

Thanks in Advance

Upvotes: 2

Views: 4511

Answers (2)

Itamar Haber
Itamar Haber

Reputation: 49982

TL;DR A single call is "better" in terms of performance.

Taking into consideration @dizzyf's answer about HMSET's deprecation, the question becomes "should I use a lot of small calls instead of one big one?". Because there is an overhead to process every command, it is usually preferred to "batch" calls together to reduce the price.

Some commands in Redis are varidiac, a.k.a dynamic arity, meaning they can accept one or more values to eliminate multiple calls. That said, overloading a single call with a huge amount of arguments is also not the best practice - that typically leads to massive memory allocations and blocks the server from serving other requests during its processing.

I would approach this by dividing the values to constant-sized "batches" - 100 is a good start, but you can always tune it afterwards - and sending each such "batch" in a single HSET key field1 value1 ... field100 value1000 call.

Pro tip: if your Redis client supports it, you can use pipelining to make everything more responsive ("better"?).

Upvotes: 2

dizzyf
dizzyf

Reputation: 3693

HMSET has been deprecated as of redis 4.0.0 in favor of using HSET with multiple key/value pairs:

https://redis.io/commands/hset

https://redis.io/commands/hmset

Performance will be O(n)

Upvotes: 5

Related Questions