Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Redis · Module

MSET

MSET atomically assigns string values to one or more keys in a single command. Existing values are overwritten, and a successful command returns OK.

Since
Redis Open Source 1.0.1

How it works

MSET accepts one or more key-value pairs and writes every pair as a single atomic operation. Existing values are replaced, and the command returns OK when the operation completes successfully.

Syntax

MSET key value [key value ...]

Parameters

NameTypeDescription
keystringA key whose string value will be written.
valuestringThe string value assigned to the preceding key.
key value ...repeated key-value pairsAdditional key-value pairs; at least one pair is required.

Returns

Simple string reply: OK.

Key points

  • MSET writes each supplied key with its corresponding string value.
  • If a key already exists, its previous value is replaced.
  • The complete batch becomes visible atomically rather than as separate updates.
  • Readers cannot observe a partially applied MSET batch.
  • Execution cost grows linearly with the number of key-value pairs.
  • A successful invocation returns OK.

Common mistakes

  • In clustered Redis, multi-key behavior can differ; consult the multi-key operations guidance when keys may be distributed across hash slots.
  • MSET overwrites existing values; use MSETNX when conditional, no-overwrite behavior is required.

Example: Set multiple string keys

MSET user:1 Alice user:2 Bob
GET user:1
GET user:2

Stores two string values in one command, then reads them back.

When to use it

Batch cache population

Write several related cache entries while reducing client-server round trips.

Atomic multi-key update

Replace a group of string values so readers see either the old or complete new state.

Bulk initialization

Initialize multiple independent string keys in one request.

Interview questions

QWhat does MSET do, and is it atomic?

MSET writes multiple string key-value pairs in one command and replaces values that already exist. It is atomic, so clients cannot observe only part of the batch.

Follow-up: How does its complexity scale? It is O(N), where N is the number of keys being set.

QWhat is MSET's syntax and return value?

The command takes alternating keys and values: MSET key value [key value ...]. It returns the simple string reply OK because the command cannot fail under its documented conditions.

Follow-up: What would you use to avoid overwriting existing keys? MSETNX, which provides conditional multi-key creation.

QWhat should you consider when using MSET with Redis Cluster?

MSET is useful for reducing round trips when several string values must be written together. In clustered deployments, however, its multi-key behavior varies, so key placement must be considered.

Follow-up: What cluster constraint should be investigated? Whether the participating keys can be handled together according to Redis cluster multi-key operation rules.

  • MSETNX
  • MGET
  • SET
  • multi-key operations

Source: MSET | Docs - Redis. Checked 2026-10-16.