Skip to content

Ask the HotKV docs

Answers come only from these docs and can be wrong; check the linked pages before you rely on them.

MSETNX

Last updated
MSETNX key value [key value ...]
Group
String
Complexity
O(N) for N keys
Arity
-3 (at least 3 words, counting the command name)
Flags
write
Keys
first 1, last final argument, step 2
HotKV Cloud
All plans
On-Premise
no license needed

MSETNX stores all the given key-value pairs only when none of the keys exist. If even one key exists, nothing is written. The check and the writes happen as one step, which makes MSETNX useful for claiming a group of names together.

In a cluster, all keys must hash to the same slot.

Case RESP2 RESP3
All keys were set Integer 1 Integer 1
At least one key existed, nothing was set Integer 0 Integer 0
127.0.0.1:6379> MSETNX seat:1 "ada" seat:2 "grace"
(integer) 1
127.0.0.1:6379> MSETNX seat:2 "linus" seat:3 "ken"
(integer) 0
127.0.0.1:6379> EXISTS seat:3
(integer) 0

Same as Redis 7 and Valkey 8.

MSET, MSETEX, SETNX