Jay Kumar R
Jay Kumar R

Reputation: 537

Redis - hmset() in hash - efficiency

Let us say there is a Redis hash that has 10 key - vals. I read them all into a perl hash ( or any language map/hash/dict ) and need to update 3 of the values. Now I changed the 3 values in the hash, and need to write it back to redis. among (A) & (B) which one will be faster?

(A) $redis_handle->hmset($redis_hash_name,\%perl_hash);

or

(B) while ( ($k,$val) = each %three_changed_items_in_perl_hash ) { $redis_handle->hset($redis_hash_name,$k,$val); }

What I am trying to contemplate is : (A) is one communication to redis with 10 elements. But redis needs to update only 3 of them. Does redis waste time even if values is not changed? (B) is three communicaitons to redis with 1 element.

Upvotes: 0

Views: 520

Answers (1)

Itamar Haber
Itamar Haber

Reputation: 50022

Fastest would be to send a single HMSET that only has the updated fields and their respective values. Redis will only update these fields and will not do anything with the ones that aren't explicitly mentioned.

(C) $redis_handle->hmset($redis_hash_name,\%three_changed_items_in_perl_hash);

Upvotes: 2

Related Questions