Strings #
26 commands: the SET family, counters, bit operations and string expiry.
| Cmd | RW | Syntax | Returns | Example → reply |
|---|---|---|---|---|
SET | W | SET KEY VALUE [EX seconds] [PX milliseconds] [NX] [XX] [KEEPTTL] | +OK, or null when NX/XX blocks the write | SET user:1 '{"n":"ada"}' EX 60 → OK |
SETNX | W | SETNX KEY VALUE | integer 1 written, 0 already exists | SETNX job:42 1 → 1 |
SETEX | W | SETEX KEY seconds VALUE | +OK | SETEX session:9 60 tok → OK |
SETEXAT | W | SETEXAT KEY unixTime VALUE | +OK | SETEXAT session:9 1767225600 tok → OK |
GETSET | W | GETSET KEY VALUE | bulk old value, null if absent | GETSET counter 0 → "7" |
GET | R | GET KEY | bulk, null if absent | GET user:1 → {"n":"ada"} |
MGET | R | MGET KEY [KEY ...] | array of bulk/null | MGET a b → [val, null] |
MSET | W | MSET KEY VALUE [KEY VALUE ...] | +OK | MSET a 1 b 2 → OK |
APPEND | W | APPEND KEY VALUE | integer new length | APPEND log "x\n" → 5 |
STRLEN | R | STRLEN KEY | integer length in bytes | STRLEN user:1 → 13 |
INCR | W | INCR KEY | integer new value | INCR hits → 1 |
INCRBY | W | INCRBY KEY increment | integer new value | INCRBY hits 5 → 6 |
DECR | W | DECR KEY | integer new value | DECR hits → 5 |
DECRBY | W | DECRBY KEY decrement | integer new value | DECRBY hits 5 → 0 |
GETRANGE | R | GETRANGE KEY start end | bulk substring (inclusive, negative offsets from the end) | GETRANGE user:1 0 3 → {"n" |
SETRANGE | W | SETRANGE KEY offset VALUE | integer new length | SETRANGE user:1 0 '{"m"' → 13 |
GETBIT | R | GETBIT KEY offset | integer 0/1 | GETBIT bitmap 7 → 0 |
SETBIT | W | SETBIT KEY offset value | integer previous bit | SETBIT bitmap 7 1 → 0 |
BITCOUNT | R | BITCOUNT KEY [start end [BYTE|BIT]] | integer number of set bits | BITCOUNT bitmap 0 0 → 1 |
BITOP | W | BITOP AND|OR|XOR|NOT destKey KEY [KEY ...] | integer length of the result | BITOP AND out a b → 2 |
BITPOS | R | BITPOS KEY bit [start [end [BYTE|BIT]]] | integer position, -1 if not found | BITPOS bitmap 1 → 7 |
DEL | W | DEL KEY [KEY ...] | integer keys removed | DEL a b → 2 |
EXISTS | R | EXISTS KEY [KEY ...] | integer number of existing keys | EXISTS a b → 1 |
EXPIRE | W | EXPIRE KEY seconds | integer 1 set, 0 key absent | EXPIRE session:9 60 → 1 |
EXPIREAT | W | EXPIREAT KEY unixTime | integer 1 set, 0 key absent | EXPIREAT session:9 1767225600 → 1 |
TTL | R | TTL KEY | integer seconds left, -1 no expiration | TTL session:9 → 59 |
Notes #
SETclears an existing expiration unlessKEEPTTLis given.PXis rounded up to the next second because expirations are stored in whole seconds, soPX 500keeps the key 1 s andPX 1500keeps it 2 s — never less than asked.SET ... NX/XXanswers a null bulk when the condition blocks the write, not an error.- There is no string
PERSIST: the handler exists but is commented out and not registered. To keep a value and change its lifetime,EXPIREagain or useSET … KEEPTTL. EXISTScounts keys in the string keyspace only; it will not see a hash with the same name. UseHKEYEXISTS,LKEYEXISTS,SKEYEXISTSfor the other types.DELdeletes the string at those keys, not the same-named hash/list/set.INCR/DECRon a non-numeric value answer-ERR, like Redis; a missing key is treated as0.BITCOUNT/BITPOSaccept the optionalBYTE/BITunit as in Redis 7.- Expiry arithmetic uses the Raft clock, so a short TTL can be rejected on a node whose Raft clock lags its local clock. See Compatibility.
TTLis computed against the local wall clock by the storage layer, so replicas can report values that differ by their apply latency.