Redis MSET Command Explained

In Redis, the MSET command allows us to set multiple keys at once. It replaces any existing values, just as SET does.

Syntax

The syntax goes like this:

MSET key value [ key value ...]

Which basically means you can set multiple key/value pairs.

Example

Here’s a basic example to demonstrate:

MSET name "Woof" type "Dog" age "10"

Result:

OK

In this case, the MSET operation was successful, and so we got a simple string reply of OK.

We can now use the MGET command to return the values of each of those keys. Here’s an example of doing that:

MGET name type age

Result:

1) "Woof"
2) "Dog"
3) "10"

Replacing Existing Values

We can use MSET to replace existing values:

MSET name "Big Woof" age "11"

Result:

OK

Now let’s get the values again:

MGET name type age

Result:

1) "Big Woof"
2) "Dog"
3) "11"

So we can see that the two keys were updated, and the other one remained the same (because we didn’t include it the second time we used MSET).