# SADDEX

```json metadata
{
  "schema_version": 2,
  "title": "SADDEX",
  "description": "Add one or more members to a given set key, and optionally set their expiration.",
  "categories": ["docs","develop","stack","oss","rs","rc","oss","kubernetes","clients"],
  "arguments": [{"display_text":"key","key_spec_index":0,"name":"key","type":"key"},{"arguments":[{"display_text":"mnx","name":"mnx","token":"MNX","type":"pure-token"},{"display_text":"mxx","name":"mxx","token":"MXX","type":"pure-token"}],"name":"condition","optional":true,"type":"oneof"},{"arguments":[{"display_text":"seconds","name":"seconds","token":"EX","type":"integer"},{"display_text":"milliseconds","name":"milliseconds","token":"PX","type":"integer"},{"display_text":"unix-time-seconds","name":"unix-time-seconds","token":"EXAT","type":"unix-time"},{"display_text":"unix-time-milliseconds","name":"unix-time-milliseconds","token":"PXAT","type":"unix-time"},{"display_text":"keepttl","name":"keepttl","token":"KEEPTTL","type":"pure-token"}],"name":"expiration","optional":true,"type":"oneof"},{"arguments":[{"display_text":"nummembers","name":"nummembers","type":"integer"},{"display_text":"member","multiple":true,"name":"member","type":"string"}],"name":"members","token":"MEMBERS","type":"block"}],
  "syntax_fmt": "SADDEX key [MNX | MXX] [EX seconds | PX milliseconds |\n  EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]\n  MEMBERS nummembers member [member ...]",
  "complexity": "O(N) where N is the number of members being added.",
  "group": "set",
  "command_flags": ["write","denyoom","fast"],
  "acl_categories": ["@write","@set","@fast"],
  "since": "8.12.0",
  "arity": -5,
  "key_specs": [{"RW":true,"begin_search":{"spec":{"index":1},"type":"index"},"find_keys":{"spec":{"keystep":1,"lastkey":0,"limit":0},"type":"range"},"update":true}],
  "tableOfContents": {"sections":[{"id":"required-arguments","title":"Required arguments"},{"id":"optional-arguments","title":"Optional arguments"},{"id":"examples","title":"Examples"},{"id":"details","title":"Details"},{"id":"redis-software-and-redis-cloud-compatibility","title":"Redis Software and Redis Cloud compatibility"},{"id":"return-information","title":"Return information"},{"id":"see-also","title":"See also"},{"id":"related-topics","title":"Related topics"}]}

,
  "codeExamples": []
}
```

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.

## Required arguments

<details open><summary><code>key</code></summary>

The name of the key that holds the set.

</details>

<details open><summary><code>MEMBERS nummembers member [member ...]</code></summary>

The members to add. `nummembers` is the number of members, followed by that many member names.

</details>

## Optional arguments

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.

<details open><summary><code>MNX</code></summary>

Only add the members if none of them already exist in the set.

</details>

<details open><summary><code>MXX</code></summary>

Only update the members if all of them already exist in the set.

</details>

<details open><summary><code>EX seconds</code></summary>

Set the specified expiration time, in seconds.

</details>

<details open><summary><code>PX milliseconds</code></summary>

Set the specified expiration time, in milliseconds.

</details>

<details open><summary><code>EXAT unix-time-seconds</code></summary>

Set the specified Unix time, in seconds, at which the members expire.

</details>

<details open><summary><code>PXAT unix-time-milliseconds</code></summary>

Set the specified Unix time, in milliseconds, at which the members expire.

</details>

<details open><summary><code>KEEPTTL</code></summary>

Retain the TTL of members that already exist in the set. New members are added with no expiration.

</details>

## Examples

```redis
redis> SADDEX myset EX 300 MEMBERS 2 member1 member2
(integer) 1
redis> STTL myset MEMBERS 2 member1 member2
1) (integer) 300
2) (integer) 300
redis> SADDEX myset MNX EX 60 MEMBERS 2 member2 member3
(integer) 0
redis> SADDEX myset KEEPTTL MEMBERS 2 member1 member3
(integer) 1
redis> STTL myset MEMBERS 3 member1 member2 member3
1) (integer) 300
2) (integer) 300
3) (integer) -1
redis> SADDEX myset MEMBERS 1 member1
(integer) 1
redis> STTL myset MEMBERS 1 member1
1) (integer) -1
redis> SADDEX myset MXX PX 1 MEMBERS 2 member2 member4
(integer) 0
redis> SADDEX myset EX 10 KEEPTTL MEMBERS 1 member1
(error) ERR Only one of EX, PX, EXAT, PXAT or KEEPTTL arguments can be specified
```

## Details

`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.

The `MNX` and `MXX` conditions apply to the members as a group. If any member fails the condition, `SADDEX` changes nothing and returns `0`.

An 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.

See [`SEXPIRE`](https://redis.io/docs/latest/commands/sexpire#details) for how member expiration interacts with other set commands.

## Redis Software and Redis Cloud compatibility

| Redis<br />Software | Redis<br />Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> |
|:----------------------|:-----------------|:------|
| <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> |  |

## Return information

**RESP2:**

One of the following:
* [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.
* [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if at least one member was added, updated, or removed.
* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):
    - if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.
    - if the key exists but is not a set.

**RESP3:**

One of the following:
* [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.
* [Integer reply](https://redis.io/docs/latest/develop/reference/protocol-spec#integers): `1` if at least one member was added, updated, or removed.
* [Simple error reply](https://redis.io/docs/latest/develop/reference/protocol-spec#simple-errors):
    - if parsing failed, mandatory arguments are missing, unknown arguments are specified, or argument values are of the wrong type or out of range.
    - if the key exists but is not a set.



## See also

[`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)

## Related topics

- [Set member expiration](https://redis.io/docs/latest/develop/data-types/sets#member-expiration)

