{
  "schema_version": 2,
  "id": "commands/msetnx",
  "title": "MSETNX",
  "url": "https://redis.io/docs/latest/commands/msetnx/",
  "summary": "Atomically modifies the string values of one or more keys only when all keys don't exist.",
  "since": "1.0.1",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-09-18T16:45:12+01:00",
  "page_type": "content",
  "content_hash": "036d121d6df4b5feb922a8a86648d92622d1d493aa19eaf6f3e30cca3bf9fd29",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "> [!NOTE]\n> This command's behavior varies in clustered Redis environments. See the [multi-key operations](https://redis.io/docs/latest/develop/using-commands/multi-key-operations) page for more information.\n\n\nSets the given keys to their respective values.\n`MSETNX` will not perform any operation at all even if just a single key already\nexists.\n\nBecause of this semantic `MSETNX` can be used in order to set different keys\nrepresenting different fields of a unique logic object in a way that ensures\nthat either all the fields or none at all are set.\n\n`MSETNX` is atomic, so all given keys are set at once.\nIt is not possible for clients to see that some of the keys were updated while\nothers are unchanged."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key value [key value ...]</code></summary>\n\nOne or more key-value pairs to set. No keys are set if any of them already exists.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "redis> MSETNX key1 \"Hello\" key2 \"there\"\n(integer) 1\nredis> MSETNX key2 \"new\" key3 \"world\"\n(integer) 0\nredis> MGET key1 key2 key3\n1) \"Hello\"\n2) \"there\"\n3) (nil)"
    },
    {
      "id": "redis-software-and-redis-cloud-compatibility",
      "title": "Redis Software and Redis Cloud compatibility",
      "role": "content",
      "text": "| Redis<br />Software | Redis<br />Cloud | <span style=\"min-width: 9em; display: table-cell\">Notes</span> |\n|:----------------------|:-----------------|:------|\n| <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> | <span title=\"Supported\">&#x2705; Standard</span><br /><span title=\"Supported\"><nobr>&#x2705; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Integer reply](../../develop/reference/protocol-spec#integers): `0` if no key was set (at least one key already existed).\n* [Integer reply](../../develop/reference/protocol-spec#integers): `1` if all the keys were set.\n\n**RESP3:**\n\nOne of the following:\n* [Integer reply](../../develop/reference/protocol-spec#integers): `0` if no key was set (at least one key already existed).\n* [Integer reply](../../develop/reference/protocol-spec#integers): `1` if all the keys were set."
    }
  ],
  "examples": []
}
