Skip to main content
0 reactions

How to Set and Get Multiple Keys at Once in Valkey (MSET and MGET)

Featured image for How to Set and Get Multiple Keys at Once in Valkey, showing MSET replying OK, MGET returning apple, banana and cherry, and MSETNX replying 0.
Valkey MSET and MGET.

MSET saves several keys in one command, and MGET reads several at once. This guide stores three fruits in Valkey®.

Type the commands at the valkey-cli prompt. To open it, run valkey-cli in a terminal.

Note: Every command and output on this page is real, run on Valkey 9.2 on an Apple silicon Mac with macOS 26.

Syntax

SyntaxPlain text
MSET key value [key value ...]
MGET key [key ...]
MSETNX key value [key value ...]
  • key value: a key and the value to save under it. Repeat the pair for more keys.
  • MGET key ...: the keys to read, in the order you want the values back.
  • MSETNX: like MSET, but saves nothing if any of the keys already exists.

From the MSET (opens in a new tab), MGET (opens in a new tab) and MSETNX (opens in a new tab) documentation.

Step 1: Save Several Keys with MSET

127.0.0.1:6379>Plain text
MSET fruit:1 "apple" fruit:2 "banana" fruit:3 "cherry"
OutputPlain text
OK

Step 2: Read Several Keys with MGET

127.0.0.1:6379>Plain text
MGET fruit:1 fruit:2 fruit:3 fruit:4
OutputPlain text
1) "apple"
2) "banana"
3) "cherry"
4) (nil)

The values come back in the order of the keys. fruit:4 doesn’t exist, so its place holds (nil).

Step 3: Save Only If None Exist with MSETNX

127.0.0.1:6379>Plain text
MSETNX fruit:3 "grape" fruit:4 "kiwi"
OutputPlain text
(integer) 0

0: fruit:3 exists, so MSETNX saved nothing, not even fruit:4. With two new keys it saves both:

127.0.0.1:6379>Plain text
MSETNX fruit:4 "kiwi" fruit:5 "lemon"
OutputPlain text
(integer) 1
127.0.0.1:6379>Plain text
MGET fruit:3 fruit:4 fruit:5
OutputPlain text
1) "cherry"
2) "kiwi"
3) "lemon"
valkey-cli session where MSET saves three fruits, MGET returns apple, banana, cherry and nil, MSETNX replies 0 and then 1, and MGET shows cherry, kiwi and lemon.
Several keys saved and read in one command each.

Clean Up

127.0.0.1:6379>Plain text
DEL fruit:1 fruit:2 fruit:3 fruit:4 fruit:5
OutputPlain text
(integer) 5

Common Mistakes

MistakeWhat happensDo this instead
Expecting MSETNX to save the keys that are freeIt saves nothing if any key existsSave the free keys with MSET, or check its reply
Leaving out a value in MSETThe pairs shift and keys get the wrong valuesAlways give a key and a value for each pair

FAQ

Is MSET all or nothing?

Yes. The MSET documentation (opens in a new tab) says it’s atomic: no client ever sees some keys changed and others not.

Why use MGET instead of several GET commands?

One MGET is one request to the server instead of one per key.

Conclusion

MSET saves several keys at once, MGET reads them back in order, and MSETNX saves only when none of the keys exist.

Additional Resources

Valkey® is a registered trademark of LF Projects, LLC. Redis® is a registered trademark of Redis Ltd. Any rights therein are reserved to Redis Ltd. Other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with the Valkey project, LF Projects or Redis Ltd., and isn’t endorsed, supported or certified by them.

0 comments