MSETNX(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– MSETNX β€” the all-or-nothing queen πŸ‘‘

okay bestie, so this is msetnx, page 3valkey, from the Valkey Command Manual. and it is literally the most "all of us or none of us" girlie in the whole string family. let's get into it πŸ’…


✨ NAME ✨

msetnx β€” atomically modifies the string values of one or more keys only when all keys don't exist. no cap. it's giving commitment issues but make it ✨atomic✨.


🌸 SYNOPSIS 🌸

MSETNX key value [key value ...]

yeah so it's just key-value pairs, stacked, as many as you want 🫢


πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the tea. MSETNX sets the given keys to their respective values. but β€” and this is the whole personality β€” MSETNX will not perform any operation at all even if just a single key already exists. one key already there? boom. nobody gets set. the entire operation just. does not happen. πŸ™ˆ

Because of this semantic, MSETNX can be used in order to set different keys representing different fields of a unique logic object in a way that ensures that either all the fields or none at all are set. like a package deal. you're not getting half the group chat to show up to brunch, it's everyone or we're cancelling πŸŽ€

And because it's atomic, all given keys are set at once. It is not possible for clients to see that some of the keys were updated while others are unchanged. no in-between states, no cursed half-finished timeline, no catching it mid-glow-up πŸ’–


πŸ’€ REPLY πŸ’€

One of the following:


⭐ COMPLEXITY ⭐

O(N) where N is the number of keys to set. so the more keys you bring to the party, the longer the party takes. basic math bestie ✨


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@slow @string @write

(translation: she's slow-ish, she's a string girlie, and she writes. iconic trio tbh)


🌈 HISTORY 🌈

  • Available since: 1.0.1 β€” a genuine OG, she's been here since basically forever 🧚

πŸ”₯ EXAMPLES πŸ”₯

watch her work her magic:

127.0.0.1:6379> MSETNX key1 "Hello" key2 "there"
(integer) 1
127.0.0.1:6379> MSETNX key2 "new" key3 "world"
(integer) 0
127.0.0.1:6379> MGET key1 key2 key3
1) "Hello"
2) "there"
3) (nil)

see what happened?? first call: neither key exists β†’ everything gets set β†’ 1. second call: key2 already exists β†’ the WHOLE thing gets refused β†’ 0, and key3 never got set at all. then MGET shows key2 still holding its old value "there" and key3 is just (nil). the way she said "nope, not doing it" 😭 periodt.


πŸ’œ SEE ALSO πŸ’œ

her besties and adjacent girlies:


and that's the whole page, bestie!! this was version 9.0.0, dated 2025-10-21, straight out of the Valkey Command Manual. go forth and set all your keys or literally none of them, no in-between πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from