# MSETNX

> Set several keys only if none of them exist.

URL: https://docs.hotkv.com/commands/msetnx/

```
MSETNX key value [key value ...]
```

- Group: String
- Complexity: O(N) for N keys
- 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.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| All keys were set | Integer `1` | Integer `1` |
| At least one key existed, nothing was set | Integer `0` | Integer `0` |

## Examples

```
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
```

## Compatibility

Same as Redis 7 and Valkey 8.

## See also

[MSET](https://docs.hotkv.com/commands/mset/index.md), [MSETEX](https://docs.hotkv.com/commands/msetex/index.md),
[SETNX](https://docs.hotkv.com/commands/setnx/index.md)