content/commands/set.md
Set key to hold the string value.
If key already holds a value, it is overwritten, regardless of its type.
Any previous time to live associated with the key is discarded on successful SET operation.
The SET command supports a set of options that modify its behavior:
NX -- Only set the key if it does not already exist.XX -- Only set the key if it already exists.IFEQ ifeq-value -- Set the key’s value and expiration only if its current value is equal to ifeq-value. If the key doesn’t exist, it won’t be created.IFNE ifne-value -- Set the key’s value and expiration only if its current value is not equal to ifne-value. If the key doesn’t exist, it will be created.IFDEQ ifeq-digest -- Set the key’s value and expiration only if the hash digest of its current value is equal to ifeq-digest. If the key doesn’t exist, it won’t be created. See the Hash Digest section below for more information.IFDNE ifne-digest -- Set the key’s value and expiration only if the hash digest of its current value is not equal to ifne-digest. If the key doesn’t exist, it will be created. See the Hash Digest section below for more information.GET -- Return the old string stored at key, or nil if key did not exist. An error is returned and SET aborted if the value stored at key is not a string.EX seconds -- Set the specified expire time, in seconds (a positive integer).PX milliseconds -- Set the specified expire time, in milliseconds (a positive integer).EXAT timestamp-seconds -- Set the specified Unix time at which the key will expire, in seconds (a positive integer).PXAT timestamp-milliseconds -- Set the specified Unix time at which the key will expire, in milliseconds (a positive integer).KEEPTTL -- Retain the time to live associated with the key.Note: Since the SET command options can replace [SETNX]({{< relref "/commands/setnx" >}}), [SETEX]({{< relref "/commands/setex" >}}), [PSETEX]({{< relref "/commands/psetex" >}}), [GETSET]({{< relref "/commands/getset" >}}), it is possible that in future versions of Redis these commands will be deprecated and finally removed.
A hash digest is a fixed-size numerical representation of a string value, computed using the XXH3 hash algorithm. Redis uses this hash digest for efficient comparison operations without needing to compare the full string content. You can retrieve a key's hash digest using the [DIGEST]({{< relref "/commands/digest" >}}) command, which returns it as a hexadecimal string that you can use with the IFDEQ and IFDNE options, and also the [DELEX]({{< relref "/commands/delex" >}}) command's IFDEQ and IFDNE options.
{{% redis-cli %}} SET mykey "Hello" GET mykey
SET anotherkey "will expire in a minute" EX 60 {{% /redis-cli %}}
{{< clients-example set="set_and_get" step="set" description="Foundational: Set the string value of a key using SET (creates key if needed, overwrites existing value, supports expiration options)" difficulty="beginner" />}}
Note: The following pattern is discouraged in favor of [the Redlock algorithm]({{< relref "/develop/clients/patterns/distributed-locks" >}}) which is only a bit more complex to implement, but offers better guarantees and is fault tolerant.
The command SET resource-name anystring NX EX max-lock-time is a simple way to implement a locking system with Redis.
A client can acquire the lock if the above command returns OK (or retry after some time if the command returns Nil), and remove the lock just using [DEL]({{< relref "/commands/del" >}}).
The lock will be auto-released after the expire time is reached.
It is possible to make this system more robust modifying the unlock schema as follows:
DEL]({{< relref "/commands/del" >}}), send a script that only removes the key if the value matches.This avoids that a client will try to release the lock after the expire time deleting the key created by another client that acquired the lock later.
An example of unlock script would be similar to the following:
if redis.call("get",KEYS[1]) == ARGV[1]
then
return redis.call("del",KEYS[1])
else
return 0
end
The script should be called with EVAL ...script... 1 resource-name token-value
| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |
{{< multitabs id="set-return-info" tab1="RESP2" tab2="RESP3" >}}
GET was not specified, one of the following:
XX/IFEQ/IFDEQ was specified. The key was not created.NX was specified or a specified IFEQ/IFNE/IFDEQ/IFDNE condition is false. The key was not set.OK: The key was set.GET was specified, one of the following:
SET operation, whether the key was created of not.-tab-sep-
GET was not specified, one of the following:
XX/IFEQ/IFDEQ was specified. The key was not created.NX was specified or a specified IFEQ/IFNE/IFDEQ/IFDNE condition is false. The key was not set.OK: The key was set.GET was specified, one of the following:
SET operation, whether the key was created of not.{{< /multitabs >}}