TExcelTAD
Home › Redis › Redis Keys & Strings

Redis Keys & Strings

Every piece of data in Redis lives under a key. A key is a binary-safe string (any bytes, up to 512 MB, although short keys are wiser), and it holds exactly one value. The simplest value type is the string: a sequence of bytes that can be text, a number, JSON, or even a small image.

SET and GET

SET key value stores a string; GET key reads it back. SET overwrites whatever the key held before, even a value of another type.

redis · try it yourself
SET user:1:name "Ada Lovelace"
GET user:1:name
SET user:1:name "Ada King"
GET user:1:name
GET user:2:name
redis> SET user:1:name "Ada Lovelace"
OK
redis> GET user:1:name
"Ada Lovelace"
redis> SET user:1:name "Ada King"
OK
redis> GET user:1:name
"Ada King"
redis> GET user:2:name
(nil)

Naming keys

Redis has no tables, so the key name is where you put structure. The common convention is object-type:id:field, separated by colons:

Key Meaning
user:1001:name name of user 1001
product:42:stock stock level of product 42
session:9f3a1c data of one login session
cache:weather:hanoi cached weather for Hanoi
views:2026-10-17 page views on one day

Good key names are readable, predictable and not too long. A key like u1n saves a few bytes but makes debugging painful; a 200-character key wastes memory on every copy.

SET options

SET takes options that change when it writes and what it returns:

Option Effect
NX only set if the key does not exist
XX only set if the key already exists
GET return the old value (or nil)
EX seconds / PX ms set an expiry (see the Expiration lesson)
KEEPTTL keep the existing expiry when overwriting
redis · try it yourself
SET config:theme dark NX
SET config:theme light NX
GET config:theme
SET config:font serif XX
SET config:theme light XX
SET config:theme blue GET
GET config:theme
redis> SET config:theme dark NX
OK
redis> SET config:theme light NX
(nil)
redis> GET config:theme
"dark"
redis> SET config:font serif XX
(nil)
redis> SET config:theme light XX
OK
redis> SET config:theme blue GET
"light"
redis> GET config:theme
"blue"

When NX or XX prevents the write, the reply is (nil) instead of OK. SET ... NX is the building block of locks and "create only once" logic, and SETNX key value is an older shortcut for the same thing.

Several keys at once

MSET writes many keys in one command and MGET reads many. A missing key appears as (nil) in the reply, in the same position you asked for it:

redis · try it yourself
MSET city:1 Hanoi city:2 Lyon city:3 Osaka
MGET city:1 city:2 city:9 city:3
MSETNX city:3 Kyoto city:4 Lima
GET city:4
redis> MSET city:1 Hanoi city:2 Lyon city:3 Osaka
OK
redis> MGET city:1 city:2 city:9 city:3
1) "Hanoi"
2) "Lyon"
3) (nil)
4) "Osaka"
redis> MSETNX city:3 Kyoto city:4 Lima
(integer) 0
redis> GET city:4
(nil)

MSETNX is all-or-nothing: because city:3 already exists, it writes neither key and returns 0.

Working with parts of a string

Strings can be appended to and sliced. Positions are byte offsets from 0, and negative positions count from the end, just like Python slices (but the end index is inclusive):

redis · try it yourself
SET log "start"
APPEND log ";step1"
APPEND log ";step2"
GET log
STRLEN log
GETRANGE log 0 4
GETRANGE log -5 -1
SETRANGE log 0 "START"
GET log
redis> SET log "start"
OK
redis> APPEND log ";step1"
(integer) 11
redis> APPEND log ";step2"
(integer) 17
redis> GET log
"start;step1;step2"
redis> STRLEN log
(integer) 17
redis> GETRANGE log 0 4
"start"
redis> GETRANGE log -5 -1
"step2"
redis> SETRANGE log 0 "START"
(integer) 17
redis> GET log
"START;step1;step2"

APPEND and SETRANGE return the new length of the string. APPEND on a missing key behaves like SET.

Reading and removing in one step

GETDEL returns a value and deletes the key, which is handy for one-time tokens. GETSET is the old name for SET ... GET.

redis · try it yourself
SET token:reset:77 "a1b2c3"
GETDEL token:reset:77
GETDEL token:reset:77
EXISTS token:reset:77
redis> SET token:reset:77 "a1b2c3"
OK
redis> GETDEL token:reset:77
"a1b2c3"
redis> GETDEL token:reset:77
(nil)
redis> EXISTS token:reset:77
(integer) 0

Strings are binary safe

Redis does not care what the bytes mean. You can store JSON, serialized objects or UTF-8 text in other languages. redis-cli prints non-ASCII bytes as \x.. escapes, while your programming language's client library returns the original text:

redis · try it yourself
SET product:42 "{\"name\":\"Mug\",\"price\":8.5}"
GET product:42
SET greeting:vi "Xin chào"
GET greeting:vi
STRLEN greeting:vi
redis> SET product:42 "{\"name\":\"Mug\",\"price\":8.5}"
OK
redis> GET product:42
"{\"name\":\"Mug\",\"price\":8.5}"
redis> SET greeting:vi "Xin chào"
OK
redis> GET greeting:vi
"Xin ch\xc3\xa0o"
redis> STRLEN greeting:vi
(integer) 9

STRLEN counts bytes, not characters: à takes two bytes in UTF-8.

Note: Storing a whole object as a JSON string is fine when you always read and write it as a unit. When you need to update single fields, a hash (later lesson) is usually a better fit.

Test yourself

What does SET color red NX return if color already exists?

  1. OK
  2. (nil)
  3. (integer) 0
  4. (error) key exists

Given SET word redis, what does GETRANGE word 1 3 return?

  1. "ed"
  2. "edi"
  3. "edis"
  4. "red"

Which naming style is the usual Redis convention?

  1. user_1001_name
  2. user:1001:name
  3. User/1001/Name
  4. 1001.user.name