kadamb
kadamb

Reputation: 1738

Are HSET and HMSET same in Redis

I was going through the Redis documentation and experimenting with Redis and came across HSET and HMSET commands. And I could not find any difference between them, I tried searching, and found the following question,

What is the difference between HSET and HMSET method in Redis database

and accepted answer states :

HMSET is like HSET, but it allows multiple field/value pairs to be set at once.

But when I tried, HSET also allows multiple field/value pairs to be set at once.

Please see the screenshot below, to me, it seems both work same:

enter image description here

Can anyone please explain to me the difference between the two. Or the use cases, where one should be used over the other.

Thanks

Upvotes: 4

Views: 3563

Answers (4)

user12036066
user12036066

Reputation:

Was looking into it in 2019:)

In case someone is curious, from official documentation:

As per Redis 4.0.0, HMSET is considered deprecated. Please use HSET in new code.

https://redis.io/commands/hmset

Upvotes: 2

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230411

(I'm the author of that accepted answer in the linked topic. :))

Apparently, at some point since then, HSET command was updated to be variadic (same as HMSET). In my local redis version (4.0.2), it can, indeed, set multiple key-value pairs (although the command syntax hints in the redis-cli don't show that this is possible). However, the version which powers interactive console on redis.io still does not support that.

redis> HSET foo a 1 b 2 c 3
ERR ERR wrong number of arguments for 'hset' command

So yes, these do appear to be identical now.

Upvotes: 9

sazzad
sazzad

Reputation: 6267

From Redis 4.0.0, HSET servers purpose of both HSET and HMSET.

Redis 4.0.0 release note states:

HSET is now variadic, and HMSET is considered deprecated (but will be supported for years to come). Please use HSET in new code.

So, if you are using Redis 4.x+, you should use HSET for both single and multiple field/value pair(s).

Upvotes: 1

GuangshengZuo
GuangshengZuo

Reputation: 4687

Anyway HMSET / HSET is an historical design and HSET does nothing more than HMSET, you can replace HSET calls with HMSET and everything works.

Upvotes: 0

Related Questions