{
  "schema_version": 2,
  "id": "commands/saddex",
  "title": "SADDEX",
  "url": "https://redis.io/docs/latest/commands/saddex/",
  "summary": "Add one or more members to a given set key, and optionally set their expiration.",
  "since": "8.12.0",
  "tags": [
    "docs",
    "develop",
    "stack",
    "oss",
    "rs",
    "rc",
    "oss",
    "kubernetes",
    "clients"
  ],
  "last_updated": "2026-11-23T06:19:08-08:00",
  "page_type": "content",
  "content_hash": "48164879b5a356dbf8b76b40a6156dd58faaf58bb2b0880b3d0104c4448037b7",
  "sections": [
    {
      "id": "overview",
      "title": "Overview",
      "role": "overview",
      "text": "Add one or more members to a set, and optionally specify their expiration time or time to live (TTL). If the key doesn't exist, Redis creates a new set."
    },
    {
      "id": "required-arguments",
      "title": "Required arguments",
      "role": "content",
      "text": "<details open><summary><code>key</code></summary>\n\nThe name of the key that holds the set.\n\n</details>\n\n<details open><summary><code>MEMBERS nummembers member [member ...]</code></summary>\n\nThe members to add. `nummembers` is the number of members, followed by that many member names.\n\n</details>"
    },
    {
      "id": "optional-arguments",
      "title": "Optional arguments",
      "role": "parameters",
      "text": "The following options modify the command's behavior. `MNX` and `MXX` are mutually exclusive. The `EX`, `PX`, `EXAT`, `PXAT`, and `KEEPTTL` options are also mutually exclusive.\n\n<details open><summary><code>MNX</code></summary>\n\nOnly add the members if none of them already exist in the set.\n\n</details>\n\n<details open><summary><code>MXX</code></summary>\n\nOnly update the members if all of them already exist in the set.\n\n</details>\n\n<details open><summary><code>EX seconds</code></summary>\n\nSet the specified expiration time, in seconds.\n\n</details>\n\n<details open><summary><code>PX milliseconds</code></summary>\n\nSet the specified expiration time, in milliseconds.\n\n</details>\n\n<details open><summary><code>EXAT unix-time-seconds</code></summary>\n\nSet the specified Unix time, in seconds, at which the members expire.\n\n</details>\n\n<details open><summary><code>PXAT unix-time-milliseconds</code></summary>\n\nSet the specified Unix time, in milliseconds, at which the members expire.\n\n</details>\n\n<details open><summary><code>KEEPTTL</code></summary>\n\nRetain the TTL of members that already exist in the set. New members are added with no expiration.\n\n</details>"
    },
    {
      "id": "examples",
      "title": "Examples",
      "role": "example",
      "text": "[code example]"
    },
    {
      "id": "details",
      "title": "Details",
      "role": "content",
      "text": "`SADDEX` applies the expiration option to every member you list, both new and existing. Without an expiration option, it adds the members with no TTL and discards the TTL of any member that already has one. Use `KEEPTTL` to keep existing TTLs instead. [`SADD`](https://redis.io/docs/latest/commands/sadd) never changes a member's TTL.\n\nThe `MNX` and `MXX` conditions apply to the members as a group. If any member fails the condition, `SADDEX` changes nothing and returns `0`.\n\nAn expiration of 0, or a Unix time in the past, removes the listed members from the set instead of adding them. If that leaves the set empty, Redis deletes the key.\n\nSee [`SEXPIRE`](https://redis.io/docs/latest/commands/sexpire#details) for how member expiration interacts with other set commands."
    },
    {
      "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=\"Not supported\">&#x274c; Standard</span><br /><span title=\"Not supported\"><nobr>&#x274c; Active-Active</nobr></span> | <span title=\"Not supported\">&#x274c; Standard</span><br /><span title=\"Not supported\"><nobr>&#x274c; Active-Active</nobr></span> |  |"
    },
    {
      "id": "return-information",
      "title": "Return information",
      "role": "returns",
      "text": "**RESP2:**\n\nOne of the following:\n* [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `0` if no member was added, updated, or removed. This includes the case where the `MNX` or `MXX` condition was not met.\n* [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if at least one member was added, updated, or removed.\n* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):\n    - if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.\n    - if the key exists but is not a set.\n\n**RESP3:**\n\nOne of the following:\n* [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `0` if no member was added, updated, or removed. This includes the case where the `MNX` or `MXX` condition was not met.\n* [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if at least one member was added, updated, or removed.\n* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):\n    - if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.\n    - if the key exists but is not a set."
    },
    {
      "id": "see-also",
      "title": "See also",
      "role": "related",
      "text": "[`SEXPIRE`](https://redis.io/docs/latest/commands/sexpire) | [`SPEXPIRE`](https://redis.io/docs/latest/commands/spexpire) | [`SEXPIREAT`](https://redis.io/docs/latest/commands/sexpireat) | [`SPEXPIREAT`](https://redis.io/docs/latest/commands/spexpireat) | [`STTL`](https://redis.io/docs/latest/commands/sttl) | [`SPTTL`](https://redis.io/docs/latest/commands/spttl) | [`SEXPIRETIME`](https://redis.io/docs/latest/commands/sexpiretime) | [`SPEXPIRETIME`](https://redis.io/docs/latest/commands/spexpiretime) | [`SPERSIST`](https://redis.io/docs/latest/commands/spersist)"
    },
    {
      "id": "related-topics",
      "title": "Related topics",
      "role": "related",
      "text": "- [Set member expiration](https://redis.io/docs/latest/develop/data-types/sets#member-expiration)"
    }
  ],
  "examples": [
    {
      "id": "examples-ex0",
      "language": "redis",
      "code": "redis> SADDEX myset EX 300 MEMBERS 2 member1 member2\n(integer) 1\nredis> STTL myset MEMBERS 2 member1 member2\n1) (integer) 300\n2) (integer) 300\nredis> SADDEX myset MNX EX 60 MEMBERS 2 member2 member3\n(integer) 0\nredis> SADDEX myset KEEPTTL MEMBERS 2 member1 member3\n(integer) 1\nredis> STTL myset MEMBERS 3 member1 member2 member3\n1) (integer) 300\n2) (integer) 300\n3) (integer) -1\nredis> SADDEX myset MEMBERS 1 member1\n(integer) 1\nredis> STTL myset MEMBERS 1 member1\n1) (integer) -1\nredis> SADDEX myset MXX PX 1 MEMBERS 2 member2 member4\n(integer) 0\nredis> SADDEX myset EX 10 KEEPTTL MEMBERS 1 member1\n(error) ERR Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments can be specified",
      "section_id": "examples"
    }
  ]
}
