Strings

Strings #

26 commands: the SET family, counters, bit operations and string expiry.

CmdRWSyntaxReturnsExample → reply
SETWSET KEY VALUE [EX seconds] [PX milliseconds] [NX] [XX] [KEEPTTL]+OK, or null when NX/XX blocks the writeSET user:1 '{"n":"ada"}' EX 60 → OK
SETNXWSETNX KEY VALUEinteger 1 written, 0 already existsSETNX job:42 1 → 1
SETEXWSETEX KEY seconds VALUE+OKSETEX session:9 60 tok → OK
SETEXATWSETEXAT KEY unixTime VALUE+OKSETEXAT session:9 1767225600 tok → OK
GETSETWGETSET KEY VALUEbulk old value, null if absentGETSET counter 0 → "7"
GETRGET KEYbulk, null if absentGET user:1 → {"n":"ada"}
MGETRMGET KEY [KEY ...]array of bulk/nullMGET a b → [val, null]
MSETWMSET KEY VALUE [KEY VALUE ...]+OKMSET a 1 b 2 → OK
APPENDWAPPEND KEY VALUEinteger new lengthAPPEND log "x\n" → 5
STRLENRSTRLEN KEYinteger length in bytesSTRLEN user:1 → 13
INCRWINCR KEYinteger new valueINCR hits → 1
INCRBYWINCRBY KEY incrementinteger new valueINCRBY hits 5 → 6
DECRWDECR KEYinteger new valueDECR hits → 5
DECRBYWDECRBY KEY decrementinteger new valueDECRBY hits 5 → 0
GETRANGERGETRANGE KEY start endbulk substring (inclusive, negative offsets from the end)GETRANGE user:1 0 3 → {"n"
SETRANGEWSETRANGE KEY offset VALUEinteger new lengthSETRANGE user:1 0 '{"m"' → 13
GETBITRGETBIT KEY offsetinteger 0/1GETBIT bitmap 7 → 0
SETBITWSETBIT KEY offset valueinteger previous bitSETBIT bitmap 7 1 → 0
BITCOUNTRBITCOUNT KEY [start end [BYTE|BIT]]integer number of set bitsBITCOUNT bitmap 0 0 → 1
BITOPWBITOP AND|OR|XOR|NOT destKey KEY [KEY ...]integer length of the resultBITOP AND out a b → 2
BITPOSRBITPOS KEY bit [start [end [BYTE|BIT]]]integer position, -1 if not foundBITPOS bitmap 1 → 7
DELWDEL KEY [KEY ...]integer keys removedDEL a b → 2
EXISTSREXISTS KEY [KEY ...]integer number of existing keysEXISTS a b → 1
EXPIREWEXPIRE KEY secondsinteger 1 set, 0 key absentEXPIRE session:9 60 → 1
EXPIREATWEXPIREAT KEY unixTimeinteger 1 set, 0 key absentEXPIREAT session:9 1767225600 → 1
TTLRTTL KEYinteger seconds left, -1 no expirationTTL session:9 → 59

Notes #

  • SET clears an existing expiration unless KEEPTTL is given. PX is rounded up to the next second because expirations are stored in whole seconds, so PX 500 keeps the key 1 s and PX 1500 keeps it 2 s — never less than asked.
  • SET ... NX/XX answers 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, EXPIRE again or use SET … KEEPTTL.
  • EXISTS counts keys in the string keyspace only; it will not see a hash with the same name. Use HKEYEXISTS, LKEYEXISTS, SKEYEXISTS for the other types.
  • DEL deletes the string at those keys, not the same-named hash/list/set.
  • INCR/DECR on a non-numeric value answer -ERR, like Redis; a missing key is treated as 0.
  • BITCOUNT/BITPOS accept the optional BYTE/BIT unit 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.
  • TTL is computed against the local wall clock by the storage layer, so replicas can report values that differ by their apply latency.