Skip to main content
Store a string value under a key. If the key already exists, its value and TTL are overwritten.

Syntax

SET key value
SET key value EX seconds

Parameters

key
string
required
The name of the key to set.
value
string
required
The string value to store.
EX
keyword
Optional flag to enable a time-to-live. Must be followed by a non-negative integer.
seconds
integer
Expiry duration in seconds from the current time. Must be >= 0. Required when EX is provided.

Return value

Returns OK on success.

Errors

ConditionError message
Wrong number of arguments(error) wrong number of arguments for 'SET'
seconds is negativeReturns (nil) — the EX clause is rejected and no key is written

Examples

>>> SET name radish
OK

>>> SET color red EX 30
OK

>>> SET counter 0
OK
Overwrite an existing key and remove its TTL:
>>> SET session abc123 EX 60
OK
>>> TTL session
(integer) 58

>>> SET session abc123
OK
>>> TTL session
(integer) -1

Notes

SET without EX stores expires_at = 0, which means the key has no expiration. If the key previously had a TTL, that TTL is cleared.
Every SET call is immediately appended to the append-only file (AOF), so the write is durable before the command returns.

Build docs developers (and LLMs) love