Redis Developer Course Redis Cloud Service Redis-Gate Server

Save multiple keys if Not eXists

Saves the given keys to their respective values. MSETNX will not perform any operation at all even if just a single key already exists. MSETNX is atomic, so all given keys are seved at once. It is not possible for clients to see that some of the keys were updated while others are unchanged.
Usage is MSETNX key1 value1 key2 value2.

Return value

Integer reply, specifically:

  • 1 if the all the keys were set.
  • 0 if no key was set (at least one key already existed).


command>msetnx key1 Hello key2 Redis
command>msetnx key2 XXXXX key3 Server
result>0   Failed to save because key2 already exists
command>mget key1 key2 key3
result>1) Hello
2) Redis
3) (nil)
Note MSETNX is atomic, so all given keys are seved at once.


MSETNX key value [key value ...]

  • Available since 1.0.1.
  • Time complexity(Logical processing time): O(N)
Related Commands SET, SETNX, MGET
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

To practice the command, click here to run Redis Web Client


Click for a question or to see comments.  Comments :    Viewed :

Email 답글이 올라오면 이메일로 알려드리겠습니다.